Bepu Info Collision and Dynamics

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
innipotente
Posts: 4
Joined: Tue Jun 12, 2012 3:45 pm

Bepu Info Collision and Dynamics

Post by innipotente »

Hi, I'm new in the world BEPU.
I am making a little game of football (soccer).
I have several questions about the use of the library:
a) I created a sphere (entity) holding my 3D ball (model) and cylinders that contain the 3d soccer goal. Why the ball collides with a cylinder only if kicked low shot (and not in air)?

Box soccergoal= new Box(new Vector3(0,0f,-1100),630,240,100);
space.Add(soccergoal);
modelsoccergoal = new EntityModel(soccergoal, content.Load<Model>("soccergoal"), Matrix.Identity, myCamera);
soccergoal.Tag = modelsoccergoal;

soccergoal.CollisionInformation.Events.DetectingInitialCollision += HandleCollision;

Cylinder poledx = new Cylinder(new Vector3(-350f, 0f, -1090), 250f, 35f);
Cylinder polesx = new Cylinder(new Vector3(350, 0f, -1090), 250f, 35f);
Cylinder poleup = new Cylinder(new Vector3(360, 250, -1090), 710f, 15f);

poleup.Orientation = Quaternion.CreateFromAxisAngle(Vector3.UnitZ, MathHelper.PiOver2);

space.Add(poledx);
space.Add(polesx);
space.Add(poleup);
poledx.CollisionInformation.Events.InitialCollisionDetected += HandleCollisionPole;
polesx.CollisionInformation.Events.DetectingInitialCollision += HandleCollisionPole;
poleup.CollisionInformation.Events.DetectingInitialCollision += HandleCollisionPole;


In this code, HandleCollisioPole is triggered only if ball run on the soccer field and not in the air. I apply an arbitrary impulse (Is it right?) to the ball when kicked.

b) the ball trajectory after it slammed on the soccer ball pole is changed by BEPU automatically, right?


c) Should I create a goalkepper that dives: the animation is done in software modeling? How to manage collisions of an animated 3D model?

Thanks in advance
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Bepu Info Collision and Dynamics

Post by Norbo »

a) I created a sphere (entity) holding my 3D ball (model) and cylinders that contain the 3d soccer goal. Why the ball collides with a cylinder only if kicked low shot (and not in air)?
Given that code, this is what the collision geometry actually looks like:
goal.png
goal.png (22.21 KiB) Viewed 5484 times
The "soccergoal" model probably doesn't line up with those collision shapes, so the ball will appear to go through it in many places (and hit places where it 'shouldn't').

The visualization helper used to create that image is the BEPUphysicsDrawer that can be found in the main source download. A fairly stripped down example of its usage can be found in the Multithreading Demo too.

By the way, those cylinders are huge. The default scale interpretation likes objects to be somewhere in the range of 0.5 to 10 units. It's possible to go outside of this range, but if you go too far (thousands upon thousands of units), numerical issues can start to crop up. If you happen to see such issues, you can address them by either normalizing the scale of the simulation to the default scale interpretation or retuning BEPUphysics to understand the larger simulation. Examples of this retuning can be found in the ScaleDemo and ConfigurationHelper.ApplyScale of the BEPUphysicsDemos project in the main source download.
I apply an arbitrary impulse (Is it right?) to the ball when kicked.
Yup, just applying an impulse is fine to kick a ball. If you want greater control over the velocity, you could just set the Linear/AngularVelocity properties directly.
b) the ball trajectory after it slammed on the soccer ball pole is changed by BEPU automatically, right?
Yup. If it's a dynamic entity (constructed with a mass), it will obey collision response. Kinematic entities (constructed without a mass), on the other hand, have infinite mass and will go straight through anything and ignore all external forces. Check out the BEPUphysicsDemos in the main source download for a bunch of examples of simulations using both kinds.
c) Should I create a goalkepper that dives: the animation is done in software modeling? How to manage collisions of an animated 3D model?
It would be a good idea to approximate the character; performing collision detection on every triangle in a character model would be a large waste of time. The movement of a character could be handled with something like the CharacterController from the BEPUphysicsDemos. The CharacterController is a single cylinder, so it would probably be a good idea to use CollisionRules to prevent the ball from hitting the character cylinder. This means the character cylinder is just used for the character's interaction with the environment and other players. To block the ball, you could position kinematic entities according to the current animation of the character; these entities could be made to only collide with the ball. This lets the pose of the character describe how it blocks incoming balls. On the other hand, this may be way too much detail for the game, and just using the CharacterController's main cylinder with perhaps an extra big blocker or something would be sufficient.
innipotente
Posts: 4
Joined: Tue Jun 12, 2012 3:45 pm

