Accomplishing springs in NxOgre?

Elspin

04-05-2011 04:53:18

I've been trying for a while now to get springs working with very little luck. In an old thread I was pointed to the "RevoluteJoint" - and though I'm still not entire sure why I'm supposed to use a hinge as a spring, I've finally got it set up as a very simple hinge in my program thanks to some helpful old posts by betajaen and others. So that is my question - how can I set up two objects with a spring constraint between them? I don't want them to swing around or rotate - which I'm not sure how to disable while keeping the spring. Currently I have my system set up like this:



Critter::KinematicBody *mHolder = mRenderSystem->createKinematicBody( NxOgre::BoxDescription(1,1,1), NxOgre::Vec3( 0, 12, 0 ), "KCube.mesh", kbodyDescription);

Critter::Body *mBody = mRenderSystem->createBody(NxOgre::BoxDescription(1,1,1), NxOgre::Vec3(0,6,0), "Cube.mesh", bodyDescription);

NxOgre::RevoluteJointDescription jointDescript;
jointDescript.reset();

jointDescript.mLocalAnchor[0] = NxOgre::Vec3( -.5, 0.5, 0 );

jointDescript.mLocalAnchor[1] = NxOgre::Vec3( 0, 4, 0 );


jointDescript.mLocalAxis[0] = NxOgre::Vec3(0,0,1);
jointDescript.mLocalAxis[1] = NxOgre::Vec3(0,0,1);


NxOgre::RevoluteJoint* basicJoint = mScene->createRevoluteJoint( mHolder, mBody, jointDescript );

NxOgre::SpringDescription springDescript;
springDescript.mDamper = 0;
springDescript.mSpring = 1;
springDescript.mTargetValue = 0;

basicJoint->setSpring( springDescript );


So basically - it's just a hinge on a slight angle with the boxes weighing 1kg each, the angle and axis of the hinge has gone through a few iterations just to see what would happen, but I can't get much of a reaction out of the spring part of the system. I'd really appreciate any help here

betajaen

04-05-2011 06:37:30

Revolute is for motors and objects that revolve around each other. If you want them limited in one direction, have a look at the other type of joints; prismatic I think.

They are described quite well in the PhysX SDK documentation file (PhysX Documentation in your start menu), most of the joints are in NxOgre, and essentially the names and behavior are the same (apart from dropping the Nx prefix).

Elspin

04-05-2011 07:47:51

Revolute is for motors and objects that revolve around each other. If you want them limited in one direction, have a look at the other type of joints; prismatic I think.

They are described quite well in the PhysX SDK documentation file (PhysX Documentation in your start menu), most of the joints are in NxOgre, and essentially the names and behavior are the same (apart from dropping the Nx prefix).


Yeah, I've been reading through the PhysX SDK a lot and I've found that NxDistanceJoint looks like what I want - a constraint that limits distance and can have a spring attached to it, though I'm still a bit shaky on how to apply the spring and I'll have to use some barebones PhysX code as it's not yet supported by NxOgre. I looked up the Prismatic Joint you mentioned and it looks like what I want in the sense of its movements, but has no mention of attaching a spring description to it :(

betajaen

04-05-2011 08:14:31

You can always use a D6 Joint if you need absolute control, and it's well supported in NxOgre, and there are a few examples how to make the other type of joints in it.

Elspin

04-05-2011 19:22:07

You can always use a D6 Joint if you need absolute control, and it's well supported in NxOgre, and there are a few examples how to make the other type of joints in it.

Thanks betajaen, I will definitely look into that! I have a spring working now using the NxDistanceJoint - and since it appears to be a commonly requested how to I'll post the full code here :D


Critter::BodyDescription bodyDescription;
bodyDescription.mMass = 1.0f;

Critter::BodyDescription kbodyDescription;
bodyDescription.mMass = 1.0f;

Critter::KinematicBody *mHolder = mRenderSystem->createKinematicBody( NxOgre::BoxDescription(1,1,1), NxOgre::Vec3( 0, 22, 0 ), "KCube.mesh", kbodyDescription);

Critter::Body *mBody = mRenderSystem->createBody(NxOgre::BoxDescription(1,1,1), NxOgre::Vec3(0,18,0), "Cube.mesh", bodyDescription);

Critter::Body *mFreeBody = mRenderSystem->createBody(NxOgre::BoxDescription(1,1,1), NxOgre::Vec3(-10,10,0), "Cube.mesh", bodyDescription);


NxDistanceJointDesc jDescription;

jDescription.actor[0] = mHolder->getNxActor();
jDescription.actor[1] = mBody->getNxActor();

jDescription.localAnchor[0] = NxVec3( 0, -0.5, 0 );
jDescription.localAnchor[1] = NxVec3( 0, 0.5, 0 );

jDescription.maxDistance = 1.0f;
jDescription.minDistance = 9.0f;

NxSpringDesc springDescript;
springDescript.damper = 0;
springDescript.spring = 2;
springDescript.targetValue = 5;

jDescription.spring = springDescript;

jDescription.flags = NX_DJF_MIN_DISTANCE_ENABLED | NX_DJF_MAX_DISTANCE_ENABLED | NX_DJF_SPRING_ENABLED;

NxDistanceJoint *springJoint = (NxDistanceJoint*)mScene->getScene()->createJoint(jDescription);