Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

How to control a kinematic body with the keyboard in box2d processing ?

$
0
0

Hi/Bonjour,

I'm trying to control a kinematic body with WASD keys to have it move around the screen like in a 2d game. Here is my mover class. Right now something is missing, but the "move" vector is being modified by the WASD key press as shown by println(). The problem remaining is that somehow the force is not being applied to the body or maybe box2d doesnt know about it.

void mover() {

Vec2 move = new Vec2(0, 0);
Vec2 pos = body.getWorldCenter();

if (key == CODED) { if (keyCode == UP) { move.y -= 10; } if (keyCode == DOWN) { move.y += 10; } if (keyCode == LEFT) { move.x -= 10; } if (keyCode == RIGHT) { move.x += 10; } }

if ((keyPressed == true) && ((key == 'w') || (key == 'W'))) { move.y -= 10; } if ((keyPressed == true) && ((key == 's') || (key == 'S'))) { move.y += 10; } if ((keyPressed == true) && ((key == 'a') || (key == 'A'))) { move.x -= 10; } if ((keyPressed == true) && ((key == 'd') || (key == 'D'))) { move.x += 10; }

println("move = " + move.x + "," + move.y);

body.applyForce(move, pos);

}

Of course, I call this mover() in draw(). I also let box2d know of this body (with properties) thru makeBody(). Right now, no errors pop up but again something is missing.

Thank you for your time ^^


Viewing all articles
Browse latest Browse all 2896

Trending Articles