NewtonBallSetConeLimits twist problem

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

NewtonBallSetConeLimits twist problem

Postby belfegor » Wed Jun 27, 2012 5:41 am

I "chained" 3 boxes and set twist to 0 but it is still twisting, can someone tell me what i am doing wrong:

Code: Select all
D3DXMATRIX Mat0, Mat1, Mat2;
   D3DXMatrixTranslation(&Mat0, 0.0f, 0.0f, -3.0f);
   D3DXMatrixTranslation(&Mat1, 0.0f, 0.0f, 0.0f);
   D3DXMatrixTranslation(&Mat2, 0.0f, 0.0f, 3.0f);

   D3DXVECTOR3 size(1.0f, 1.0f, 3.0f);
   float mass = 2.0f;
   float Ixx = 0.7f * mass * (size.y * size.y + size.z * size.z) / 12.0f;
   float Iyy = 0.7f * mass * (size.x * size.x + size.z * size.z) / 12.0f;
   float Izz = 0.7f * mass * (size.x * size.x + size.y * size.y) / 12.0f;
   
   NewtonCollision* coll = NewtonCreateBox(NWorld, size.x, size.y, size.z, 0, 0);
   b0 = NewtonCreateBody(NWorld, coll, Mat0);
   NewtonBodySetMassMatrix(b0, mass, Ixx, Iyy, Izz);

   b1 = NewtonCreateBody(NWorld, coll, Mat1);
   NewtonBodySetMassMatrix(b1, mass, Ixx, Iyy, Izz);

   b2 = NewtonCreateBody(NWorld, coll, Mat2);
   NewtonBodySetMassMatrix(b2, mass, Ixx, Iyy, Izz);
   
   D3DXVECTOR3 pivot0, pivot1;
   pivot0 = D3DXVECTOR3(0.0f, 0.0f, -1.5f);
   pivot1 = D3DXVECTOR3(0.0f, 0.0f, 1.5f);
   NewtonJoint* j0 = NewtonConstraintCreateBall(NWorld, pivot0, b1, b0);
   NewtonJoint* j1 = NewtonConstraintCreateBall(NWorld, pivot1, b2, b1);

   float cone  = D3DXToRadian(90.0f);
   float twist = 0.0f;
   D3DXVECTOR3 pin = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
   NewtonBallSetConeLimits(j0, pin, cone, twist);
   NewtonBallSetConeLimits(j1, pin, cone, twist);

   NewtonBodySetForceAndTorqueCallback( b0, ApplyForceAndTorque );
   NewtonBodySetForceAndTorqueCallback( b1, ApplyForceAndTorque );
   NewtonBodySetForceAndTorqueCallback( b2, ApplyForceAndTorque );


Video of a problem:



Thanks for your time.
User avatar
belfegor
 
Posts: 53
Joined: Mon Sep 18, 2006 4:42 pm
Location: Serbia

Re: NewtonBallSetConeLimits twist problem

Postby Julio Jerez » Wed Jun 27, 2012 7:53 am

you nee to use the custom joint library
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: NewtonBallSetConeLimits twist problem

Postby belfegor » Wed Jun 27, 2012 8:39 am

You mean constraintball is deprecated?
User avatar
belfegor
 
Posts: 53
Joined: Mon Sep 18, 2006 4:42 pm
Location: Serbia

Re: NewtonBallSetConeLimits twist problem

Postby Julio Jerez » Wed Jun 27, 2012 8:54 am

yes. all build in joint are deprecated since newton 1.53
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: NewtonBallSetConeLimits twist problem

Postby belfegor » Wed Jun 27, 2012 9:32 am

What is the first param for CreateCustomBallAndSocket function? Vector? Matrix?
User avatar
belfegor
 
Posts: 53
Joined: Mon Sep 18, 2006 4:42 pm
Location: Serbia

Re: NewtonBallSetConeLimits twist problem

Postby belfegor » Thu Jun 28, 2012 11:42 am

Can someone PLEASE just to confirm me about CreateCustomBallAndSocket first param.
1. Is it using for pivot position:
Code: Select all
matrix[3][0]
matrix[3][1]
matrix[3][2]

and this for pin orientation:
Code: Select all
matrix[0][2]
matrix[1][2]
matrix[2][2]

?

2. Say i have 2 boxes with dimensions (3, 1, 1) conected:

Image

pin would be (1, 0, 0) right?

Thank you for your time.
User avatar
belfegor
 
Posts: 53
Joined: Mon Sep 18, 2006 4:42 pm
Location: Serbia

Re: NewtonBallSetConeLimits twist problem

Postby JoeJ » Thu Jun 28, 2012 4:43 pm

Your assumption about pivot position is correct.
But pin vector is:

matrix[0][0]
matrix[0][1]
matrix[0][2]

== x axis == matrix.m_front.

An idendity matrix with position set to the center of both boxes should do what you want given the example of your screenshot.
So i wonder why ho have problems.

Note: Don't forget to set the last row correctly (matrix[3][3] = 1, matrix[0-2][3] = 0)

And: Newtons default ball and socket allows only an cone angle smaller than 180 degrees -
otherwise flipping problems will happen, with bad explosive behaviour.
You can exceed that limit by creating your own custom joint and a quaternion twist.
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: NewtonBallSetConeLimits twist problem

Postby belfegor » Thu Jun 28, 2012 7:02 pm

Thank you JoeJ.

I thought Newton is using DX like matrices (row major, LH)?
If i set like in your example:
Code: Select all
D3DXMATRIX pinPivMat;
D3DXMatrixIdentity(&pinPivMat);
pinPivMat.m[0][0] = 0.0f;
pinPivMat.m[0][1] = 0.0f;
pinPivMat.m[0][2] = 1.0f;

pinPivMat.m[3][0] = pivot0.x;
pinPivMat.m[3][1] = pivot0.y;
pinPivMat.m[3][2] = pivot0.z;
...

It doesn't work (weird behavior, twisting and what not...), althogh in my project i have 2 boxes "pointing" in positive Z so it has to be a bit different, but works with my first asumption:
Code: Select all
pinPivMat.m[0][2] = 0.0f;
pinPivMat.m[1][2] = 0.0f;
pinPivMat.m[2][2] = 1.0f;


I just wanna be sure if i am doing the right thing.

I don't need cone larger then 180. :)
User avatar
belfegor
 
Posts: 53
Joined: Mon Sep 18, 2006 4:42 pm
Location: Serbia

Re: NewtonBallSetConeLimits twist problem

Postby JoeJ » Fri Jun 29, 2012 9:57 am

... hm, there is some more useless confusion to discover.

Trial and error should be the best way to get it working for you but i want to clear something out:
you got my example slightly wrong, it should be:
float matrix[4][4] = {
1,0,0, 0, // x axis row == pin vector == axis of the limit cone
0,1,0, 0, // y axis row...
0,0,1, 0,
pivotX, pivotY, pivotZ, 1
};

if you'd apply your "1st assumption" to this,
matrix[0][2] = 0.0f;
matrix[1][2] = 0.0f;
matrix[2][2] = 1.0f;
... nothing will change.
And that should work for the example from your screenshot.

But now you say, you have 2 boxes "pointing" in positive Z,
while in screenshot they "point" in X... that's why i am confused now.
Maybe all this is only mismatch of assumptions and different conventions.
Trying out will give you more of "Now I'm sure" than asking other people :)


However, if the boxes point in Z direction, and i assume cone should be also point to Z, an example matrix would be:
float matrix[4][4] = {
0,0,1, 0, // pin vector now in Z dir
1,0,0, 0,
0,1,0, 0,
pivotX, pivotY, pivotZ, 1
};


Some other convention pitfalls you may meet:
Newton uses OpenGL order (which does not mean you have to convert the matrices to use in DirectX),
but its math lib uses swapped multiplication order for both mats and quats:
Newton: a = b * c;
Most other Math Libs: a = c * b;

If you work with quaternions:
Newton: q(w,x,y,z);
Usual: q(x,y,z,w);
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: NewtonBallSetConeLimits twist problem

Postby belfegor » Fri Jun 29, 2012 1:46 pm

However, if the boxes point in Z direction, and i assume cone should be also point to Z, an example matrix would be:
Code: Select all
float matrix[4][4] = {
0,0,1, 0, // pin vector now in Z dir
1,0,0, 0,
0,1,0, 0,
pivotX, pivotY, pivotZ, 1
};



You shifted second & third row to left by one, is that intentional?

As i said i tried setting first row to {0 0 1 0} but i got weird grafical result, this is with my project where i setup 2 boxes to point in Z positive axis.

Trying out will give you more of "Now I'm sure" than asking other people

I don't like to guess, it will break my code later, and there is no clear up-to-date documentation to rely on.
If i could know by the params/function names i would probably already have enough knowledge to build my own physics library, but i don't so i have to beg for answeres.

Thanks greatly for yor help so far.
User avatar
belfegor
 
Posts: 53
Joined: Mon Sep 18, 2006 4:42 pm
Location: Serbia

Re: NewtonBallSetConeLimits twist problem

Postby JoeJ » Fri Jun 29, 2012 3:58 pm

belfegor wrote:You shifted second & third row to left by one, is that intentional?


It's not a shift, ots more a "rotation" of the rows:
0->1
1->2 // 1st row becomes 2nd and so on
2->0
Doing it that way i can be sure that the coordinate system stays left-handed.
(or is it right-handed? I don't usually care about conventions - if there are 2 possibilities i simply try out and don't care for the right term)
This also results in a true rotation in 3d space.

If you would use that (without the "shift"):
0,0,1 x // swap this row with another to get z axis as the pin vector
0,1,0 x
1,0,0 x
x x x x
... you would do a rotation, but also a "flip" or "mirror" - one axis pointed in positive direction before, but points in negative direction afterwards.
Thus you change from left handed to right handed (or vice versa) - and maybe that explains why the joint jumps wildly around!
You could fix that also by negating any one or two 1s in that matrix, as a second "flip" makes it right it again.

Conclusion of that funny game is:
The matrix you give must be a standart orthonormal transormation matrix - no sheer - no scale - no flip,
same as used anywhere else in Newton. Mostly you can use a copy of the matrix from one of the bodies an change only the translation.

belfegor wrote:I don't like to guess, it will break my code later

What really really helps me in situations like that is visualizing.
I our case that would mean to visualize the pin vector (and others...) inside the joint's SubmitConstraints function.
That would really destroy your doubts, as you could see what happens at runtime and where that vector points.
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: NewtonBallSetConeLimits twist problem

Postby JoeJ » Fri Jun 29, 2012 4:04 pm

belfegor wrote:You shifted second & third row to left by one, is that intentional?


It's not a shift, ots more a "rotation" of the rows:
0->1
1->2 // 1st row becomes 2nd and so on
2->0
Doing it that way i can be sure that the coordinate system stays left-handed.
(or is it right-handed? I don't usually care about conventions - if there are 2 possibilities i simply try out and don't care for the right term)
This also results in a true rotation in 3d space.

If you would use that (without the "shift"):
0,0,1 x // swap this row with another to get z axis as the pin vector
0,1,0 x
1,0,0 x
x x x x
... you would do a rotation, but also a "flip" or "mirror" - one axis pointed in positive direction before, but points in negative direction afterwards.
Thus you change from left handed to right handed (or vice versa) - and maybe that explains why the joint jumps wildly around!
You could fix that also by negating any one or two 1s in that matrix, as a second "flip" makes it right it again.

Conclusion of that funny game is:
The matrix you give must be a standart orthonormal transormation matrix - no sheer - no scale - no flip,
same as used anywhere else in Newton. Mostly you can use a copy of the matrix from one of the bodies an change only the translation.

belfegor wrote:I don't like to guess, it will break my code later

What really really helps me in situations like that is visualizing.
I our case that would mean to visualize the pin vector (and others...) inside the joint's SubmitConstraints function.
That would destroy your doubts once and for all, as you could see what happens at runtime and where that vector points.
See my screenshot - shoulders of a standing ragdoll. It's hard to see the bodies :) but there is visualisation of the cones and the twist.
That's some work, but the only way for me to be really "sure" (I trust more my eyes than a lot of numbers)

vis.jpg
vis.jpg (94.43 KiB) Viewed 6188 times
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: NewtonBallSetConeLimits twist problem

Postby belfegor » Fri Jun 29, 2012 4:57 pm

Thanks greatly for your help, i really appreciate it.

Since you mention ragdolls i have another question and it seams you are experienced Newton user. :)

I have created mesh with 3 bones:

Image

Each bone is 3 "units" in lenght (total 9 units), pointing in positive Z axis, and centered at origin.
At the same time i render that "boned" mesh and 3 simple boxes to see if it match up, but it does not as "boned" mesh apeirs somewhat scaled in lenght and some have offset too:

Image

Red is "boned" mesh, purple is boxes. This is by using same matrix for both, NewtonBodyGetMatrix

Any tip you can give me to correct my test ragdoll?
User avatar
belfegor
 
Posts: 53
Joined: Mon Sep 18, 2006 4:42 pm
Location: Serbia

Re: NewtonBallSetConeLimits twist problem

Postby JoeJ » Fri Jun 29, 2012 5:19 pm

Visualize the 3 transforms that you get from Newton and use to skin the mesh.
It would be interresting if they are at the center of the bodies or not.
Draw red line for x-axis, green for y, blue for z ... everyone uses that color theme.
Code to visualize a transform matrix like that is extremely useful and reusable - you'll see nonuniform scaling, sheering, offset...

Wireframe view would be good too to see the tesselation like in the model screenshot.
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: NewtonBallSetConeLimits twist problem

Postby belfegor » Sat Jun 30, 2012 10:37 am

I think i know what the problem is but cannot wrap my head how to solve it.
Transform/world matrix given by Newton is at a center of body/object, but for bones it should be at a "tip" of that object and that is probably the cause of my problem.
How do i calculate correct offset to get proper matrices for my bones?

Thanks for your time.
User avatar
belfegor
 
Posts: 53
Joined: Mon Sep 18, 2006 4:42 pm
Location: Serbia

Next

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest