2

Hello I'm experimenting with Box2dWeb, and working with top-down car game.

My problem arises when I try to control the car, so it will move, at first only forwards. For simplicity I don't want to use wheels, and just apply the force to the car (a box).

For the controls I made a function for but for a reason it's not getting called... That's where I need a pointer or advice. (Creation and placement of objects works just fine)

Here's part of the code:

var GlobalVar={   }
var KEY = {
    UP: 87,//W
    DOWN: 83,//s
    LEFT: 65,//A
    RIGHT: 68//D
}        
GlobalVar.pressedKeys = [];//an array to remember which key is pressed or not

$(function(){
   $(document).keydown(function(e){
     GlobalVar.pressedKeys[e.keyCode] = true;
 });
$(document).keyup(function(e){
  GlobalVar.pressedKeys[e.keyCode] = false;
 });

Rendering();
PlaceStuff(GlobalVar.currentLevel);//placing stuff, like car and boundaries/walls
moveCar();

});
function moveCar(){
 if (GlobalVar.pressedKeys[KEY.UP]){
   var force = new b2Vec2(0, -10000000);
   GlobalVar.car.ApplyForce(force, GlobalVar.car.GetWorldCenter());
  }
}
1
  • 1
    moveCar only gets called once at the start of the program. Basically, you're never checking if the car should move after startup. Commented Jul 8, 2014 at 7:56

2 Answers 2

1

It doesn't look like the moveCar function is being called more than once.

You should do the following:

function moveCar(){

   if (GlobalVar.pressedKeys[KEY.UP]){
       var force = new b2Vec2(0, -10000000);
       GlobalVar.car.ApplyForce(force, GlobalVar.car.GetWorldCenter());
    }


    requestAnimationFrame(moveCar);

}

You may also want to add a modifier to modify the amount of force added depending on the frame rate:

then = Date.now();

function moveCar(){

    var now = Date.now();
    var modifier = now - then; // Make modifier the time in milliseconds it took since moveCar was last executed.

    then = now;

   if (GlobalVar.pressedKeys[KEY.UP]){
       var force = new b2Vec2(0, -10000000);
       GlobalVar.car.ApplyForce(force * modifier, GlobalVar.car.GetWorldCenter());
    }

    requestAnimationFrame(moveCar);

}

This will ensure the car doesn't move slower on slower systems.


If you also want the Rendering() function to be executed more than once, you may also want to create another function which gets called as often as possible and calls the other two functions.

then = Date.now();

function moveCar(modifier){
   if (GlobalVar.pressedKeys[KEY.UP]){
       var force = new b2Vec2(0, -10000000);
       GlobalVar.car.ApplyForce(force * modifier, GlobalVar.car.GetWorldCenter());
    }
}

function update() {
    var now = Date.now();
    var modifier = now - then; // Make modifier the time in milliseconds it took since moveCar was last executed.

    then = now;

    moveCar(modifier);
    Rendering();

    requestAnimationFrame(update);
}
Sign up to request clarification or add additional context in comments.

Comments

0

As pointed out in the comments, you only call moveCall once, but you probably want to do it after each key press:

$(document).on('keydown keyup', function(e){
    GlobalVar.pressedKeys[e.keyCode] = true;
    moveCar();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.