# Problem with PhysX Candy Wrapper

IvanJ147

20-02-2012 14:57:27

Hi all,
I don't know if this is a right question for this forum, so if I'm wrong I apologize.
I'm using PhysX candy wrapper with mogre; in my scene there are only a table, an hand and some objects on the table. I manage hand motion with a inertial sensor.
The purpose of the scene is to take the objects and position them on a specific sector of table.
The problem is that I can't manage in correct way the object I take. I tried in 2 ways:
1- When I take it, I set its Kinematic parameter to True. Now, everytime I move the hand I use this instruction:
``` takenObj.MoveGlobalPosition(mSceneMgr.GetSceneNode("Hand").Position) ```
In this way I notice the problem that you see in this video:
Seems that the under object is subjecting of a huge force.

2- I calculate the instantaneous velocity of hand motion, and I set to taken object this velocity. In this case I haven't the problem of previous case, but another: the object doesn't follow perfectly the hand.
``` dim velHand a mogre.vector3 velHand.x = (posCurrX - o.mSceneMgr.GetSceneNode("hand").Position.x) / timeStep velHand.y = (posCurrY - o.mSceneMgr.GetSceneNode("hand").Position.y) / timeStep velHand.z = (posCurrZ - o.mSceneMgr.GetSceneNode("hand").Position.z) / timeStep takenObj.LinearVelocity = velHand ```

Can you say me how to use correctly one of two ways? Or say me another way to manage taken objects, if these are not the best.

zarfius

21-02-2012 10:59:05

This is probably not what you want to hear but this is normal behaviour of most physics engines. The problem is that physics engines provide 2 polar opposite things and you need something in between.

Kinematics :- the branch of mechanics concerned with motion without reference to force or mass.
Dynamics :- The branch of mechanics concerned with the motion of bodies under the action of forces.

Both the approaches you've tried are perfectly acceptable, unfortunately, neither of them are easy to get working correctly. This problem usually comes up when you need a character controller and the most common way to solve it is to implement a kinematic body with lots of extra special case code to handle different situations (walking up stairs, ladders, jumping, picking up objects, etc).

PhysX is really good at dynamics, but in my experience I found the character controller to be lacking. These days I use BEPU physics instead. The character controller that comes with it is almost perfect out of the box. I realise this won't solve your problem exactly, but hopefully will lead you down the right track.

Based on your video I would work on the kinematic approach and handle the situation when the two objects collide and at that exact point position them correctly and turn them back into dynamic objects (or make the hand let go).

IvanJ147

21-02-2012 15:51:02

I'd like to try BEPU, but unfortunately I have very little time to finish my project, so I'm afraid that I will have to close it with PhysX. For now.
I tried to switch on/off kinematic parameter after a collision, but it seems to make scene very unstable.
Maybe do you know why my taken object doesn't follow correctly the hand?
I use a stopwatch to take my FPS locked on 25FPS, then every frame I calculate velocity in this way:
``` velHand.x = (CurrPosX - mSceneMgr.GetEntity("Hand").Skeleton.GetBone("joint1").Position.x) / (1 / 25) velhand.y = (CurrPosY - mSceneMgr.GetEntity("Hand").Skeleton.GetBone("joint1").Position.y) / (1 / 25) velHand.z = (CurrPosZ - mSceneMgr.GetEntity("Hand").Skeleton.GetBone("joint1").Position.z) / (1 / 25) ```
..and then, always in every frame, I update physx in this way:
``` objectTaken.LinearVelocity = velHand scenePhys.Simulate(1 / 25) scenePhys.FetchResults(SimulationStatuses.AllFinished, True) ```
If the object would follow the hand in correct way it would be perfect, because in this way the collisions are managed correctly...
Thanks again...

Pyritie

21-02-2012 19:57:41

The guy who runs the physx candy wrapper, smoove or something, is very helpful - maybe try sending him an email at smoove at users dot sourceforge dot net

zarfius

22-02-2012 03:42:04

Normally you would set the velocity of the object once and let the physics engine move the object over time adding in gravity and any other velocity changes from collisions. When you call Simulate I believe it actually performs more than one frame internally to keep the physics engine accurate and stable (accounting for floating point errors for example). Therefore, setting the velocity of an object every frame is not going have the desired behaviour (as you already know).

Rather than blindly setting the velocity of the object to the velocity of the hand you might be able to caclulate the desired velocity of the object from thier relative postions. The idea being that as the object starts to move away from the hand you will set it's velocity in the opposite direction.

Another thought is that setting the velocity explicitly will be like applying a strong force when the objects collide. You'll effectively be ignoring the mass of the objects. To correct this you might have to instead caclulate the force to apply to the object to achieve the desired velocity. I'm sure the formula for this is easily found on the internet somewhere.

I strongly suggest you do some reading on character controllers because your situation is closely related. There are not many great articles on the topic, but here's one I found that explains the differences between dynamic and kinematic approaches.
http://www.digitalrune.com/Support/Blog ... llers.aspx

IvanJ147

24-02-2012 17:00:16

Thank you Zarfirius and Pyritie!!
I'll try to write to Smoove, and I'll read the article about character controllers.
I'll make you know if I'll find a solution.
Thanks again.