ForumsProgramming ForumAS2 Rotation Code Help

3 6387
themontana
offline
themontana
3 posts
Nomad

Hi!
I am Montana.
For 4 years, I have been looking for a rotation code. This code will allow you to walk in different directions in a Virtual World.
I have found it, but it is laggy.
Do any of you know the code?

  • 3 Replies
Annihalation
offline
Annihalation
479 posts
Nomad

If you want to make a character move in the direction they are facing in a 2D world from a topdown perspective, you can use basic trigonometry to calculate the amount that they should move in the x/y directions based on their rotation. The Pythagorean theorem does the trick.

this._y = this._y + (speed*sin(rotation*(3.14159/180)));
this._x = this._x + (speed*cos(rotation*(3.14159/180)));

Flash stores it's rotational data in radians, which need to be converted into degrees (hence the Pi/180 part of the formula).

Annihalation
offline
Annihalation
479 posts
Nomad

And I think I did that wrong, I'm kinda positive its going to be 180/pi , and not pi/180. Sorry for double post.

arobegamr
offline
arobegamr
130 posts
Nomad

Depends on what you want.

If you want to use the mouse:

player.radians = Math.atan2(_ymouse-player._y,_xmouse-player._x);
player._rotation = player.radians*180/Math.PI;
player._x += Math.cos(player.radians)*player.speed;
player._y += Math.sin(player.radians)*player.speed;


Otherwise, you can just use the conversion from degrees to radians from the rotation:

player.radians = player._rotation/180*Math.PI;
player._x += Math.cos(player.radians)*player.speed;
player._y += Math.sin(player.radians)*player.speed;


And a clarification on conversions:
Degrees to radians: Divide by 180 Multiply by PI
Radians to degrees: Multiply by 180 Divide by PI

Showing 1-3 of 3