"Walking" along a rotating surface in LimeJS

Posted by Dave Lancea on Game Development See other posts from Game Development or by Dave Lancea
Published on 2013-06-15T18:37:47Z Indexed on 2013/06/26 22:30 UTC
Read the original article Hit count: 288

Filed under:
|
|

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: http://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?

© Game Development or respective owner

Related posts about movement

Related posts about geometry