Dragging a body around

Night Elf

21-02-2008 20:50:54

I want to be able to pick up a body lying on the ground with the mouse, then move it freely, then release it. I want the body's movement to be very precise while I'm dragging (so I want it to go exactly where the mouse says), but also have it collide with other bodies in the scene.

I've tried implementing this with Body::moveTowards(), and it works quite right, only the movement is not precise and the body tends to orbitate the mouse position, rather than stick to it.

What I would prefer is, I think, that the object behaves much like a kinematic object while I'm dragging it, but using moveGlobalPosition doesn't respond to collisions, so the body goes right through every obstacle.

Any ideas how I could implement something like this?

OgreMage

12-05-2008 06:15:03

I have been trying to do this exact same thing.

I have resorted to making the Head of the body KINEMATIC so that I can drag him however I do not get the desired effect because it goes right through objects, of course.

FROZEN also does not work well using setGlobalPosition, similar issue.

Basically to be done right it appears addForce has to be used, but what I do not understand is how to calculate addForce to cover an exact distance, i.e. keep up with the mouse.

For example, if I want to move the character 1m, how much force to apply?

I have come across other threads in this forum that talk about the fact to move objects, use addForce and not moveTo or setGlobalPos, of course. But if you want to move an object an exact distance, how much force do you apply? Especially in the case of a ragdoll which has multiple actors etc.

Respectfully,
OM

betajaen

12-05-2008 10:42:57

@Night Elf

I've seen some PhysX demos using a fixed joint (attached to the Actor and Scene), which the global position is moved around. That seems to work, but looks a little odd.

@OgreMage

It should be calculated via F=MA, Mass being the mass of your actor - obviously, and A being 1.


@Both of you

"moveTo" is my invention. It's not part of the PhysX SDK, and it's not that well written. I would avoid using it, in anything apart from Cake.

Characters work well for moving to exact positions, but I've found with enough damping and direct control you can move a normal actor around to.

Observe, the SmartCamera code from Cake:

ActorParams actorParams;
actorParams.setToDefault();
actorParams.mMass = 1;
actorParams.mDensity = 0;
actorParams.mLinearDamping = 10;
actorParams.mAngularDamping = 10;
actorParams.mBodyFlags |= NX_BF_DISABLE_GRAVITY;
actorParams.mBodyFlags |= NX_BF_FROZEN_ROT;

NxOgre::Pose actorPose(
Vector3(0,15,22)
);

mActor = scene->createBody("CakeSmartCamera", new NxOgre::Sphere(0.25f), actorPose, renderableParams, actorParams);


To move it, I use any of these:

void translateVert(NxReal vTrans) {
Ogre::Vector3 trans = mCamera->getOrientation() * Ogre::Vector3(0, vTrans, 0);
mActor->addForce(trans, NX_IMPULSE, true);
}

void translate(NxReal rtrans) {
Ogre::Vector3 trans = mCamera->getOrientation() * Ogre::Vector3(0, 0, rtrans);
mActor->addForce(trans, NX_IMPULSE, true);
}

void translateHorz(NxReal hTrans) {
Ogre::Vector3 trans = mCamera->getOrientation() * Ogre::Vector3(hTrans, 0, 0);
mActor->addForce(trans, NX_IMPULSE, true);
}


You could use DominanceGroups along with this code, so that the Actor has a one way collision with other smaller Actors, and use proper collision with some of the more massive ones.