[Solved] addForce seems to do nothing...

xius

15-04-2009 21:56:13

Hello, I've been trying to work with Ogre + PhysX, but I'm stuck here. Seems to me that there is missing something important, because it should move a cube when it is added force, right?


struct Atom
{
NxActor * actor;
SceneNode * node;
};

inline Vector3 toOgre(const NxVec3 &v) {
return Vector3(v.x, v.y, v.z);
}

inline NxVec3 toNx(const Vector3 &v) {
return NxVec3(v.x, v.y, v.z);
}

inline Quaternion toOgre(const NxQuat &q) {
return Quaternion(q.w, q.x, q.y, q.z);
}

inline NxQuat toNx(Quaternion& q, bool _normalise = true) {
if (_normalise) q.normalise();
NxQuat a; a.x = q.x; a.y = q.y; a.z = q.z; a.w = q.w;
return a;
}
...
OgrePhysXFrameListener (RenderWindow * win, Camera * cam, SceneManager * sceneMgr): ExampleFrameListener (win, cam), mSceneMgr (sceneMgr)
{
initializePhysX();
createPhysicScene();
createBox (Vector3 (0,50,0));

mCamNode = cam->getParentSceneNode();
mSceneMgr = sceneMgr;

force = 20000;
}

bool processUnbufferedKeyInput(const FrameEvent& evt)
{
return true;
}

void initializePhysX()
{
PxSDK = NxCreatePhysicsSDK (NX_PHYSICS_SDK_VERSION, NULL, NULL);
}

void createPhysicScene ()
{
NxSceneDesc sceneDesc;
sceneDesc.gravity.set (0,-9.8,0);
sceneDesc.groundPlane = true;
PxScene = PxSDK-> createScene (sceneDesc);

NxMaterial* defaultMaterial = PxScene->getMaterialFromIndex(0);
defaultMaterial->setRestitution(0.5f); //! 0.5
defaultMaterial->setStaticFriction(0.5f); //! 0.5
defaultMaterial->setDynamicFriction(0.5f); //! 0.5

Plane plane(Vector3::UNIT_Y, 0);
MeshManager::getSingleton().createPlane("planemesh", "planusGroup", plane, 1000, 1000, 32, 32, true, 1, 5, 5, Vector3:: UNIT_Z);
Entity * pl = mSceneMgr-> createEntity ( "plane", "planemesh");
pl->setMaterialName("Examples/Rockwall");
SceneNode * node = mSceneMgr-> getRootSceneNode () -> createChildSceneNode ();
node-> attachObject (pl);
}

void updateAllAtoms (NxReal time)
{
// update the physical scene
PxScene-> simulate (time * 10.0f);
PxScene-> flushStream ();
PxScene-> fetchResults (NX_ALL_FINISHED, true);
for (unsigned int i = 0; i <atoms.size (); i++)
{
atoms[i].node->setPosition(toOgre(atoms[i].actor->getGlobalPosition()));
atoms[i].node->setOrientation(toOgre(atoms[i].actor->getGlobalOrientationQuat()));
}
}

void ApplyForceToActor(NxActor* actor, const NxVec3& forceDir, NxReal time)
{
NxVec3 forceVec = force*forceDir*time;
actor->addForce(forceVec);
}

void createBox (Vector3 pos)
{
Atom BoxAtom;
NxActorDesc actorDesc;
NxBodyDesc bodyDesc;
NxBoxShapeDesc boxDesc;
boxDesc.dimensions.set(10,10,10);
actorDesc.shapes.pushBack(& boxDesc);
actorDesc.body = &bodyDesc;
actorDesc.density = 100;
actorDesc.globalPose.t = toNx(pos);
BoxAtom.actor = PxScene-> createActor(actorDesc);

Entity * e = mSceneMgr-> createEntity("go" + StringConverter::toString(AtomID++), "f_box.mesh");
BoxAtom.node = mSceneMgr-> getRootSceneNode() -> createChildSceneNode();
BoxAtom.node-> attachObject(e);
BoxAtom.node-> scale(10,10,10);
BoxAtom.node-> setPosition(pos);
atoms.push_back(BoxAtom);
}

bool frameStarted(const FrameEvent &evt)
{
atoms[0].actor->addForce(NxVec3(0,0,40000)*evt.timeSinceLastFrame);
mMouse->capture();
mKeyboard->capture();

if(mKeyboard->isKeyDown(OIS::KC_ESCAPE))
return false;

if (mKeyboard->isKeyDown(OIS::KC_W))
ApplyForceToActor(atoms[0].actor, NxVec3(0,0,1), evt.timeSinceLastFrame); // nothing happens
if (mKeyboard->isKeyDown(OIS::KC_S))
ApplyForceToActor(atoms[0].actor, NxVec3(0,0,-1), evt.timeSinceLastFrame); // nothing happens
if (mKeyboard->isKeyDown(OIS::KC_LEFT) || mKeyboard->isKeyDown(OIS::KC_A))
ApplyForceToActor(atoms[0].actor, NxVec3(1,0,0), evt.timeSinceLastFrame); // nothing happens
if (mKeyboard->isKeyDown(OIS::KC_RIGHT) || mKeyboard->isKeyDown(OIS::KC_D))
ApplyForceToActor(atoms[0].actor, NxVec3(-1,0,0), evt.timeSinceLastFrame); // nothing happens

updateAllAtoms(evt.timeSinceLastFrame);
bool ret = ExampleFrameListener::frameStarted(evt);
return ret;
}


I know about NxOgre being out there :), but why this isn't working? I tried to shoot cubes and everything moves right. Just when I try to move something by addForce, eh, nothing happens. (also, experimented with NX_FORCE, NX_IMPULSE... and nothing)
Thanks for advice.

betajaen

15-04-2009 22:18:01

It's a combination of the force being to small and the cube having to much mass. Increase one or reduce the other.

xius

15-04-2009 22:37:51

AARGH! :oops: Ridiculous mistake! Shame on me...

Thanks a lot!