• Advertisement
Sign in to follow this  

Box2DLite porting. Questions.

This topic is 2444 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello. I'm trying to port famous Box2DLite to AS3.0 (yeah, I know that AS3 version of Box2D exists, but I need to port it myself to pump my physics skill) and there're some question. First of all - I don't know C++ (but can read it a little) and thats main reason that my ported version works incorrectly. If you don't mind, I'll post my questions here and I hope you'll help a little ).
#1. There're strange construction in Arbiter constructor:
[code]Arbiter::Arbiter(Body* b1, Body* b2)
{
if (b1 < b2)
{
body1 = b1;
body2 = b2;
}
else
{
body1 = b2;
body2 = b1;
}
...
}[/code]
And there;re no overloading '<' operator in Body class. So what does it mean and why are we need such check?

Share this post


Link to post
Share on other sites
Advertisement
In this case, operator<() simply sorts based on pointer values. Without looking at the source code I can't tell you exactly what the context is, but one reason you might do this would be to facilitate easy comparison of Arbiter objects (assuming that b1, b2 and b2, b1 should be considered to be the same pair).

Whatever the reason for the sorting is, you should be able to determine it by looking at the code for the Arbiter class in its entirety. (Most likely there's an operator or other function somewhere in the class that relies on the two bodies being in a pre-determined order.)

Share this post


Link to post
Share on other sites
Thanks, jyk. I think I understand now. There is map<ArbiterKey, Arbiter> and later there is search in this map by ArbiterKey. So I think that's the reason.

Share this post


Link to post
Share on other sites
As far as I remember Box2D the sorting there is only to add some consistency such that the same pair of bodies is always processed in the "same order". This could be done by assigning id to the bodies and sorting by that or just sort by the pointers since they are unique too... Basically, he just wants and arbitrary order so he might as well use the pointers.

Share this post


Link to post
Share on other sites
If you do the port for learning purposes I strongly encourage you do this. On the other site Erin mentioned yesterday that some people took Adobe Alchemy to port Box2D to flash and it gave very good performance. So if you want to use Box2D just in Flash this might be a much easier route to go.

[url="http://labs.adobe.com/technologies/alchemy/"]http://labs.adobe.co...logies/alchemy/[/url]

Share this post


Link to post
Share on other sites
japro, thanks. Dirk, yeah, it's purely for learning - I want to understand every line of code.
Next question:
#2. There's Collide class and there's collide() method that contains:
[code]var RotA:Matrix22 = new Matrix22(bodyA.rotation);//creates current rotation matrices
var RotB:Matrix22 = new Matrix22(bodyB.rotation);

var RotAT:Matrix22 = RotA.transpose();//inverse of rotation matrices
var RotBT:Matrix22 = RotB.transpose();

var dp:Vector2 = posB.subtract(posA);//dist between bodies

var dA:Vector2 = RotAT.multiplyByVector(dp);//converts dist between bodies to local frame of each body
var dB:Vector2 = RotBT.multiplyByVector(dp);

//this I can't understand - what is C and what is absolute and why we need it??? (In code abs - converts all entries of vector/matrix to positive values)
var C:Matrix22 = RotAT.multiplyByMatrix(RotB);
var absC:Matrix22 = C.absolute();
var absCT:Matrix22 = absC.transpose();

// Box A faces
var faceA:Vector2 = dA.absolute().subtract(hA).subtract(absC.multiplyByVector(hB));
if (faceA.x > 0 || faceA.y > 0) return 0;

// Box B faces
var faceB:Vector2 = dB.absolute().subtract(absCT.multiplyByVector(hA)).subtract(hB);
if (faceB.x > 0 || faceB.y > 0) return 0;[/code]
There's comment about absolute matrix - what is this and why?

Share this post


Link to post
Share on other sites
C represents the coordinate frame of one of the boxes expressed with respect to the coordinate frame of the other. absolute() probably returns the matrix with all elements set to their absolute values.

The code in question implements the separating axis test (SAT) for two boxes. The SAT for boxes typically involves computing the absolute values of the dot products of pairs of box axes (one from the first box and one from the second), and it looks like that's what absC represents here.

Share this post


Link to post
Share on other sites
Thanks jyk, but I know SAT a little and this code definitely not SAT - there's no axes at all - there's some sort of projections. But how are they work - for example this line:
[code]absC.multiplyByVector(hB)//here hB - is a distance from bodys center to vertex.[/code]
What does it do? It's not rotation - but what is it?

Share this post


Link to post
Share on other sites
[quote name='Volgogradetzzz' timestamp='1303466359' post='4801553']
this code definitely not SAT[/quote]
Are you sure? :)

Have you considered the possibility that it is the SAT, but expressed in a form you're not accustomed to?

(Disclaimer: I'm fairly certain it is in fact the SAT, but to be completely certain I'd need to see the function in its entirety, and would probably need to work through it to make sure it's doing what I think it's doing.)

Share this post


Link to post
Share on other sites
Ok, jyk. I'll agree that maybe it's sat - but not in usual form that I see before. And post #6 is entire function, i.e. it's enougth to check penetration.

Share this post


Link to post
Share on other sites
Hello again!
At last i'm getting close to soul of Box2D - famous Sequential Impulses. I've read Catto's presentations, investigate code, but anyway it's hard to understand the purpose of such technique. Using this sequential impulses he reduces final impulse applied to body. But why? isn't it cheaper to manually decrease it than recalculate it in loop. Also there no bounce at all - i.e. collisions fully inelastic. I added some restitution and immidiatelly get jittering. So what the profit of sequential impulses???

Share this post


Link to post
Share on other sites
Seqential Impulses are verz easy to implement and give very good results. There are some tricks like slobs and warmstarting. You can look into the ODE and check their iterative Projected Gauss-Seidel and even the direkt Dantzig solver. Let me know what you find easier to implement :)

You have to be careful with restitution. If the relative velocity falls below some threshold ( roughly 1-2 m/s ) you use the usual bias. Otherwise you get jitter. So this is a problem of your implementation and not sequential impulses :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement