## Spline joint.

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

### Spline joint.

I would like to create a joint that would act like a slider but the attached bodies would move along a curved path. But I dont really know how to set about it. I suspect that it would involve changing the pin direction depending on the bodies position on the path.

Has anyone done anything similar to this yet? Could you tell me how you did it or perhaps show me the constraint code?

Chris
DarthGak

Posts: 25
Joined: Sat Nov 25, 2006 4:55 pm

### Re: Spline joint.

is this joint like a ball and socket or a free slider but with the difference that the pivot follows a spline path?
Julio Jerez
Moderator

Posts: 11154
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

### Re: Spline joint.

Exactly.
DarthGak

Posts: 25
Joined: Sat Nov 25, 2006 4:55 pm

### Re: Spline joint.

Ok I can post code for the joint here for you.
will it be OK if I take the code for the slider and adpate to do what you want, are you using those joints?

Tell me does the joint need to hold the objec like a hinged along the path of can it rotate around a pivot point?
The diffrent is that if is is liek a hinge it iwll act liek a cart in a mono rail, if i n can roitate wil will be like a thos bad roller coster when peopel seat on a musrun shape obje tthat kang forom the rail.
I guess I cna mak eteh mode comlex one and you can make the other if you want.
Julio Jerez
Moderator

Posts: 11154
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

### Re: Spline joint.

Any of those options would be great. I think once I have the basics I can figure out the rest.

Thanks Julio!
Chris
DarthGak

Posts: 25
Joined: Sat Nov 25, 2006 4:55 pm

### Re: Spline joint.

Ok here is the basic framwork for a Path follow joint
Header
Code: Select all
`class JOINTLIBRARY_API CustomPathFollow: public NewtonCustomJoint  {   public:   CustomPathFollow (const dMatrix& pinsAndPivoFrame, NewtonBody* body);   virtual ~CustomPathFollow();   virtual dMatrix EvalueCurve (const dVector& posit);   protected:   virtual void SubmitConstrainst (dFloat timestep, int threadIndex);   virtual void GetInfo (NewtonJointRecord* info) const;   dMatrix m_localMatrix0;};`

code

Code: Select all
`#define MIN_JOINT_PIN_LENGTH   50.0fCustomPathFollow::CustomPathFollow (const dMatrix& pinAndPivotFrame, NewtonBody* child)   :NewtonCustomJoint(6, child, NULL){   // calculate the two local matrix of the pivot point   dMatrix tmp;   CalculateLocalMatrix (pinAndPivotFrame, m_localMatrix0, tmp);}CustomPathFollow::~CustomPathFollow(){}void CustomPathFollow::GetInfo (NewtonJointRecord* info) const{}// caluate the closest point from the spline to point pointdMatrix CustomPathFollow::EvalueCurve (const dVector& posit){   dMatrix matrix;   // as demonstraction I will use a starion line for path   dVector lineSlope (1.0f, 0.0f, 0.0f, 0.0f);   dVector lineOrigin (0.0f, 10.0f, 0.0f, 0.0);   // calculate distacne for point to list   matrix.m_posit = lineOrigin + lineSlope.Scale ((posit - lineOrigin) % lineSlope);   matrix.m_posit.m_w = 1.0f;   //the tangent of the path is the line slope, passes in the forst matrix dir   matrix.m_front = lineSlope;   // the normal will be such tha is is hortizaontl to the teh floor and perpendicular to teh path    dVector normal (dVector (0.0f, 1.0f, 0.0f, 0.0f) * matrix.m_front);   matrix.m_up = normal.Scale (1.0f / dSqrt (normal % matrix.m_front));   // the binormal is just the cross product;   matrix.m_right = matrix.m_front * matrix.m_up;   return matrix;}void CustomPathFollow::SubmitConstrainst (dFloat timestep, int threadIndex){   dMatrix matrix0;         // Get the global matrices of each rigid body.   NewtonBodyGetMatrix(m_body0, &matrix0[0][0]);   matrix0 = m_localMatrix0 * matrix0;   dMatrix matrix1 (EvalueCurve (matrix0.m_posit));   // Restrict the movement on the pivot point along all tree teh normal and binormal of the path   const dVector& p0 = matrix0.m_posit;   const dVector& p1 = matrix1.m_posit;   NewtonUserJointAddLinearRow (m_joint, &p0[0], &p1[0], &matrix0.m_up[0]);   NewtonUserJointAddLinearRow (m_joint, &p0[0], &p1[0], &matrix0.m_right[0]);      // get a point along the ping axis at some reasonable large distance from the pivot   dVector q0 (p0 + matrix0.m_front.Scale(MIN_JOINT_PIN_LENGTH));   dVector q1 (p1 + matrix1.m_front.Scale(MIN_JOINT_PIN_LENGTH));   // two more constraints rows to inforce the normal and binormal     NewtonUserJointAddLinearRow (m_joint, &q0[0], &q1[0], &matrix0.m_up[0]);   NewtonUserJointAddLinearRow (m_joint, &q0[0], &q1[0], &matrix0.m_right[0]);//   Julio: at this point the path follow will maintaing the pivot fixed to the path and will allow it to slide freelly//   it will also spin around the path tangent, if more control is needed then more constraion rows need to be submited //   for example friction along the oath tangent be added to make more realistic behavior//  or another point constraint along the binormal to make it no spin around the path tangent/*   // example of ading friction   dVector veloc0;    dVector omega0;    NewtonBodyGetVelocity(m_body0, &veloc0[0]);   NewtonBodyGetOmega(m_body0, &omega0[0]);   veloc0 += omega0 * (matrix0.m_posit - p0);   dFloat relAccel;    relAccel = - (veloc0 % matrix0.m_front) / timestep;   #define MaxFriction 10.0f   NewtonUserJointAddLinearRow (m_joint, &p0[0], &p0[0], &matrix0.m_front[0]);   NewtonUserJointSetRowAcceleration (m_joint, relAccel);   NewtonUserJointSetRowMinimumFriction (m_joint, -MaxFriction);   NewtonUserJointSetRowMaximumFriction(m_joint, MaxFriction);*/}`

