Body phases through wall.

AndroidAdam

16-02-2010 22:52:47

Hi, I have a problem with my character moving through walls if he pushes against them long enough. Hoping someone here can help.

I move the character by setting the velocity when a WASD key is pressed. I use C# with Mogre and MogreNewt

bool moving = false;

if (Game.Keyboard.IsKeyDown(MOIS.KeyCode.KC_A))
{
Vector3 dir = node.Orientation * new Vector3(-1, 0, 0);
body.Velocity = dir * 3.0f;
moving = true;

}
if (Game.Keyboard.IsKeyDown(MOIS.KeyCode.KC_D))
{
Vector3 dir = node.Orientation * new Vector3(1, 0, 0);
body.Velocity = dir * 3.0f;
moving = true;
}
if (Game.Keyboard.IsKeyDown(MOIS.KeyCode.KC_W))
{
Vector3 dir = node.Orientation * new Vector3(0, 0, -1);
body.Velocity = dir * 3.0f;
moving = true;
}
if (Game.Keyboard.IsKeyDown(MOIS.KeyCode.KC_S))
{
Vector3 dir = node.Orientation * new Vector3(0, 0, 1);
body.Velocity = dir * 3.0f;
moving = true;
}

This works great for moving the character, and collision does occur when he hits the wall. It's only after he is pressing against the wall for an extended amount of time that he phases through it.

[attachment=1]normalNinja.png[/attachment]

[attachment=0]phasingNinja.png[/attachment]

Here's the body definition, I use a simple box primitive.

Collision col = new MogreNewt.CollisionPrimitives.Box(Game.World, entity.BoundingBox.Size, 0);
body = new Body(Game.World, col);
col.Dispose();
body.SetMassMatrix(mass, Vector3.ZERO);
body.AttachNode(node);
body.SetPositionOrientation(node.Position, node.Orientation);
body.IsGravityEnabled = true;


MogreNewt.BasicJoints.UpVector uv2 = new MogreNewt.BasicJoints.UpVector(Game.World, body, Vector3.UNIT_Y);


Sorry for the rather lengthy post, any help would be appreciated.

koirat

16-02-2010 23:23:23

Do not set velocity like that.
Use forces instead , there is an equation about what force to use to make body move with specified velocity.

AndroidAdam

17-02-2010 00:14:26

Thank you for your quick reply. I will adjust my code accordingly.

koirat

17-02-2010 12:50:13

Just in case.
This is the equation I'm using.

public static Vector3 CalculateForce(float bodyMass,Vector3 currentVelocity,Vector3 desiredVelocity,float time)
{
return bodyMass * (desiredVelocity - currentVelocity) / time;
}

PJani

18-02-2010 17:16:12

You have inverseDynamicForce i thnk for this type of problem!!!