CJ OgreNewt Getting Started

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

I've struggled with using custom joints with OgreNewt. Here is a simple class that will constrain a body to the xz plane. This section assumes you are able to use Ogre, OgreNewt, and the built in joints but are struggling with custom joints in OgreNewt.

header file

#ifndef SIMPLE_JOINT_H
#define SIMPLE_JOINT_H
#include <OgreNewt.h>

class SimpleJoint :  public OgreNewt::CustomJoint
{
public:
	SimpleJoint( OgreNewt::Body* child); 

	~SimpleJoint(); 

	// also user MUST implement this function to actually apply the constraint.
	void submitConstraint();
};
#endif


source file

#ifndef SIMPLE_JOINT_CPP
#define SIMPLE_JOINT_CPP
#include "SimpleJoint.h"

SimpleJoint ::SimpleJoint ( OgreNewt::Body* child) : OgreNewt::CustomJoint( 1, child, NULL )
{
} 


SimpleJoint ::~SimpleJoint ()
{
}


// the important function that applies the joint.
void SimpleJoint::submitConstraint()
{
	Vector3 v3pos;
	Quaternion quat;
	this->m_body0->getPositionOrientation(v3pos, quat); 

	//drive the body to 0,0,0 along the Y direction.
	addLinearRow( v3pos, Vector3(0,0,0), Ogre::Vector3(Ogre::Vector3::UNIT_Y) );
}
  1. endif

Common Mistakes

I inadvertantly wrote :

addLinearRow(Vector3(0,0,0), v3pos, Ogre::Vector3(Ogre::Vector3::UNIT_Y) );

which did not work because v3pos refers to body0. The first argument refers to the first body, and the second argument refers to the second body.

Additionally, when adding more constraints ensure that the num_constraints parameter shown below is set appropriately. If it is set to 1 for example and you have 3 constraints, no errors are reported, and it simply ignored the other two constraints. Quite frustrating when trying to figure out why simple constraints were no longer working.

SimpleJoint ::SimpleJoint ( OgreNewt::Body* child) : OgreNewt::CustomJoint( num_constraints, child, NULL )