Forums

ForumsProgramming Forum

AS2 Rotation Code Help

Posted Jun 2, '12 at 3:55pm

themontana

themontana

3 posts

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?

 

Posted Jun 2, '12 at 8:41pm

Annihalation

Annihalation

216 posts

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).

 

Posted Jun 2, '12 at 8:44pm

Annihalation

Annihalation

216 posts

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.

 

Posted Jun 5, '12 at 7:09pm

arobegamr

arobegamr

120 posts

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

 
Reply to AS2 Rotation Code Help

You must be logged in to post a reply!