Stiff Joint

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Re: Stiff Joint

Postby Carli » Mon Jan 11, 2010 9:31 am

then i have an alternative question:

how can i deactivate a body? - nothing is colliding with it, the body will not simulated etc. but i don't want to delete the body and i will set the matrix of the body each frame.
Carli
 
Posts: 245
Joined: Fri Oct 02, 2009 5:28 am

Re: Stiff Joint

Postby kallaspriit » Mon Jan 11, 2010 11:21 am

Perhaps move it outside world limits? There are probably better ways :P
kallaspriit
 
Posts: 216
Joined: Sun Aug 14, 2005 6:31 pm

Re: Stiff Joint

Postby Carli » Mon Jan 11, 2010 11:49 am

Maybe I should say more about the abstract issue:

I have a character and a vehicle.

How do I attach the Character to the vehicle?

My current implementation sets the collision of the character to a null-collision and updates the matrix of the character each frame so that i don't have to change the rendering code.
Carli
 
Posts: 245
Joined: Fri Oct 02, 2009 5:28 am

Re: Stiff Joint

Postby JernejL » Mon Jan 11, 2010 1:47 pm

I simply delete the character controller collision when character enters car, and it just works.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1587
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Stiff Joint

Postby Carli » Tue Jan 12, 2010 5:29 am

