Problem to put physics in the arm bones

mbvital

04-05-2012 14:44:08

Hi,

I’m working with motion sensors, and I have a mesh with skeleton in my application.
I want to reflect my arm movements in a Virtual Human.
So I apply the orientation of the sensors to the bones of the arms.

However, I want to put physics in my mesh (ok, only on the arms at the moment).
At the beginning I applied physics to the mesh like this (code in C# for Mogre and MogreNewt):

Entity entHV = mSceneMgr.CreateEntity("Ent", "Victoria 4.2 QuickStart.mesh");
ConvexHull entity = new ConvexHull(world, entHV, 1, 1);
Body bodyEnt = new Body(world, entity, true);
bodyEnt.SetPositionOrientation(entHV.Position, entHV.Orientation);


But then I realized that wouldn't work very well since this would apply to the complete mesh and I would like to have something more close to each arm segment.
So I tried to create a Cylinder collision object and apply it to each segment, roughly like this:

Bone upArm = skeleton.GetBone("rShldr");
upArm.SetManuallyControlled(true);

colUpArm = new Cylinder(world, 7, 25, 10);
bd = new Body(world, colUpArm, true);
colUpArm.Dispose();
bd.SetPositionOrientation(upArm.Position, upArm.Orientation);


I thought that this could work but however, I see my physics object at the world origin instead of the correct location within the character's mesh. If I move the character's mesh to another location, the skeleton keeps in the same place, it doesn't move with the mesh.

My question is: does anyone has an idea on how i can apply physics to the bones of the arms?
Or does anyone has an alternative idea on how should I do this?

Thank you!

Tubulii

07-05-2012 20:22:57

Do you want to use it as a character controller? If yes, than it is not neccessary to do this. Create a capsule or Box around the player. In addition Bullet and Physx have speacial character controllers.

mbvital

08-05-2012 13:01:22

Eventually in the future I want a character controller but for now I only want physics in the arms, because I want to eventually have a form of interaction with the arms and was starting by trying to apply physics to the arms to prevent that I see the arms go through walls and other objects (but at the same time, if it is an object I wanted to see a reaction from the contact).

I had tried at the beginning to place a convex hull in the mesh and it’s ok. But I need more detail.

The problem is:
--> When I put the physics body in the bone, it appears in the local position of the skeleton.

I move my mesh to another place but the skeleton stays in the original position and does not follow the mesh. So, the physics body stays far from the mesh

Any idea of why this is happening?

Thank you for the answer

Tubulii

08-05-2012 15:38:52

Are you updating the physics objects (Position and orientation)? Make sure that you use global coordinates...

mbvital

08-05-2012 17:02:14

How can you get the global coordinates? I think I tried what you said, I used the function (in bold):

Bone upArm = skeleton.GetBone("rShldr");
Cylinder cylinder = new Cylinder(world, 7, 25, 10);
Body body = new Body(world, cylinder, true);
cylinder.Dispose();
body.SetPositionOrientation(upArm.ConvertLocalToWorldPosition(upArm.Position), upArm.ConvertLocalToWorldOrientation(upArm.Orientation));


and happens the same.
At this time I don’t need to update my physics object because I first want to put the physics in the mesh, and when I get this to work I will start working with the movement of the sensors.

To load my mesh I do this:

entHV = mSceneMgr.CreateEntity("entity", "HV.mesh");
nodeHV = mSceneMgr.RootSceneNode.CreateChildSceneNode("entity");
nodeHV.AttachObject(entHV);
nodeHV.Position = new Vector3(200, 0, -200);


I had the function entHV.DisplaySkeleton = true to see my skeleton, and the skeleton is in the (0,0,0) but the mesh is in the (200, 0, -200); I don’t know why this is happening.

Thank you for the answers

Tubulii

08-05-2012 17:44:32

Mm. Try a custom skeleton display -> http://www.ogre3d.org/forums/viewtopic.php?p=152821 (scroll down to find the source code, conversion should be easy). I tried DisplaySkeleton, too, and its also at 0,0,0. The entity is loaded correctly (as far i can see). Maybe its a bug or both you and me made a mistake somewhere ;) .

The material "BaseModel_Bones_Material" is just a material script with vertex colors:
Here are some example materials: Copy and paste them into a file in the "Media" folder and use the material names "Debug/Blue", "Debug/Green" or "Debug/Red" to color your skeleton.
material Debug/Blue
{
technique
{
pass
{
ambient 0.25 0.35 0.45
diffuse 0.25 0.35 0.45
specular 0.45 0.45 0.45 10
emissive 0 0 0
}
}
}

material Debug/Green
{
technique
{
pass
{
ambient 0.309804 0.443137 0.243137
diffuse 0.309804 0.443137 0.243137
specular 0.45 0.45 0.45 10
emissive 0 0 0
}
}
}

material Debug/Red
{
technique
{
pass
{
ambient 0.439216 0.0392157 0.0392157
diffuse 1 0 0
specular 0.22549 0.22549 0.22549 10
emissive 0 0 0
}
}
}


For global coodinates and orientation and scale use: MyNode (or Mybone, ...)._getDerivedPosition (|_getDerivedScale|_getDerivedOrientation) but be carefull, these values are not cached but calculated at runtime each time you call these functions (therefore "_" as a prefix: "use with caution").

Beside: Is there any connection between the physics objects and the bones? Does the physics objects "notify" the entity/skeleton if they are hit or or something similar? (just a question)

mbvital

09-05-2012 15:49:16

Hi,

Thank you a lot for your help.
I saw the code and now I can see the skeleton in the same position of the mesh, and I have my problem solved!!

I don’t know if I understood correctly your question but I just want to put physics in the arm bones to treat collision between external objects and the arm so it won’t pass a wall (I’m ignoring gravity).

Is that what you mean?
Thank you

Tubulii

09-05-2012 20:07:46

[...] the arm so it won’t pass a wall (I’m ignoring gravity).
Ah, you just want to know if the arms hit something and not what "happens" to the arm (the cylinders) if it is e.g. pushed away.

Good luck.

mbvital

12-05-2012 17:52:24

Hi,

In the future I also want to deal with what “happens” to the arm, to do that I was considering using joints between the colision objects I created. Not sure if it will work though.
But I’m far from that because I have a problem with attaching the body physics to the mesh's bone. It is not possible to do that (if it is I don't know how). It is only possible do a attachNode to a sceneNode and the bone is not a SceneNode. And if I don't do that when I move the body physics the mesh stays in the same place.