Re: Bepu Info Collision and Dynamics

Post by innipotente »

By the way, those cylinders are huge. The default scale interpretation likes objects to be somewhere in the range of 0.5 to 10 units. It's possible to go outside of this range, but if you go too far (thousands upon thousands of units), numerical issues can start to crop up. If you happen to see such issues, you can address them by either normalizing the scale of the simulation to the default scale interpretation or retuning BEPUphysics to understand the larger simulation. Examples of this retuning can be found in the ScaleDemo and ConfigurationHelper.ApplyScale of the BEPUphysicsDemos project in the main source download.
Thanks a lot.
To solve this problem, can I resize the models? Now my models are exported in centimeters, but I can export them in meters. In this way, I will have ball (0.22 unit of diameter) and soccergoal (about 7x2.5x1 unit) at the distance of 11 meters (11 unit).
In this way I can avoid the scaling in the code, is it right?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Bepu Info Collision and Dynamics

Post by Norbo »

The exporter can be changed to output a different size model, yes. However, because the graphics are completely separate from the physics representation, the collision shapes themselves still require modification to bring them into the default scale interpretation.
innipotente
Posts: 4
Joined: Tue Jun 12, 2012 3:45 pm

Re: Bepu Info Collision and Dynamics

Post by innipotente »

Hi Norbo,
so, to do animation:
1) I must create animation in 3d studio max and export it to fbx;
2) I play the animation with XNA
3) I must create the bepu cylinder or bepu sphere to wrap my model to handle collision with the ball

Is it ok?
And if goalkeeper dives left, cylinder moves automatically with animation play? Or do I have to move the cylinder through code?

I must see SimpleCharacterController or CharacterController?
Thanks in advance :wink:
It would be a good idea to approximate the character; performing collision detection on every triangle in a character model would be a large waste of time. The movement of a character could be handled with something like the CharacterController from the BEPUphysicsDemos. The CharacterController is a single cylinder, so it would probably be a good idea to use CollisionRules to prevent the ball from hitting the character cylinder. This means the character cylinder is just used for the character's interaction with the environment and other players. To block the ball, you could position kinematic entities according to the current animation of the character; these entities could be made to only collide with the ball. This lets the pose of the character describe how it blocks incoming balls. On the other hand, this may be way too much detail for the game, and just using the CharacterController's main cylinder with perhaps an extra big blocker or something would be sufficient.
Norbo
Site Admin

Posts: 2970
Joined: Tue Jul 04, 2006 5:45 am
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Bepu Info Collision and Dynamics

Post by Norbo »

Is it ok?
That's the rough outline, yup.
And if goalkeeper dives left, cylinder moves automatically with animation play? Or do I have to move the cylinder through code?
The entities must be controlled directly. The physics engine, by itself, has no concept of graphics; any association between graphics and physics must be directly handled.

The entities representing the body could be created in the 'bind pose' of the character and associated with certain animation bones by a transform. When the character moves, the entities would be moved according to the animation bones and the transform associated with the entity. So, if localTransform is the transform from the animation bone to the entity, the world space location of the entity would be the localTransform * animationBoneWorldTransform.

To actually move the entities, it would be a good idea to use velocities instead of teleportation alone. This allows collision response to work correctly. The EntityMover and EntityRotator are a couple of simple convenience classes which will compute the velocities to apply to a kinematic entity given a position/orientation goal for you.
I must see SimpleCharacterController or CharacterController?
The CharacterController and SphereCharacterController are substantially better than the SimpleCharacterController. The SphereCharacterController is a sphere and doesn't support stepping, but if the character body is only colliding with the environment, it might not matter that it's a sphere.
innipotente
Posts: 4
Joined: Tue Jun 12, 2012 3:45 pm

Re: Bepu Info Collision and Dynamics

Post by innipotente »

Hi Norbo,
I have played animation in my project and It works!
I have added SphereCharacterController and EntityMover but when I use EntityMover.TargetPosition my model is moved! Why?

Code: Select all

CharacterController = new SphereCharacterController(new Vector3(0, 0, -11f), 1.5f,100f);
                bodyMover = new EntityMover( CharacterController.Body);
                CharacterController.Body.BecomeKinematic();
                
                space.Add(CharacterController);
                space.Add(bodyMover);
                modeldude = new EntityModel(CharacterController.Body, content.Load<Model>("GoalkeeperLeftAnim"), Matrix.Identity);
                CharacterController.Body.Tag = modeldude;
               
                SkinningData skinningData = modeldude.model.Tag as SkinningData;

                if (skinningData == null)
                    throw new InvalidOperationException
                        ("This model does not contain a SkinningData tag.");

                // Create an animation player, and start decoding an animation clip.
                animationPlayer = new AnimationPlayer(skinningData);

                AnimationClip clip = skinningData.AnimationClips["Take 001"];

                animationPlayer.StartClip(clip);
And In my Update method:

Code: Select all

          animationPlayer.Update(gameTime.ElapsedGameTime, true, Matrix.Identity);

            bodyMover.TargetPosition = bodyMover.Entity.Position +new Vector3(-0.1f,0f,0f);

My fbx have animation and I play it!
I must move entities only to collisions handle!

Finally, how can I associate sphere to a single bone? If my bone's name Tom, must I refer to "Tom"? And how can I capture Tom position (position change continuously in animation fbx)?

Why must I use SphereCharacterController? Can I create 5 simple sphere (body, legs and arms) to wrap my character? (Whereas my animations are preloaded in fbx files!)

Thanks in advance :wink:















Norbo wrote:
Is it ok?
That's the rough outline, yup.
And if goalkeeper dives left, cylinder moves automatically with animation play? Or do I have to move the cylinder through code?
The entities must be controlled directly. The physics engine, by itself, has no concept of graphics; any association between graphics and physics must be directly handled.

The entities representing the body could be created in the 'bind pose' of the character and associated with certain animation bones by a transform. When the character moves, the entities would be moved according to the animation bones and the transform associated with the entity. So, if localTransform is the transform from the animation bone to the entity, the world space location of the entity would be the localTransform * animationBoneWorldTransform.

To actually move the entities, it would be a good idea to use velocities instead of teleportation alone. This allows collision response to work correctly. The EntityMover and EntityRotator are a couple of simple convenience classes which will compute the velocities to apply to a kinematic entity given a position/orientation goal for you.
I must see SimpleCharacterController or CharacterController?
The CharacterController and SphereCharacterController are substantially better than the SimpleCharacterController. The SphereCharacterController is a sphere and doesn't support stepping, but if the character body is only colliding with the environment, it might not matter that it's a sphere.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Bepu Info Collision and Dynamics

Post by Norbo »

The SphereCharacterController should not be moved with an EntityMover. The SphereCharacterController has its own movement abilities; check out the SphereCharacterControllerInput class to see an example of telling the character to do things.

The body of the SphereCharacterController is just used as a simple proxy to collide with the environment. This is because attempting to use a bunch of per-bone entities for character movement would be extremely complicated and unnecessary. The per-bone entities should be set up to collide only with the soccer ball, and the character body should be set up to only collide with the environment (i.e. things that aren't the soccer ball).
Finally, how can I associate sphere to a single bone? If my bone's name Tom, must I refer to "Tom"? And how can I capture Tom position (position change continuously in animation fbx)?
That depends on the animation system. The physics engine doesn't know or care about where the information comes from. Typically, animation systems support some method of getting the current world transform for a certain bone. That, combined with a local transform for the entity, would be sufficient to position an associated entity in the right spot.

Once you have the world position and rotation of the entity, an EntityMover/EntityRotator can be used to position the bone's entity in the right spot.
Why must I use SphereCharacterController? Can I create 5 simple sphere (body, legs and arms) to wrap my character? (Whereas my animations are preloaded in fbx files!)
The SphereCharacterController is just for environment collisions and character-like movement control; you can create whatever per-bone entity representation you want to collide with the soccer ball. Those per-bone entities are separate from and in addition to the character controller.
Post Reply