CJ Simple kinematic

From Newton Wiki
Revision as of 08:02, 10 June 2019 by WikiSysop (talk | contribs) (1 revision imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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.

Construction

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)
{
	mpBody0=kpBody0->GetBody();

	dMatrix matrix0;
	NewtonBodyGetMatrix(mpBody0, &matrix0[0][0]);
	mPosition=matrix0.m_posit;

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

Position

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

void JointKinematic::SetPosition(dVector kPosition)
{
	mPosition=kPosition;
}

SubmitConstraints

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]);
}

Usage

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.


Links