CJ Simple kinematic

From Newton Wiki
Jump to: navigation, search

Sometimes you may want to dictate the movement of an object instead of applying forces. This can be useful for movements that are complex to specify with forces but easy as formulae, or to force a heavy object such as a door to move and push away anything in its path. This is a kinematic approach instead of the usual dymanics approach. For this to work you need a way of specifying position at each iteration and let Newton work out the required forces. This can be done with a joint.


This is mostly the same as the 2D joints but now we have a position variable - mPosition which gets initialised with the object's position when this constructor is called:

JointKinematic::JointKinematic(IrrNewtonBody* kpBody0)

	dMatrix matrix0;
	NewtonBodyGetMatrix(mpBody0, &matrix0[0][0]);

	mpJoint=NewtonConstraintCreateUserJoint(mspWorld, 6, SubmitConstraints, mpBody0, 0);
	NewtonJointSetUserData (mpJoint, (void*) this);


Before each call to UpdateWorld() you need to specify the desired position of the object by calling this function:

void JointKinematic::SetPosition(dVector kPosition)


The constraint code simply fixes the position of the object in each axis according to mPosition:

void JointKinematic::LocalSubmitConstraints(const NewtonJoint* kpJoint)
	dMatrix matrix0;
	NewtonBodyGetMatrix(mpBody0, &matrix0[0][0]);

	dVector pin0(1.0,0.0,0.0);
	dVector pin1(0.0,1.0,0.0);
	dVector pin2(0.0,0.0,1.0);

	NewtonUserJointAddLinearRow (mpJoint, &matrix0.m_posit[0], &mPosition[0], &pin0[0]);
	NewtonUserJointAddLinearRow (mpJoint, &matrix0.m_posit[0], &mPosition[0], &pin1[0]);
	NewtonUserJointAddLinearRow (mpJoint, &matrix0.m_posit[0], &mPosition[0], &pin2[0]);


Simply specify a position with a call to SetPosition() before update world. Use whatever formular you want for the position.

Extensions to this joint could include controlling orientation of the object as well.