Topdown player-warlock movement in Box2D(.XNA)

(text taken from here: http://box2dxna.codeplex.com/Thread/View.aspx?ThreadId=211636)

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);
}
hi Santhos,

This is the way I moved a player in an overhead view game. You can see the game here:
http://dev.freedomandpartners.com/TSW_1240/games/gameshell/#/paparazzi_dash

updatePlayerControls is called on enterframe.

Hope this helps.
ps. gravity is set to 0.

Code:
      
private function updatePlayerControls():void{
            var goto:b2Vec2;
            var force:int = 60;
            var xxx:Number = 0;
            var yyy:Number = 0;
            var actor:MovieClip = player.GetUserData().data.actor;
                                 
            var playerIsMoving:Boolean = false;
               
            if(Input.isKeyDown(38)/*up*/){
               //up
               goto = new b2Vec2(0,-force);
               player.ApplyForce( goto , player.GetPosition());
               yyy = 2;               
               playerIsMoving = true;
               Tweener.addTween( actor, {rotation:MathUtils.getCorrectedAngle(actor.rotation, 180), onUpdate:playerXform, time:.2, transition:"linear"});
            }
            if(Input.isKeyDown(40)/*down*/){
               goto = new b2Vec2(0,force);
               player.ApplyForce( goto , player.GetPosition());
               yyy = - 2;                  
               playerIsMoving = true;
               Tweener.addTween( actor, {rotation:MathUtils.getCorrectedAngle(actor.rotation, 0), onUpdate:playerXform, time:.2, transition:"linear"});               
            //   movePlayer("down");            
            }
            if(Input.isKeyDown(37) /*left*/ ){
               goto = new b2Vec2(-force,0);
               player.ApplyForce( goto , player.GetPosition());                  
               xxx = 2;
               playerIsMoving = true;
               Tweener.addTween( actor, {rotation:MathUtils.getCorrectedAngle(actor.rotation, 90), onUpdate:playerXform, time:.2, transition:"linear"});                              
            }
            if(Input.isKeyDown(39) /*right*/ ){
               goto = new b2Vec2(force,0);
               player.ApplyForce( goto , player.GetPosition());                  
               xxx = -2;               
               playerIsMoving = true;
               Tweener.addTween( actor, {rotation:MathUtils.getCorrectedAngle(actor.rotation, 270), onUpdate:playerXform, time:.2, transition:"linear"});                                             
            }
            //diagonals
            if(Input.isKeyDown(38) && Input.isKeyDown(39)/*up RIGHT*/){
               Tweener.addTween( actor, {rotation:MathUtils.getCorrectedAngle(actor.rotation, 225), onUpdate:playerXform, time:.2, transition:"linear"});
            }
            if(Input.isKeyDown(40) && Input.isKeyDown(39) /*down RIGHT*/){
               Tweener.addTween( actor, {rotation:MathUtils.getCorrectedAngle(actor.rotation, 315), onUpdate:playerXform, time:.2, transition:"linear"});               
            }
            if(Input.isKeyDown(38) && Input.isKeyDown(37)/*up LEFT*/){
               Tweener.addTween( actor, {rotation:MathUtils.getCorrectedAngle(actor.rotation, 135), onUpdate:playerXform, time:.2, transition:"linear"});
            }
            if(Input.isKeyDown(40) && Input.isKeyDown(37) /*down LEFT*/){
               Tweener.addTween( actor, {rotation:MathUtils.getCorrectedAngle(actor.rotation, 45), onUpdate:playerXform, time:.2, transition:"linear"});               
            }
      }
      public function playerXform():void{
         var actor:MovieClip = player.GetUserData().data.actor;         
         var angle:Number = MathUtils.degreesToRadians(actor.rotation);
         player.SetXForm(player.GetPosition(), angle);
      }
Thanks. As far as I see it looks similar to my code.The only thing i didnt get was the Tweaner, what does it do?

How did you set up the forces? (meaning their values) And values of linear damping etc.?

My gravity is set to 0 as well (otherwise it wouldnt work actually, since the gravity vector is only two dimensional)

EDIT: i see, the force is set to 60. That looks quite low to me, but maybe its because of the other values. Were your player controlled objects a little floating? I would think so.

Original thread: https://box2d.org/forum/viewtopic.php?t=4941