Simple character controller with skinned model

Discuss any questions about BEPUphysics or problems encountered.
limeh
Posts: 30
Joined: Sat Apr 30, 2011 7:22 pm

Simple character controller with skinned model

Post by limeh »

Hi, i'm trying to do a 3rd person shooting game using bepu physics engine.
Using the sample from the demo-SimpleCharacterController and SimpleCharacterControllerInput, i have been trying to replace the capsule models into my own .x model. I have successfully loaded the model and animation using SkinnedEffect normally without the physics engine but I can't figure out how to integrate it into simplecharactercontroller. Does anyone have any ideas or samples?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Simple character controller with skinned model

Post by Norbo »

It sounds like all you need to do is position the model where the character is. You can do this by grabbing the Position value from the character's body entity and using it as a translation component in the transformation applied to the model. The character controller body itself does not rotate, so you'll need to handle its orientation separately (based on camera pitch and yaw, for example).

The base transformation would then be something like RotationFromCameraViewsTransform * Matrix.CreateTranslation(character.Body.Position).
limeh
Posts: 30
Joined: Sat Apr 30, 2011 7:22 pm

Re: Simple character controller with skinned model

Post by limeh »

thanks! i did what you said and i got it to work. The issue now is, how do i go about doing the appropriate animation? Lets say when my character gets hit by a fireball, he is suppose to fall or just move back. This is already calculated by the physics. But when it comes to animation (using ChangeAnimation("Fall") or such), how do i make it fall nicely? I looked at the ActionFigureDemo but its done by seperating each body parts which i definitely can't do in a human model.
Seleste
Posts: 6
Joined: Fri Apr 29, 2011 2:12 pm

Re: Simple character controller with skinned model

Post by Seleste »

If you already have a falling animation defined, you wouldn't really need actionfigure or ragdoll physics. You'd likely just want to register an InitialCollisionDetectedEventHandler in which you change your character animation when colliding with a fireball or other such projectiles. These events are registered through a ContactEventManager<T>, you'd likely want to instantiate one for your character class where you'll be able to handle all special collision cases through event callbacks.

If you do want ragdoll physics, then I'll defer to Norbo as I don't know the first thing about those in BEPU. :)
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Simple character controller with skinned model

Post by Norbo »

Yup, if you have an animation, going with Seleste's approach is a good way to do it. If you want ragdoll physics, you'll have to create a constrained physical version of the character which is enabled when some event occurs (like being hit too hard, dying, or exploding), and then attach the model's bones to the ragdoll.

A sample of the physics part of a ragdoll can be found in the ragdoll sample (first link): http://bepuphysics.codeplex.com/wikipag ... umentation
limeh
Posts: 30
Joined: Sat Apr 30, 2011 7:22 pm

Re: Simple character controller with skinned model

Post by limeh »

I've got a preset animation of the fall (falling on the spot) so I dont think I want to go to the ragdoll method.
Right now i'm still using the simple character controller and am trying to tune the collision behaviour. My capsule is being hit by a fireball but right now the capsule will never fall. It will always remain standing. I've tried playing with the mass but it doesn't seem to change anything other than the velocity.

Secondly, as mentioned by Seleste, what is this InitialCollisionDetectedEventHandler ? I can't seem to find any documentation about it.. are there any demos or samples on it?

Thirdly, when i try to use the tags : "if ((string)localEntityInformation.Entity.Tag == "model")" , it gives me an error saying "Invalidcastexception was unhandled". Why is this happening?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Simple character controller with skinned model

Post by Norbo »

Right now i'm still using the simple character controller and am trying to tune the collision behaviour. My capsule is being hit by a fireball but right now the capsule will never fall. It will always remain standing. I've tried playing with the mass but it doesn't seem to change anything other than the velocity.
The character controller has infinite inertia, so it cannot rotate. This is needed for the SimpleCharacterController to stay "Simple" :) One good option is just to avoid doing anything physical to the character's orientation and just portray everything but simple linear motion with animations.
Secondly, as mentioned by Seleste, what is this InitialCollisionDetectedEventHandler ? I can't seem to find any documentation about it.. are there any demos or samples on it?
The events documentation can be found here:
http://bepuphysics.codeplex.com/wikipag ... umentation

The FishInABarrelDemo in the BEPUphysicsDemos project shows a simple usage of collision events. The GettingStartedDemo (http://bepuphysics.codeplex.com/wikipag ... umentation) has another example.
Thirdly, when i try to use the tags : "if ((string)localEntityInformation.Entity.Tag == "model")" , it gives me an error saying "Invalidcastexception was unhandled". Why is this happening?
If the entity's tag is not a string, it cannot be cast to a string, and you will get an InvalidCastException.
limeh
Posts: 30
Joined: Sat Apr 30, 2011 7:22 pm

Re: Simple character controller with skinned model

Post by limeh »

I found out that it is not a string when I use 'toAdd.tag = model;'. This is solved when i change it to 'toAdd.tag = "model";' . However, now i cannot remove the model using 'Components.Remove((EntityModel)localEntityInformation.Entity.Tag);' . It gives the same error to it this time. How to i fix that?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Simple character controller with skinned model

Post by Norbo »

A string can't be cast to an EntityModel :)

If you want both, consider making a custom object/container that contains an EntityModel and a string, and put that combo-object into the tag.
limeh
Posts: 30
Joined: Sat Apr 30, 2011 7:22 pm

Re: Simple character controller with skinned model

Post by limeh »

Sorry, what do you mean by creating a custom object/container?

Also, back to the previous post, you mention that a good option is to avoid doing anything physical to the character's orientation. But if i do that, the collision box will not change from the original and that would be a problem?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Simple character controller with skinned model

Post by Norbo »

The "Tag" property just contains an object. Any type object can be stuffed in it. However, a single object can't be both an EntityModel and a string simultaneously, so you can't cast it to both without InvalidCastExceptions. Instead, you can make a new class that has properties for an EntityModel and String. Instead of casting to an entity model or string individually, you cast to that new type, and then grab the property you want at the time.
But if i do that, the collision box will not change from the original and that would be a problem?
Is it actually a problem? What systems require that the character's physical capsule tip over? Would it be easier to simply 'pretend' that it fell over? Can you fake the behavior (ignore raycasts or hits on certain parts of the body while in the 'fallen' state)? Could you just swap out the entity for a different shape that represents the 'fallen' character more tightly (be careful with ray positioning)? These are all rhetorical questions, but there usually aren't that many times where making the character's actual body capsule tip over is the best option. My advice would be to look at what the game needs and do it as directly as possible.
limeh
Posts: 30
Joined: Sat Apr 30, 2011 7:22 pm

Re: Simple character controller with skinned model

Post by limeh »

okies, i'll try what you suggest.

For now, I'm having another problem and thats changing the animation of the enemy when he gets hit. I tried looking into FishInABarrelDemo but i really can't understand how it works. Or rather, i dont understand what "acceptedTriggerEntity.CollisionInformation" actually stores?
Right now as mentioned i'm able to remove the fireball when it collides with something. But i'm unable to change the animation for the object it hits. I believe that i need to dig information from 'Collidable other' but I can't. I tried using 'if ((string)otherEntityInformation.Entity.Tag == "Firemage")' and it works when the collision hits the enemy. However, when it hits the terrain(static mesh) or other things, it comes up with 'nullreferenceexception was unhandled'.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Simple character controller with skinned model

Post by Norbo »

An entity's CollisionInformation acts as its collision proxy in the collision pipeline. The type of the property, EntityCollidable, is a child class of Collidable (the superclass of all things capable of generating contacts), which is in turn a child class of BroadPhaseEntry (the superclass of all things that can live in the broad phase).

The StaticMesh is a child of Collidable as well. It is not itself an Entity nor an EntityCollidable, so it cannot be cast to either.

Since there is no entity associated with a StaticMesh, you can't use the Entity.Tag with it. However, in the development version (which you can download here: http://bepuphysics.codeplex.com/SourceC ... evelopment), BroadPhaseEntry has its own tag. Since StaticMesh is a BroadPhaseEntry, it has a Tag property. Similarly, since EntityCollidable is a BroadPhaseEntry, it also has a Tag property of its own (entity.CollisionInformation.Tag, independent of the owning entity's Tag property).

If you need to access tags from collision events, using the BroadPhaseEntry tag is probably the best bet.
limeh
Posts: 30
Joined: Sat Apr 30, 2011 7:22 pm

Re: Simple character controller with skinned model

Post by limeh »

I tried using the development version but i can't seem to assign a tag to my static mesh other than using mesh.tag = "level";
This however would cause the same problem as before.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Simple character controller with skinned model

Post by Norbo »

The purpose of the tag on broad phase entries is to make it easier to get the tag from within a collision event, where you can't necessarily assume the other object is an entity.

The problem of storing multiple types of data in the Tag field is still best solved by creating a single object that contains all the information you want in the tag.
Post Reply