Jump to content

  • Log In with Google      Sign In   
  • Create Account

Box2DLite porting. Questions.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 nikitablack   Members   -  Reputation: 614

Like
0Likes
Like

Posted 21 April 2011 - 12:48 AM

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:
Arbiter::Arbiter(Body* b1, Body* b2)
{
	if (b1 < b2)
	{
		body1 = b1;
		body2 = b2;
	}
	else
	{
		body1 = b2;
		body2 = b1;
	}
...
}
And there;re no overloading '<' operator in Body class. So what does it mean and why are we need such check?


Sponsor:

#2 scgames   Members   -  Reputation: 1981

Like
0Likes
Like

Posted 21 April 2011 - 01:17 AM

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.)

#3 nikitablack   Members   -  Reputation: 614

Like
0Likes
Like

Posted 21 April 2011 - 01:29 AM

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.

#4 japro   Members   -  Reputation: 887

Like
0Likes
Like

Posted 21 April 2011 - 04:41 AM

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.

#5 Dirk Gregorius   Members   -  Reputation: 817

Like
0Likes
Like

Posted 21 April 2011 - 09:15 AM

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.

http://labs.adobe.co...logies/alchemy/

#6 nikitablack   Members   -  Reputation: 614

Like
0Likes
Like

Posted 22 April 2011 - 01:48 AM

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:
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;
There's comment about absolute matrix - what is this and why?

#7 scgames   Members   -  Reputation: 1981

Like
0Likes
Like

Posted 22 April 2011 - 02:11 AM

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.

#8 nikitablack   Members   -  Reputation: 614

Like
0Likes
Like

Posted 22 April 2011 - 03:59 AM

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:
absC.multiplyByVector(hB)//here hB - is a distance from bodys center to vertex.
What does it do? It's not rotation - but what is it?

#9 scgames   Members   -  Reputation: 1981

Like
0Likes
Like

Posted 22 April 2011 - 04:12 AM

this code definitely not SAT

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.)

#10 nikitablack   Members   -  Reputation: 614

Like
0Likes
Like

Posted 22 April 2011 - 04:50 AM

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.

#11 nikitablack   Members   -  Reputation: 614

Like
0Likes
Like

Posted 11 May 2011 - 02:54 AM

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???

#12 Dirk Gregorius   Members   -  Reputation: 817

Like
1Likes
Like

Posted 11 May 2011 - 09:17 AM

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 :)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS