NewtonBallSetConeLimits twist problem

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Re: NewtonBallSetConeLimits twist problem

Postby Julio Jerez » Sat Jun 30, 2012 11:57 am

you cna set the rigid body matrix at the location of eth visual object, abd teh center of mass at teh cetne of the bone

that way bone matrix an dteh visual matrix will match. you also nee to ofsset the trransformation of eth collision shape to the center of mass.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: NewtonBallSetConeLimits twist problem

Postby JoeJ » Sat Jun 30, 2012 1:21 pm

I'm not sure if Julios suggestion affects physics - i mean the true center of mass of a forearm should stay at the center of it, not at the ellbow position.

Another way worth mentioning anyways is:

// You get the matrix from the body:
matrix mb;
NewtonBodyGetMAtrix (mb, body);

// bodies point 3 units in z, so we need to translate 1.5 units backwards to get to the bone-tip:
mb[3] -= mb[2] * 1.5; // we subtract the local z axis from the translation to achieve that.

Now you can use that matrix for graphics.



You could do the same with matrix math:
matrix pompb = { // "precomputed offset matrix per bone"
1,0,0,0
0,1,0,0
0,0,1,0
0,0,-1.5,1};

gfxBoneMatrix = bodyMatrix * pompb;
// or:
gfxBoneMatrix = pompb * bodyMatrix;
// depending on your math libs conventions (i don't know DirectX)

... this looks complex and first method is faster, but if you also need to fix rotation it will make sense, because you only need to modify pompb-matrix.
You can also put the Scaling-correction you need in that pompb-matrix.
Something that could fix most of your mismatch is:
matrix pompb = {
1,0,0,0
0,1,0,0
0,0,0.333,0 // make z axis less long to fix the scale
0.5, 0, -1.5, 1}; // try the 0.5 value both positive and negative, and also at y instead of x, as i can't see the axis in your screenshot i can only guess.

Again - visualize all Transforms to know what you're doing.
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: NewtonBallSetConeLimits twist problem

Postby belfegor » Sat Jun 30, 2012 1:29 pm

@Julio
Well i tried your proposal and it looks scaled a bit along Z axis, so when i do "picking" on bodies its somehow like there is a space between those 3:

Image

instead this:

Image

Maybie i am missing something stupid, code:
Code: Select all
NewtonBody* CreateNewtonBody(NewtonCollision* coll, float* matrix, float mass)
{
   D3DXVECTOR3 inertia, origin;
   NewtonConvexCollisionCalculateInertialMatrix(coll, inertia, origin);
   float Ixx = mass * inertia.x;
   float Iyy = mass * inertia.y;
   float Izz = mass * inertia.z;
   NewtonBody* body = NewtonCreateBody(NWorld, coll, matrix);
   NewtonBodySetMassMatrix(body, mass, Ixx, Iyy, Izz);
   return body;
}

...

D3DXMATRIX bodyMatrix[3];
   D3DXMatrixTranslation(&bodyMatrix[0], 0.0f, 0.0f, -4.5f); // hard-coded bones matrix but it is correct
   D3DXMatrixTranslation(&bodyMatrix[1], 0.0f, 0.0f, -1.5f);
   D3DXMatrixTranslation(&bodyMatrix[2], 0.0f, 0.0f, 1.5f);

   D3DXMATRIX offsetMatrix[3];
   D3DXMatrixTranslation(&offsetMatrix[0], 0.0f, 0.0f, -3.0f);
   D3DXMatrixTranslation(&offsetMatrix[1], 0.0f, 0.0f, 0.0f);
   D3DXMatrixTranslation(&offsetMatrix[2], 0.0f, 0.0f, 1.5f);

   D3DXVECTOR3 bonePos[3];
   bonePos[0] = D3DXVECTOR3(bodyMatrix[0].m[3][0], bodyMatrix[0].m[3][1], bodyMatrix[0].m[3][2]);
   bonePos[1] = D3DXVECTOR3(bodyMatrix[1].m[3][0], bodyMatrix[1].m[3][1], bodyMatrix[1].m[3][2]);
   bonePos[2] = D3DXVECTOR3(bodyMatrix[2].m[3][0], bodyMatrix[2].m[3][1], bodyMatrix[2].m[3][2]);

   D3DXVECTOR3 size(1.0f, 1.0f, 3.0f);
   float mass = 30.0f;
   NewtonCollision* coll[3];
   coll[0]   = NewtonCreateBox(NWorld, size.x, size.y, size.z, 0, offsetMatrix[0]);
   coll[1]   = NewtonCreateBox(NWorld, size.x, size.y, size.z, 0, offsetMatrix[1]);
   coll[2]   = NewtonCreateBox(NWorld, size.x, size.y, size.z, 0, offsetMatrix[2]);
   b0 = CreateNewtonBody(coll[0], bodyMatrix[0], mass);
   b1 = CreateNewtonBody(coll[1], bodyMatrix[1], mass);
   b2 = CreateNewtonBody(coll[2], bodyMatrix[2], mass);
   
   D3DXVECTOR3 pivot[2];
   pivot[0] = bonePos[1];
   pivot[1] = bonePos[2];
   
   D3DXVECTOR3 pin[2];
   pin[0] = bonePos[1] - bonePos[0];
   pin[1] = bonePos[2] - bonePos[1];
   D3DXVec3Normalize(&pin[0], &pin[0]);
   D3DXVec3Normalize(&pin[1], &pin[1]);
   
   D3DXMATRIX pinPivMatrix[2];
   D3DXMatrixIdentity(&pinPivMatrix[0]);
   D3DXMatrixIdentity(&pinPivMatrix[1]);
   pinPivMatrix[0].m[0][2] = pin[0].x;
   pinPivMatrix[0].m[1][2] = pin[0].y;
   pinPivMatrix[0].m[2][2] = pin[0].z;
   pinPivMatrix[0].m[3][0] = pivot[0].x;
   pinPivMatrix[0].m[3][1] = pivot[0].y;
   pinPivMatrix[0].m[3][2] = pivot[0].z;

   pinPivMatrix[1].m[0][2] = pin[1].x;
   pinPivMatrix[1].m[1][2] = pin[1].y;
   pinPivMatrix[1].m[2][2] = pin[1].z;
   pinPivMatrix[1].m[3][0] = pivot[1].x;
   pinPivMatrix[1].m[3][1] = pivot[1].y;
   pinPivMatrix[1].m[3][2] = pivot[1].z;

   NewtonUserJoint* j0 = CreateCustomBallAndSocket(pinPivMatrix[0], b1, b0);
   NewtonUserJoint* j1 = CreateCustomBallAndSocket(pinPivMatrix[1], b2, b1);

   float coneMax = D3DX_PI * 0.2f;
   float twistMax = 0.0f;

   BallAndSocketSetConeAngle (j0, coneMax);
    BallAndSocketSetTwistAngle (j0, twistMax, twistMax);

   BallAndSocketSetConeAngle (j1, coneMax);
    BallAndSocketSetTwistAngle (j1, twistMax, twistMax);

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


Thank you for your time.

EDIT:
@JoeJ
Give me time to try your suggestion too. Thanks greatly on your help.
User avatar
belfegor
 
Posts: 53
Joined: Mon Sep 18, 2006 4:42 pm
Location: Serbia

Re: NewtonBallSetConeLimits twist problem

Postby belfegor » Sat Jun 30, 2012 1:58 pm

@JoeJ
Looks like your solution is fixed my errors:
Code: Select all
D3DXMATRIX bodyMat[3];
   NewtonBodyGetMatrix(b0, bodyMat[0]);
   NewtonBodyGetMatrix(b1, bodyMat[1]);
   NewtonBodyGetMatrix(b2, bodyMat[2]);
...
// draw 3 visualisation boxes
...

    D3DXMATRIX bonesMatMove[3];
   D3DXMatrixTranslation(&bonesMatMove[0], 0.0f, -0.5f, 0.0f);
   D3DXMatrixTranslation(&bonesMatMove[1], 0.0f, -0.5f, 0.0f);
   D3DXMatrixTranslation(&bonesMatMove[2], 0.0f, -0.5f, 0.0f);

   D3DXVECTOR3 vScale(1.0f, 1.0f, 0.33f);
   D3DXMATRIX bonesMatScale[3];
   D3DXMatrixScaling(&bonesMatScale[0], vScale.x, vScale.y, vScale.z);
   D3DXMatrixScaling(&bonesMatScale[1], vScale.x, vScale.y, vScale.z);
   D3DXMatrixScaling(&bonesMatScale[2], vScale.x, vScale.y, vScale.z);

   D3DXMATRIX bonesMat[3];
   bonesMat[0] = bonesMatScale[0] * bonesMatMove[0];
   bonesMat[1] = bonesMatScale[1] * bonesMatMove[1];
   bonesMat[2] = bonesMatScale[2] * bonesMatMove[2];

   bodyMat[0] = bonesMat[0] * bodyMat[0];
   bodyMat[1] = bonesMat[1] * bodyMat[1];
   bodyMat[2] = bonesMat[2] * bodyMat[2];
...
//use bodyMat for bones
User avatar
belfegor
 
Posts: 53
Joined: Mon Sep 18, 2006 4:42 pm
Location: Serbia

Re: NewtonBallSetConeLimits twist problem

Postby JoeJ » Sat Jun 30, 2012 2:16 pm

belfegor wrote:Looks like your solution is fixed my errors:


Nice, but now you need to find out, why this fix works.
The value of 0.333 gives little sense: Maybe Max creates bones that use the scale of one axis to keep track of its "one over length" (= 1/3) - ???
The value of -0.5 was only a guess from the screenshots - absolutely no sense there. :(

Do the boxes collide correctly with all faces at the ground?
If so, gfx and physycs align, and you can exclude Newton from further bug-searching, and i guess its somewhere in Max to engine or skinning pipeline.


EDIT:
-0.5 * 3 = 1.5

so we have both fix-numbers we expected (3 as scale and 1.5 as translation).
But is till wonder, why the 0.5 needs to be at y and not z position.
Last edited by JoeJ on Sat Jun 30, 2012 2:28 pm, edited 1 time in total.
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: NewtonBallSetConeLimits twist problem

Postby belfegor » Sat Jun 30, 2012 2:24 pm

The value of -0.5 was only a guess from the screenshots - absolutely no sense there.

Me too, i have "boned" mesh and visualisation boxes centered at origin.

All in all i have to investigate where and what the problem is caused.

BTW. Do you use some offset matrix for your "boned" ragdolls or directly use body matrix?

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

Re: NewtonBallSetConeLimits twist problem

Postby Julio Jerez » Sat Jun 30, 2012 2:34 pm

like I said you need to use teh direct matrix, and set the center of mass to the center of the bone. also the collision center should be the offset to the center of the bone.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: NewtonBallSetConeLimits twist problem

Postby JoeJ » Sat Jun 30, 2012 2:38 pm

belfegor wrote:Do you use some offset matrix for your "boned" ragdolls or directly use body matrix?


Do not force me to look at my own code :)
I guess i construct the skeleton from the positions of the joints, but with orientations from the bodies - can't remember.

offset matrix is usual practice - some more scary example:
Years ago ragdolls have had less joints than animation skeletons (no clavicle...).
Physics Lib Havoc has complex tools to match the high-res skeleton to the low-res physics.
So offset matrix is pretty cheap compared to that :)
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: NewtonBallSetConeLimits twist problem

Postby JoeJ » Sat Jun 30, 2012 2:49 pm

Julio Jerez wrote:set the center of mass to the center of the bone


... so my doupts are gone.
Belfegor - it should safe you some work to use Julios suggestion.
You don't need to keep track of the offsets yourself if you let newton do that for you.
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: NewtonBallSetConeLimits twist problem

Postby belfegor » Sat Jun 30, 2012 3:12 pm

I am still experimenting, no good results so far. :oops:
It would be awesome if Newton could do that for me.

Maybie i don't understand this "offset" matrix for collision. With what it is relative to?

Also when you said "center of mass", with what is relative to?

This function NewtonBodySetCentreOfMass?

Thanks you 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 » Sat Jun 30, 2012 4:57 pm

Basically the center of mass and the offset matrix of a collision are child matrices of the body matrix,
the same way a bone is a child bone of another bone

Say that you make a collision box of 1 x 1 x 1
In Newton that box will have the center at position 0, 0, 0
And the Box will be [(-0.5, -0.5, -0.5) (0.5, 0.5, 0.5)]
Say your world is a floor plane of high 0.
If you position that box in the world at location 0, 0, 0
The lower half of the box will be below the ground the ground for you to see the box you will have to place at location 0, 0.5, 0
Now let us say the visual geometry is also a box by with an origin at 0, 0, 0
But the box is [(-0.5, -0.0, -0.5) (0.5, 1.0, 0.5)]

The collision but at 0.5 will make thw collision good but the visual will be 0.5 above the floor.

To fix that problem you can do as fallow

Get the collision box form the body, and

Make a transformation matrix (offset matrix) that is and identity matrix and set the position to a vector
0, 0.5, 0

Now you can either make the collision box with this matrix, or get the collision box form the body and set the collision matrix to that previous matrix

After you do that the collision box can be place at location 0, 0, 0
But the lower half will be flushed with the floor.
When you get the transformation matrix form the body, it will match the visual mesh.

This will make the collision and the visual mesh to match.
To make the dynamics behavior correct you also need to set the center of mass of the body to the position of the collision box.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Previous

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest

cron