Move Character

Discuss any questions about BEPUphysics or problems encountered.
saturn1
Posts: 34
Joined: Thu Dec 24, 2009 11:14 pm

Move Character

Post by saturn1 »

Hello i try to move my character (box) in my map(StaticTriangleGroup) but when i push Key.Up my character move a little on the ground but its fluid and after few second it fall on the empty !

My code come from the example gettingStarted and i add this :

Code: Select all

//function Draw of class EntityModel
            worldMatrix = transform * 
                Matrix.CreateTranslation(entity.centerPosition) *
                entity.orientationMatrix;
....

Code: Select all

//Function handleInput of EntityModel
       public void handleInput(KeyboardState currentKeyboardState)
        {
            // First, we want to check to see if the tank should turn. turnAmount will 
            // be an accumulation of all the different possible inputs.
            float turnAmount = 0.0f;
            if (currentKeyboardState.IsKeyDown(Keys.Left))
            {
                turnAmount += 1;
            }

            if (currentKeyboardState.IsKeyDown(Keys.Right))
            {
                turnAmount -= 1;
            }

            // clamp the turn amount between -1 and 1, and then use the finished
            // value to turn the tank.
            turnAmount = MathHelper.Clamp(turnAmount, -1, 1);
            heroFacingDirection += turnAmount * heroTurnSpeed;


            // Next, we want to move the tank forward or back. to do this, 
            // we'll create a Vector3 and modify use the user's input to modify the Z
            // component, which corresponds to the forward direction.
            Vector3 movement = Vector3.Zero;

            if (currentKeyboardState.IsKeyDown(Keys.Up))
            {
                movement.Z = 1;
            }
            if (currentKeyboardState.IsKeyDown(Keys.Down))
            {
                movement.Z = -1;
            }

            // next, we'll create a rotation matrix from the direction the tank is 
            // facing, and use it to transform the vector.
            orientation = Matrix.CreateRotationY(heroFacingDirection);
            Vector3 velocity = Vector3.Transform(movement, orientation);
            velocity *= heroVelocity;

            // Now we know how much the user wants to move. We'll construct a temporary
            // vector, newPosition, which will represent where the user wants to go. If
            // that value is on the heightmap, we'll allow the move.
            Vector3 newPosition = position + velocity;

            orientation.Right = Vector3.Cross(orientation.Forward, orientation.Up);
            orientation.Right = Vector3.Normalize(orientation.Right);

            orientation.Forward = Vector3.Cross(orientation.Up, orientation.Right);
            orientation.Forward = Vector3.Normalize(orientation.Forward);

            entity.orientationMatrix = orientation;
            // once we've finished all computations, we can set our position to the
            // new position that we calculated.
            position = newPosition * 0.02f;
            entity.move(position);
}

I cant move correctly my character, i need a tutoriel or some help.
Thank you!
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Move Character

Post by Norbo »

First, Entity.move is an incremental move. This means that the position of the entity will be added to the parameter to get its new position. Entity.moveTo positions the entity on top of the parameter.

Second, the thing that's probably causing your character to fall through the ground (if I'm understanding the issue correctly) is that the move methods actually teleport the entity. The system cannot respond rigidly to the collisions because there is no velocity- the entity is just popping into other entities. A bit of springy penetration correction force will be applied, but if you're walking over bumpy surfaces or if you're teleporting quickly, your entity will very likely squish through.

In general, entities should move using velocities. Instead of teleporting your character forward 1/60 units 60 times a second, you could set the character's velocity to 1 unit/second in the direction you want to go each frame. A dynamic entity will be properly stopped by walls and obstacles when using velocities as opposed to teleportation.
saturn1
Posts: 34
Joined: Thu Dec 24, 2009 11:14 pm

Re: Move Character

Post by saturn1 »

Thank you for your fast answer.
I tried something like
//Call in Update
public void handleInput(KeyboardState keyboardState, GameTime gameTime)
{
if (keyboardState.IsKeyDown(Keys.Up))
entity.linearVelocity = Vector3.Up;
}

My character jump of one meter but when he touch the ground again it fly ^^ !!


Thank you for the help :)

PS : and Happy XMAS
saturn1
Posts: 34
Joined: Thu Dec 24, 2009 11:14 pm

Re: Move Character

Post by saturn1 »

OMG!!(xD)
I think i got it !

With that
Box heroBox = new Box(new Vector3(0, 0.15f, -5), 0.5f, 0.5f, 0.5f, 1); // work perfectly

Box heroBox = new Box(new Vector3(0, 0.15f, -5), 0.05f, 0.05f, 0.05f, 1); // bugging

I used 0.05 to render good siwe between my map and my hero ...
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Move Character

Post by Norbo »

.05 would definitely be on the extreme side of things. The collision detection system is tuned to support fairly 'regular sized' objects (.5 to 10 units per entity is safe; outside of that range generally works too, but if you go too far you can see problems). This is due to floating point precision issues; there aren't enough numbers represented in the floating point format to run these algorithms on really thin or huge objects.

And Merry Christmas to you too!
saturn1
Posts: 34
Joined: Thu Dec 24, 2009 11:14 pm

Re: Move Character

Post by saturn1 »

Yep merry christmas :)