entHV = mSceneMgr.CreateEntity("RorkimaruMesh", "Victoria 4.2 QuickStart.mesh");
nodeHV = mSceneMgr.RootSceneNode.CreateChildSceneNode("RorkimaruMesh");
nodeHV.AttachObject(entHV);

skeleton = entHV.Skeleton;
Bone bone = skeleton.getBone(“rShldr”)

var cylinder = new Cylinder(world, 5.2f, 30, 8);
Body body = new Body(world, cylinder, true);
c.Dispose();
//body.AttachNode(bone);
cylinder.SetPositionOrientation(scale._getDerivedPosition(), scale.ConvertLocalToWorldOrientation(scale.Orientation));



I considered attaching the body to my nodeHV but I can't do that because of the root position of the mesh.

Thank you

Tubulii

12-05-2012 18:49:16

Usually, ragdoll physics are used to simulate "human interaction with other (solid) bodies": In some games killed enimies "flow" over the ground, in other games test dummies crash against walls etc. If you want to do something similar, than look for "ogrenewt" and "ragdoll" -> Tutorials and Ogrenewt (Tutorial 8 should be interesting) and some other informations (Mogrenewt).
The hero an the other hand "sticks" to the ground, there is no real "interaction" with the invironment but there is a very good "move and slide" algorithmus.

And if I don't do that when I move the body physics the mesh stays in the same place.

Theres always a problem with pyhsics: Either the physics engine controls the mesh (like a flying box) or the player (or script or AI) controls it. Otherwise there could be a race condition.
-> Update the physic objects (the position and orientation) or move the mesh by applying forces (very inaccurate)
I am not very familiar with (M)OgreNewt, but I think there has to be a callback/event which gets fired if the physics bodies are hit.

mbvital

24-05-2012 11:50:52

Hi,

I cant apply forces because when i have the sensors giving data, i am going to update the bones with that data, not the body physics.

Now i am updating physics object with the data of the bones. The problem is when i change my mesh position against the Wall, it passes the Wall. Do you have any suggestions?

Thank you

Tubulii

24-05-2012 12:04:54

Have set up physics objects for the wall, too?

mbvital

28-05-2012 13:04:35

Hi,

Yes i have. I have physics object for every objects in my application. I thought that could be what you said, but then i create a cube with physics and it does not pass through the Wall or ground. It means that the wall physics and ground are there.

Tubulii

28-05-2012 15:48:57

Have you enabled the debug view options of MogreNewt?
You would be able to see the pyhsics objects. Very usefull.

And you said, you updated the physics objects with the data of the bones. But if the physics objects hit something, they cannot influence the mesh's position?

mbvital

06-06-2012 09:13:03

Hi,

Yes, I have the debug enabled, and I can see all my physics bodies.
Yes, I update the physics objects with the positions of the bone, and if the physics object hits something, nothing happens to the mesh (the mesh passes through the Wall for example). It seems that when I apply physics I only can work with the physics body and I cannot change anything in the mesh, but I need to since I get my data from the sensors.

Thank you.

Tubulii

06-06-2012 13:31:36

If I am right, you need to apply the mesh's position and orientation with MogreNewt, because ...
every time it was moved by Newton, it's scene node position and orientation would update! (from wiki)
... otherwise, you need to call MYBODY.setPositionOrientation(...) or both objects get out of sync.