That could be no opinion for me because the car has one graphics and the character has an other one. Both are drawn at the newton object matrix. (And i don't want to change the rendering code)
I solved the problem in giving the character a null collision and updating the matrix of the object each frame. (relative to the object it is attached to)

thanks for the effort.
Carli
 
Posts: 245
Joined: Fri Oct 02, 2009 5:28 am

Re: Stiff Joint

Postby carli2 » Wed May 23, 2012 3:12 am

Hi,

I'm reviving this topic again because I'm needing this thing again.

In my current implementation, I have a function attachTo which gives the attached body a null collision and updates the position each frame. This means, the collision of the attached body is not considered in the compound.
What do you think is the best way to also consider the collision of the second body?
(Best would be if you also decide which body is hitted by an other object, but that's optional)
carli2
 
Posts: 157
Joined: Thu Nov 10, 2011 1:53 pm

Re: Stiff Joint

Postby JoeJ » Wed May 23, 2012 3:31 pm

JernejL wrote:viewtopic.php?f=15&t=4056&p=27525&hilit=rigid+joint (example rigid joint implementation)


That works and while it's not 100% stiff it's surely a good way to attach a player to a car, if that's still what you want it for.
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: Stiff Joint

Postby carli2 » Tue May 29, 2012 5:19 am

JoeJ wrote:
JernejL wrote:viewtopic.php?f=15&t=4056&p=27525&hilit=rigid+joint (example rigid joint implementation)


That works and while it's not 100% stiff it's surely a good way to attach a player to a car, if that's still what you want it for.


I tried that, but I did not really understand where to get matrix0 and matrix1 from. I took the matrix of the two joined bodies, but the bodies clamped together and rotated as wild.

I also tried to use single LinearRows to understand the behaviour (I used matrix0 as pivot0 and matrix0+(0,4,0) as pivot1, that should stack both bodies.... did not work as expected)
carli2
 
Posts: 157
Joined: Thu Nov 10, 2011 1:53 pm

Re: Stiff Joint

Postby JoeJ » Tue May 29, 2012 9:30 am

carli2 wrote:I tried that, but I did not really understand where to get matrix0 and matrix1 from. I took the matrix of the two joined bodies


That's right and should work.
EDIT: For sure you need some offset , otherwise the bodies try to fight to be at the same position.
You tried that later? Maybe it did not work because you used a worldspace offset - try to costruct it using one of the bodies orientation, like in my example.

Julios example depends on joint library, so i guess you have a little problem extending it or recreating functionality from its base class.
I'll try to give a sample most simple as possible.
It' s a ball and socket joint - distance between center of mass is constrained, but orientation not.
Whe you've got that working, it should be easy to extend it.


Code: Select all

// setup ...

NewtonJoint* joint = NewtonConstraintCreateUserJoint (world, 3, MyCallback, carBodyPtr, playerBodyPtr);
MyData data
data.body0 = carBodyPtr;
data.body1 = playerBodyPtr;
NewtonJointSetUserData (joint, &data);



// callback...

void MyCallback (const NewtonJoint* joint, float timestep, int threadIndex)
{
MyData* data= (MyData*) NewtonJointGetUserData (joint);
dMatrix matrix0, matrix1;
NewtonBodyGetMatrix (data->body0, matrix0);
NewtonBodyGetMatrix (data->body1, matrix1);

matrix1.posit += matrix1.up.scale (10.0); // create some offset

NewtonUserJointAddLinearRow (joint, (float*)&matrix0.posit, (float*)&matrix1.posit, (float*)&matrix0.up);
NewtonUserJointAddLinearRow (joint, (float*)&matrix0.posit, (float*)&matrix1.posit, (float*)&matrix0.front);
NewtonUserJointAddLinearRow (joint, (float*)&matrix0.posit, (float*)&matrix1.posit, (float*)&matrix0.right); // fix positional displacement in all dimensions

}

User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: Stiff Joint

Postby carli2 » Tue May 29, 2012 10:53 am

I finally managed to implement a proper working rigid joint :)

It really behaves like one body but with multiple collision parts.

Here's the code of the callback:
Code: Select all
procedure callback(const userJoint: PNewtonJoint; timestep : Float; threadIndex : int); cdecl;
var m0, m1: TMatrix4;
    p0, p1: TVec3;
begin
  NewtonBodyGetMatrix(NewtonJointGetBody0(userJoint), @m0[0][0]);
  NewtonBodyGetMatrix(NewtonJointGetBody1(userJoint), @m1[0][0]);

  p0:=MatrixGetTransform(m0); // the is-matrix
  p1:=MatrixGetTransform(TJoint(NewtonJointGetUserData(userJoint)).matrix*m1); // read out the relative matrix and calculate the target should-be-matrix
  // movement for going to the right position
  NewtonUserJointAddLinearRow(userJoint, p0, p1, @m1[0][0]);
  NewtonUserJointAddLinearRow(userJoint, p0, p1, @m1[1][0]);
  NewtonUserJointAddLinearRow(userJoint, p0, p1, @m1[2][0]);
  // copy rotation and orientation
  NewtonUserJointAddAngularRow(userJoint, 0, @IdentityMatrix[0][0]);
  NewtonUserJointAddAngularRow(userJoint, 0, @IdentityMatrix[1][0]);
  NewtonUserJointAddAngularRow(userJoint, 0, @IdentityMatrix[2][0]);
end;
carli2
 
Posts: 157
Joined: Thu Nov 10, 2011 1:53 pm

Re: Stiff Joint

Postby JoeJ » Tue May 29, 2012 11:10 am

carli2 wrote: NewtonUserJointAddAngularRow(userJoint, 0, @IdentityMatrix[0][0]);
NewtonUserJointAddAngularRow(userJoint, 0, @IdentityMatrix[1][0]);
NewtonUserJointAddAngularRow(userJoint, 0, @IdentityMatrix[2][0]);


That code makes me think that it either simply prohibits any rotation for the bodies, or does nothing.
Have you tried: Is it still possible to rotate the bodies? Is their orientation linked to each other?

Julios example uses 3 other linear constraints to link the bodies orientations at the end of his example.
It would be possible to achieve the same result with 2 angular constraints. But you'd need to find axis and angle of rotation, which is numercally instable in case of a satisfied situation.
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: Stiff Joint

Postby carli2 » Tue May 29, 2012 12:16 pm

JoeJ wrote:
carli2 wrote: NewtonUserJointAddAngularRow(userJoint, 0, @IdentityMatrix[0][0]);
NewtonUserJointAddAngularRow(userJoint, 0, @IdentityMatrix[1][0]);
NewtonUserJointAddAngularRow(userJoint, 0, @IdentityMatrix[2][0]);


That code makes me think that it either simply prohibits any rotation for the bodies, or does nothing.
Have you tried: Is it still possible to rotate the bodies? Is their orientation linked to each other?

Julios example uses 3 other linear constraints to link the bodies orientations at the end of his example.
It would be possible to achieve the same result with 2 angular constraints. But you'd need to find axis and angle of rotation, which is numercally instable in case of a satisfied situation.


I can rotate and move around three linked objects as they were one single object.
carli2
 
Posts: 157
Joined: Thu Nov 10, 2011 1:53 pm

Re: Stiff Joint

Postby JoeJ » Tue May 29, 2012 1:14 pm

I can rotate and move around three linked objects as they were one single object.


Tried out myself - you're right.
But there is another problem with it:
If a large force hits the bodies (car crash, collisions...), their relative rotation can get distorted.
Julios method would try to fix that distortion, while your method tries to keep the distortion.
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: Stiff Joint

Postby carli2 » Tue May 29, 2012 2:09 pm

JoeJ wrote:
I can rotate and move around three linked objects as they were one single object.


Tried out myself - you're right.
But there is another problem with it:
If a large force hits the bodies (car crash, collisions...), their relative rotation can get distorted.
Julios method would try to fix that distortion, while your method tries to keep the distortion.


No,
I always fix that dissortion with the relative position matrix.
But there seems to be an other problem: the rotation is submitted as "relative rotation", so the bodyies can only keep their relative rotation. I should test if I find a solution. The relative position matrix already contains the information about the rotation, so I have to * it out.
carli2
 
Posts: 157
Joined: Thu Nov 10, 2011 1:53 pm

Re: Stiff Joint

Postby Julio Jerez » Tue May 29, 2012 2:29 pm

His method will work, however it will not recover from any deformation.
I remember that this is similar to what we were talking about in another thread.
Basically it was relate to how to make a joint when you can control the three Euler angular impended from each other.
NewtonUserJointAddAngularRow(userJoint, 0, @IdentityMatrix[0][0]);
NewtonUserJointAddAngularRow(userJoint, 0, @IdentityMatrix[1][0]);
NewtonUserJointAddAngularRow(userJoint, 0, @IdentityMatrix[2][0]);
These functions are telling the engine to calculate three independent torques, one alone each axis than when applied to the body will drive to zero the relative angle between the two bodies along that axis.

Since he is passing 0 as relive angle the joint will calculate torque to zero out the toque and angulated velocity, but if they relative angle was already different that zero , the joint will never recover and will drift apart over time.
The way to solve this is like this.
-Calculate the relay matrix between the two bodies.
-Decompose the matrix into three Euler angles

These angles have the property that the do not commute, but we said that the solver will calculate three independent torques that can be applied in any order.
Here the laws of algebra throw us a curve ball, and they are telling us that we can no solve the problem in one step.

We however can use Calculators of variation, which basically state that we will happens to a system if you apply a small perturbation keeping everything else constant.

So using this will can break our three Euler angle in an array of small angles.

Say Pitch is 30 degree, Yaw is 45, and Roll is 85.

-We will take angular step, say 2 degrees.
-We find the largest angular error which in this case is 85 degrees.

We will set u evert thin so that the maximum rotation is no largest the 2 degrees,
this mean we must calculate the percent of 2 degree for the other two angles. That is
RelativePitch = 30 * 2 / 85
RelativeYaw = 45 * 2 / 85
RelativeRoll = 85 * 2 / 85

Now we pass these constraints

NewtonUserJointAddAngularRow(userJoint, RelativePitch, @IdentityMatrix[0][0]);
NewtonUserJointAddAngularRow(userJoint, RelativeYaw, @IdentityMatrix[1][0]);
NewtonUserJointAddAngularRow(userJoint, RelativeRoll, @IdentityMatrix[2][0]);

The joint will not try to recover the complete angle instead it try to recover a small fraction of it.
Why this work? This works because you can prove that in the limit, infinitesimal rotations are Commutative,
which are the criteria that the solver demand to work correctly.

If you do it correctly, you can get an awesome fix joint and a power, teh preble is that it can no be unconditinally solid.
But remember and uncoditinally ridid joint does not exist, in real like you deal with materail deformation,
in mathematic we must deal with numerical error. this is because the infinitlly ridid body is an idealization that does not exist.

when I get back to the Joints, I will make the power Ragdoll use this method.
I remember I mention this to you a few year ago.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

PreviousNext

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 5 guests