It is very simple but I thonk it is funtional, I only compilerd bu I did not tested,
I derive form oteh existing joint so I thong it a very good change it is funtional
Please l;et me knwo if it works, or it is whet you wanted.

The path is definr by a virtual function, you can jus tchnge the funtion to do whant you want.
I used a very simple fix straight line as path.
I hope is works.

Note:
The joint is very basic it only uses the geometry of the problem, but I think it will wolk fine fo slow or moving vody on spline with decent or smooth curvatures.
It may drift a lithe of sharp curves because for that the constraints need to know the spline curvature to calculte the centripetal force to add based on the partical derivatives
of the pivot velocity along the spline normal and binormal.
At that point if gets a litle more complicated but fist the us try this and see how it goes.
Julio Jerez
Moderator

Posts: 11154
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

### Re: Spline joint.

Thats exactly what I was looking for. And it looks a lot simpler that I would have expected.

But its not working quite right yet. If I am reading the code right, I would expect an body attached to the joint to fall (under gravity) from 10m to 0m in along a shallow line. But when I use it the body moves as described but like its under rocket power. Something is pushing in along the line.

I'll poke at it and see if I can fix it. But any ideas?

Thanks Julio!
Last edited by DarthGak on Sat Sep 20, 2008 5:01 pm, edited 1 time in total.
DarthGak

Posts: 25
Joined: Sat Nov 25, 2006 4:55 pm

### Re: Spline joint.

well I did not tested, se if you can find where teh bug is, if no I can ste ul a test a see what it is.
Julio Jerez
Moderator

Posts: 11154
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

### Re: Spline joint.

I think I found the problem. It was a combination of scaling (again) and me not understanding how the joint would work.

Once I fixed the scaling I realized that this constraint will pull the _center_ of the body to a absolute point in space. Everthing went flying off because the body wasn't exactly centered on the joint.

For my application I will have to introduce an offset to the body based on its location at run time. And make the path relative to a parent body. But I can figure all that out.

Thanks again Julio.
Chris
DarthGak

Posts: 25
Joined: Sat Nov 25, 2006 4:55 pm

### Re: Spline joint.

if you check carefully teh construtor o fteh joint
CustomPathFollow (const dMatrix& pinsAndPivoFrame, NewtonBody* body);

pinsAndPivoFrame is a full matrix.
the 4th row is the position of the pivot point that is contrained to the path.
if you set the pivot to the center of the body plus some offset, then it will do what you want.

can you check that and see if it works?
Julio Jerez
Moderator

Posts: 11154
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

### Re: Spline joint.

Yes! That works perfectly. And I should have known. Thats how I am doing the other joints, but for some reason I though this one was different.

Ok now to get it on a curved path. I think I know how to do that. I'll post the result here.
DarthGak

Posts: 25
Joined: Sat Nov 25, 2006 4:55 pm

### Re: Spline joint.

Excellent, when you get goint, if possible post a video, I am currisu as to waht you are doing.
Julio Jerez
Moderator

Posts: 11154
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

### Re: Spline joint.

Will do. And I am curious too. I am just making it because it sounds useful. Somethings I think it could do are moving platforms, train tracks, conveyor belts, slot car tracks, roller coasters, cars that drive on paths etc.
DarthGak

Posts: 25
Joined: Sat Nov 25, 2006 4:55 pm

Return to General Discussion

### Who is online

Users browsing this forum: No registered users and 4 guests