Topdown player movement

May 5, 2010 at 11:27 AM

Hi,

i'm developing a game called Warlock Football as my bachelor thesis and i'm using your Box2D port. Although i'm not sure if I should post the question here, I'll try it. In worst case please redirect me somewhere else :)

 

I have a problem with motion of th warlocks who are basically moved by player. In this case they can either move forward or backward and then they can turn right and left. The first is made by applying force to their bodies and the other by applying torque. To limit the speed and rotation speed I use linear and angular damping, but since they are "humans" the movement should have very high acceleration and they should stop immediatelly when you stop pressing the movement key. I handle this by applying quite big force and torque and setting damping quite high, but still it makes the movement float-like. I have also considered setting their linear and angular velocities directly, but then I'm afraid that I'd screw the whole physics, for isntance when there would be an explosion near by, you'd able to escape its physical consequences by pressing movement key and thus overriding the applied forces. Has anyone any answer to such a question? (I actually hope that I have explained myself clearly and understandably :)

 

In addition here is the body definition code:

// define his body
 var bodyDef = new BodyDef();
bodyDef.type = BodyType.Dynamic;
bodyDef.position = position;
bodyDef.angle = angle;
bodyDef.bullet = false;
bodyDef.active = true;
bodyDef.fixedRotation = false;
bodyDef.allowSleep = false;
bodyDef.linearDamping = 4.5f; // DAMPING HERE
bodyDef.angularDamping = 5.0f;

// define his shape
var circleShape = new CircleShape();
circleShape._radius = 1.4f;

// define fixture using his shape
var fixtureDef = new FixtureDef();
fixtureDef.shape = circleShape;
fixtureDef.density = 10.0f;
fixtureDef.friction = 0.0f;
fixtureDef.restitution = 0.0f;

// forward force and torque for SteeringComponent <- FORCES FOR MOVEMENT
float forwardForce = 2800.0f;
float torque = 1200.0f;

And the way I apply forces for movement:
            // check movement flags and apply forces accordingly
            if ((Move & Movement.Forward) == Movement.Forward)
            {
                Vector2 force = new Vector2((float)Math.Cos(physicalBodyComponent.Angle), (float)Math.Sin(physicalBodyComponent.Angle));
                force *= forwardForce;
                physicalBodyComponent.ApplyForce(force, physicalBodyComponent.Position);
            }

            if ((Move & Movement.Backward) == Movement.Backward)
            {
                Vector2 force = Vector2.Negate(new Vector2((float)Math.Cos(physicalBodyComponent.Angle), (float)Math.Sin(physicalBodyComponent.Angle)));                
                force *= forwardForce;
                physicalBodyComponent.ApplyForce(force, physicalBodyComponent.Position);
            }

            if ((Move & Movement.TurnLeft) == Movement.TurnLeft)
            {
                physicalBodyComponent.ApplyTorque(-torque);
            }

            if ((Move & Movement.TurnRight) == Movement.TurnRight)
            {
                physicalBodyComponent.ApplyTorque(torque);
            }

Coordinator
May 8, 2010 at 7:32 PM

I have not played around with top-down physics to know what the good tricks are.

I would first suggest using Body.ApplyLinearImpulse and Body.ApplyAngularImpluse since those give you mass-independent behavior.

Next, you can try tweaking the dampening or implement your own more aggressive dampening using counter-impulses.

If you want your warlocks to be blasted away and knocked around then you definitely do not want to make them kinematic (where you set the velicity directly yourself) so you need to be working in terms of forces/impulses.  Even with dynamici bodies you can "cheat" though and directly set their position or velocity but you have to be careful about it and overall I would recommend avoiding it.

 

 

May 9, 2010 at 8:55 AM

Don't worry. I have actually tried several forums. Basically I haven't worked with Box2d before, so anyone can suggest a thing that might help :)

The body will be definitely Dynamic - it already is. I guess that part I got quite easily. I wasn't really sure what for the impulses are so I will try to use them and see how it behaves.

To dampen the effect of forces I already use quite harsh damping, but I'm afraid it will have a negative impact on the blasting / knocking away behaviour.

 

Finally, I have come up with an idea yesterday, not sure if it is going to work. That I would always take the linear and angular velocity and in case of detected movement I would add/substract something and then set it, but as you say and as well as it is written at many places, messing with setting velocity and position manually is not the clear way to do things.

 

Thanks for your time, I will consider and try your "using impulses" suggestion.