Connecting physics with animations - for enemies

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
zygmuntix
Posts: 8
Joined: Wed Mar 28, 2012 11:26 am

Connecting physics with animations - for enemies

Post by zygmuntix »

Hi,
I am making a 3D game right now and I need some help. Physics for camera and static objects like pyramids and temples work rather well. Problem is with making physics for enemies. I am using ConvexHull to represent them physically. But I don't know how to connect animations for them with their physical representations (Convex Hulls). Additionally (and more importantly) it looks like physics aren't connected with the graphical representations of enemies. When I am moving convex hull, the model doesn't move at all. I would like to have convex hulls, that cannot be rotated or moved by player, but they have to move when the characters are walking, running etc.
Here is the code:

Code: Select all

//Load in mesh data and create the group.
                        Vector3[] dynamicTriangleVertices;
                        int[] dynamicTriangleIndices;
                        Vector3 rotRad = (float)Math.PI / 180.0f * rot;
                        Quaternion rotationQuaternion = Quaternion.CreateFromYawPitchRoll(rotRad.Y, rotRad.X, rotRad.Z);

                        //This load method wraps the TriangleMesh.GetVerticesAndIndicesFromModel method 
                        //to output vertices of type StaticTriangleGroupVertex instead of TriangleMeshVertex or simply Vector3.
                        TriangleMesh.GetVerticesAndIndicesFromModel(enemyModel, out dynamicTriangleVertices, out dynamicTriangleIndices);
                        Matrix world = Matrix.CreateFromQuaternion(rotationQuaternion) * Matrix.CreateScale(scale); //Matrix.CreateTranslation(pos);

                        ConvexHull convexHull = new ConvexHull(dynamicTriangleVertices, 10.0f);

                        TransformableShape transShape = new TransformableShape(convexHull.CollisionInformation.Shape, Matrix3X3.CreateFromMatrix(world));
                        Entity entity = new Entity(new ConvexCollidable<TransformableShape>(transShape), 10.0f, convexHull.LocalInertiaTensor, convexHull.Volume);
                        entity.Position = pos;
                        entity.Orientation = rotationQuaternion;

                        PhysicsManager.Instance.Space.Add(entity);
                        Game1.ModelDrawer.Add(entity);
Fe_Yoshi
Posts: 397
Joined: Tue Jul 04, 2006 5:05 pm
Location: New Tower!

Re: Connecting physics with animations - for enemies

Post by Fe_Yoshi »

You don't need to be able to see where things are. Just FEEL where they are. It's much more effective.
It's simple, just take the hydraulic phase ship emulator and attach it to the photon particle emitter, BAM, new tower!
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Connecting physics with animations - for enemies

Post by Norbo »

You don't need to be able to see where things are. Just FEEL where they are. It's much more effective.
While asking players to develop second sight to find their enemies might work, there are probably better options.
But I don't know how to connect animations for them with their physical representations (Convex Hulls).
If this refers to making individual ragdoll pieces follow an animation, then this is done by positioning the entity according to animation bones. At content creation time, establish a local transform from the animation bone in bind pose to the entity. At runtime, compute the bone's final world position through the hierarchy and then include the local transform to get the entity location. (Be careful about the multiplication order; the local transform should be applied 'before' the animation bone world transform.)

Multiple bones could be weighted together to compute an entity transform, too.

The actual process of moving an entity should be done using velocities if possible unless nothing can possibly interact with them. It's still good to have a way to compute the velocities even if you do teleport the objects by setting their Position/Orientation directly, though- if the character dies and goes full ragdoll, the velocities of the bones will provide a natural extrapolation of movement rather than just stopping and crumpling to the ground.

It's good to make these ragdoll pieces completely uncollidable while the character is not in ragdoll mode. That way, you don't have to worry about them smashing objects out of the way or negatively influencing the character controller behavior. You can make objects transparent to collisions using collision rules or just by not adding them to the space until they're needed.

If you're not trying to make ragdoll pieces, but rather just want to move a model to match an object, then you can use the entity.WorldTransform which is a convenience property built from the Position and Orientation of the entity. For example, using a CharacterController, the transform is CharacterController.Body.WorldTransform. Use this as a 'root' transform for any skinning operation and the character should appear in the right spot.
When I am moving convex hull, the model doesn't move at all.
This is a purely graphical issue, so I can't be much help. Make sure the objects themselves are actually moving. If you're using the BEPUphysicsDrawer for debug visualization purposes, make sure the drawer's Update method gets called so that the transforms get updated.
zygmuntix
Posts: 8
Joined: Wed Mar 28, 2012 11:26 am

Re: Connecting physics with animations - for enemies

Post by zygmuntix »

Thanks very much for the reply :) I will try to make the things that you have written.
Post Reply