In fact my problem is
my map is normal size but my my model hero is big big big size compare to map !
So its for that i used 0.05f!

I think the only solution its to reduce size of my hero in 3DS or blender??

Thank you :)
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Move Character

Post by Norbo »

Typically yes, some resizing has to be done to the graphics to make it fit. However, if your environment is physically small like your hero used to be, you could also scale it up too (it will probably work better in the end).
saturn1
Posts: 34
Joined: Thu Dec 24, 2009 11:14 pm

Re: Move Character

Post by saturn1 »

Thank you guy !!
I prefer to resize into 3DS!

Now need to learn 3ds^^ :)
saturn1
Posts: 34
Joined: Thu Dec 24, 2009 11:14 pm

Re: Move Character

Post by saturn1 »

Hello,
As i told u with the model cube it works perfectly.

But i try with a model , with the same scale on 3DSMAX, and this model is very very very bigger on the game !!


Some ideas ?

Thank you :)
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Move Character

Post by Norbo »

If you're talking just about the graphical scale, that is one of the fun parts of dealing with a content pipeline from start (3DS max or other production program) to finish (in game). Each program and conversion system will behave a little differently.

The only reliable suggestion I can provide is to find a certain content path that works, and then keep using it whenever possible. This content path may involve downscaling models an arbitrary amount in 3DS before exporting, or applying a scaling matrix when rendering, or scaling inside the XNA Content Pipeline, or a variety of other things.
saturn1
Posts: 34
Joined: Thu Dec 24, 2009 11:14 pm

Re: Move Character

Post by saturn1 »

or applying a scaling matrix when rendering
I try something like :
In Draw method of my hero
worldMatrix = Matrix.CreateScale(0.01f) * transform * entity.worldTransform;//0.01 its to render at good size my hero

But its not convenient because the Box has still 1 as scale so for collision its not good !
or scaling inside the XNA Content Pipeline
WHat is that ?

Thank you :)
saturn1
Posts: 34
Joined: Thu Dec 24, 2009 11:14 pm

Re: Move Character

Post by saturn1 »

I try to make it better, but as im not good in mathematic i dont found a formula :

But with something like :
//draw hero
worldMatrix = transform * entity.worldTransform * Matrix.CreateScale(0.9f);

heroModel = Content.Load<Model>("Models\\dude");
foreach (ModelBone bones in heroModel.Bones)
{
bones.Transform = Matrix.CreateScale(0.2f);
}


Box heroBox = new Box(new Vector3(0, 2f, -2), 0.5f, 0.5f, 0.5f, 1);

It s near perfect xD
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Move Character

Post by Norbo »

When you're setting the matrix up for drawing, imagine you're doing the operations in order. For example, let's say the 'transform' in the following expression is "rotate 90 degrees around X axis", then

Code: Select all

worldMatrix = transform * entity.worldTransform * Matrix.CreateScale(0.9f);
would first rotate a point 90 degrees around the x axis, then rotate and translate it based on the entity's rotation and translation, then finally multiply that location by .9.

The final scaling matrix shrinks the location in world space, so while your character might be at (1000,0,0), it is drawn at (900,0,0).

Instead, you want to scale the character before translating him around. So you could use:

Code: Select all

worldMatrix =  Matrix.CreateScale(0.9f) * transform * entity.worldTransform;
The 'transform' field of an EntityModel is designed to include this sort of scaling or local translation, so you could just set the EntityModel instance's transform to Matrix.CreateScale(0.9f) (multiplied by whatever other local transforms you want too) to get the desired result without having to hardcode anything in the draw method.
saturn1
Posts: 34
Joined: Thu Dec 24, 2009 11:14 pm

Re: Move Character

Post by saturn1 »

Thank you guy :)

Just a another little question...
Box
Specialized primitive with a mathematical definition, well suited for crate duty.
Capsule
Spherically expanded line segment; a pill.
Cylinder
Mathematically defined object with a height and radius with two circular ends.
Cone
Mathematically defined object with a height and radius with one circular and one single-point end.
Sphere
Simple mathematically defined object with a radius.
Triangle
Three vertices connected by three edges.
MinkowskiSum
Sweeps the two component entities through each other to form a new shape.
ConvexHull
A triangulated exterior shell of a point set.
WrappedBody
An implicit convex hull around a set of shapes.
I try to "encapsule" my hero with Box (but its more a rectancle than a box...) and it works.
So i tried with Sphere and Capsule and cylinder :
I see my hero but there is no collision ! I mean just it stay on the horizontal plane :

Just i changed that :

Code: Select all

Cylinder heroBox = new Cylinder(MotionState.defaultState, 0.5f, 0.5f);
            heroBox.linearVelocity = Vector3.Forward;
            space.add(heroBox);
            Matrix scaling = Matrix.CreateScale(0.5f);
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Move Character

Post by Norbo »

Every entity type has two main kinds of constructor- dynamic and kinematic. Dynamic entities have mass, respond to collisions, and generally bounce around. Kinematic entities obey their velocity and will ignore collisions to get where their velocity wants them to go. You can think of kinematic entities as having infinite mass (an "unstoppable force"). The difference between a kinematic constructor and a dynamic constructor is the inclusion of the mass parameter in the dynamic constructor. If you add one more value to the end of that constructor, it should work as expected.
Post Reply