CJ 2D Joint planar rotation

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

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).

Construction

The construction code is the same:

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

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;

	object_point=matrix0.TransformVector(mPlaneNormal);
	world_point=mPlaneOrigin+mPlaneNormal;
	NewtonUserJointAddLinearRow (mpJoint, &object_point[0], &world_point[0], &mPlaneNormal[0]);
}

Usage

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.

Links