CJ 2D Joint planar rotation

From Newton Wiki
Jump to: navigation, search

The previous 2D joint restricted position but rotation was free. The version on this page restricts the object so it may only rotate in the 2D plane (around the plane normal).


The construction code is the same:

Joint2D::Joint2D(NewtonBody* kpBody0, dVector kPlaneNormal)

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

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


The constraint code begins the same but we add another linear constraint. This fixes a point the line through the object's origin along the plane normal, to a 2D plane that was parallel to the first. This second 2D plane is positioned away from the first such that the point is always in its desired position. The result of all of this is that rotations around the axes perpendicular to the plane normal are prevented because they would take the point out of its plane.

Example C++ code:

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

	// this line clamps the origin to the plane
	NewtonUserJointAddLinearRow (mpJoint, &matrix0.m_posit[0], &mPlaneOrigin[0], &mPlaneNormal[0]);

	// we can prevent rotation that takes any points out of the plane by clamping a point on the
	// object that is on the line through the origin of the object with the same vector as the
	// plane normal.  The clamp is to another plane that is parallel to the first, offset by the
	// plane normal vector.  Rotations around either of the axes orthogonal to the plane normal
	// will be prevented because they take the object point off that parallel plane.

	dVector object_point;
	dVector world_point;

	NewtonUserJointAddLinearRow (mpJoint, &object_point[0], &world_point[0], &mPlaneNormal[0]);


As before, 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.

Rotation is still allowed in the plane. The next two sections explain how to remove this.