- Code: Select all
void SubmitConstraints(const NewtonJoint* me, dFloat timestep, int threadIndex);
void GetInfo (const NewtonJoint* me, NewtonJointRecord* info);
class myJoint
{
protected:
NewtonBody* mChild;
NewtonBody* mParent;
NewtonJoint* mJoint;
NewtonWorld* mWorld;
bool mNotCreated;
dMatrix mChildMatrix;
dMatrix mParentMatrix;
public:
myJoint(void){
mJoint=NULL;
mWorld=NULL;
mChild=NULL;
mParent=NULL;
}
void makeBallAndSocket(NewtonBody* Child, NewtonBody* Parent,
Vector3 pos=Vector3(0,0,0),Quaternion rot=Quaternion::IDENTITY)
{
Matrix3 m;
Vector3 xcol, ycol, zcol;
rot.ToRotationMatrix(m);
xcol = m.GetColumn(0);
ycol = m.GetColumn(1);
zcol = m.GetColumn(2);
dMatrix matrix(
dVector(xcol.x,ycol.x,zcol.x,0),
dVector(xcol.y,ycol.y,zcol.y,0),
dVector(xcol.z,ycol.z,zcol.z,0),
dVector(pos.x,pos.y,pos.z,1));
if(Child==NULL)return;
mChild=Child;
mParent=Parent;
mNotCreated=false;
CalculateLocalMatrix (matrix, mChildMatrix, mParentMatrix);
mWorld=NewtonBodyGetWorld(Child);
mJoint=NewtonConstraintCreateUserJoint(mWorld, 6, SubmitConstraints,
GetInfo, mChild, mParent);
NewtonJointSetUserData (mJoint, this);
}
void CalculateLocalMatrix (const dMatrix& pinsAndPivotFrame, dMatrix& localMatrix0,
dMatrix& localMatrix1) const
{
dMatrix matrix0;
// Get the global matrices of each rigid body.
NewtonBodyGetMatrix(mChild, &matrix0[0][0]);
dMatrix matrix1(GetIdentityMatrix());
if (mParent) {
NewtonBodyGetMatrix(mParent, &matrix1[0][0]);
}
localMatrix0 = pinsAndPivotFrame * matrix0.Inverse();
localMatrix1 = pinsAndPivotFrame * matrix1.Inverse();
}
void CalculateGlobalMatrix (dMatrix& localMatrix0, dMatrix& localMatrix1,
dMatrix& matrix0, dMatrix& matrix1)
{
if(mNotCreated)return;
dMatrix childMatrix;
// Get the global matrices of each rigid body.
NewtonBodyGetMatrix(mChild, &childMatrix[0][0]);
dMatrix parentMatrix (GetIdentityMatrix());
if (mParent) {
NewtonBodyGetMatrix(mParent, &parentMatrix[0][0]);
}
matrix0 = localMatrix0 * childMatrix;
matrix1 = localMatrix1 * parentMatrix;
}
void jointSubmitConstraints(dFloat timestep, int threadIndex)
{
if(mNotCreated)return;
//split these out for different joint types
//if(mJointType==JT_BALLANDSOCKET)BallAndSocketSubmitConstraints(dFloat timestep, int threadIndex);
dMatrix matrix0;
dMatrix matrix1;
// calculate the position of the pivot point and the jacobian direction vectors, in global space.
CalculateGlobalMatrix (mChildMatrix, mParentMatrix, matrix0, matrix1);
// Restrict the movement on the pivot point along all tree orthonormal direction
NewtonUserJointAddLinearRow (mJoint, &matrix0.m_posit[0], &matrix1.m_posit[0], &matrix0.m_front[0]);
NewtonUserJointAddLinearRow (mJoint, &matrix0.m_posit[0], &matrix1.m_posit[0], &matrix0.m_up[0]);
NewtonUserJointAddLinearRow (mJoint, &matrix0.m_posit[0], &matrix1.m_posit[0], &matrix0.m_right[0]);
}
};
void SubmitConstraints (const NewtonJoint* me, dFloat timestep, int threadIndex)
{
myJoint* joint = (myJoint*) NewtonJointGetUserData(me);
joint->jointSubmitConstraints(timestep, threadIndex);
}
void GetInfo (const NewtonJoint* me, NewtonJointRecord* info)
{
}
myJoint joint;
joint.makeBallAndSocket(body1,body2,Vector3(0.5,10.5,0),Quaternion::IDENTITY);
This code is pretty much bastardized to not use the joint library. It does however, use the core CustomJoint and the CustomBallAndSocket. Which basically means that it will restrict all linear movement, but allow all rotational movement. After all this, I found that it does have an affect on my simulation. Basically, I have 2 boxes stacked offset, and joined them in the middle. If this was correctly done, the ball joint should just simply allow the bodies to overlap or rotate through each other.
OK, my video representation of it:
I wanted to test it in 3 different states to see what the differences were. I found that each state reacted differently in the simulation. Both separate acted normally. When I linked them together, they fell through each other as if they didn't exist in each other's world. Finally I tested it with the Child linked to NULL. This should have prevented it from dropping all the way. Such was not the case, but oddly it gained more force coming down. Dunno how that happened.
Mind you, this was all a test of the jointSubmitConstraints function, and to see if the NewtonConstraintCreateUserJoint actually created a link and how it reacted in the world.
So in conclusion, I don't have a clue how the jointSubmitConstraints actually preforms the operation.