I'm trying to have a character walk along a plank (a long, thin rectangle) that works like a seesaw, being rotated around a central point by box2d physics (falling objects). I want the left and right arrow keys to move the player up and down the plank, regardless of it's slope, and I don't want to use real physics for the player movement.

My idea for achieving this was to compute the coordinate based on the rotation of the plank and the current location "up" or "down" the board.

My math is derived from here: https://math.stackexchange.com/questions/143932/calculate-point-given-x-y-angle-and-distance

Here's the code I have so far:

movement = 0;
if(keys[37]){ // Left
    movement = -3;

if(keys[39]){ // Right
    movement = 3;

// this.plank is a LimeJS sprite.
// getRotation() Should return an angle in degrees
var rotation = this.plank.getRotation(); 

// this.current_plank_location is initialized as 0
this.current_plank_location += movement;

var x_difference = this.current_plank_location * Math.cos(rotation);
var y_difference = this.current_plank_location * Math.sin(rotation);

this.setPosition(seesaw.PLANK_CENTER_X + x_difference, seesaw.PLANK_CENTER_Y + y_difference);

This code causes the player to swing around in a circle when they are out of the center of the plank given a slight change in rotation of the plank.

Any ideas on how I can get the player position to follow the board position?


1 Answer 1


One issue is that Math.cos/Math.sin take radians, not degrees. E.g.,

> Math.cos(Math.PI)
> Math.cos(180)

So, you're going to have to convert the rotation:

rotation = rotation * (Math.PI/180);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .