CJ Simple 2D Joint

From Newton Wiki
Jump to: navigation, search

The simplest custom joint is a 2D joint with the world. This constrains the child object to only move in a plane although it is free to rotate around any of its axes.

Construction

The joint will need to know the plane of permissable movement. The plane can be defined by its normal vector and an origin in space. A convenient way to define the origin is to simply use the current origin of the child object when the joint is created. Since the plane is not supposed to move it is easiest to use the world as the parent body (i.e. passing 0 or NULL).

So example C++ code may be:

Joint2D::Joint2D(NewtonBody* kpBody0, dVector kPlaneNormal)
{
	mpBody0=kpBody0;
	mPlaneNormal=kPlaneNormal;

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

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

SubmitConstraints

This joint is simply designed to simple drive to zero the relative differences between the child's origin and the plane's origin, along the normal to the plane. Since the parent object is NULL, only the object moves. It is effectively clamped to the plane.

So example C++ code may be:

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

	NewtonUserJointAddLinearRow (mpJoint, &matrix0.m_posit[0], &mPlaneOrigin[0], &mPlaneNormal[0]);
}

Usage

Say you want to limit to the 2D plane defined by the x and y axes (i.e. no movement along the z axis). The normal to this plane is (0,0,1) so that is what you pass to the constructor. Any plane is possible by defining the correct normal vector.

The object can still rotate freely around its origin. See the next section for a 2D joint with rotation limited to the plane.

Links