Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 03-13-2010, 08:56 AM   #1
Registered User
Join Date: Dec 2008
Posts: 7
Default [AS2] Angles, Sine, and Cosine

I am having some trouble with a script of mine. Basically, what I want to do is turn a direction, and a distance in that direction into a distance in x and y that a movieclip would need to cover to get to that point. I tried something that I figured would work, but it just simply doesn't.

var facingAngle:Number = 45;
var Dist:Number = 10;
onEnterFrame = function (){
xStep = Dist * Math.cos(facingAngle);
yStep = Dist * Math.sin(facingAngle);
myMC._x += xStep
myMC._y += yStep
With this, I figure it should be moving at a distance of 10 at a 45 degree angle every frame. x/yStep=10*cos(45). It seems simple enough to me. Instead, regardless of what I put in, it seems to go off in random directions, with no real rhyme or reason. It most obviously doesn't work with numbers like 180.

I concocted this script under the assumption that I could get the Opposite and Adjacent measurements through the equation
O=h*sin(a) and A=h*cos(a) but I seem to be completely off track. I also seem to remember that sin and cosine won't work past 90 degrees or something, and as such, aren't suited for flash's (immensely annoying) -180 through 180 rotation measurements.

Basically, what I want to use this for is an Asteroids type game, where movement direction is determined by the angle of the ship. I planned to do this through a set hypotenuse (the distance of a 'step', or the distance I wanted the ship to move each frame. acceleration ignored for the sake of this thread because that's not part of the problem) as a benchmark for determining how far along x and y it'd have to move to get that far at it's current angle.

Obviously it's possible and I'm just doing it wrong. Anyone have any suggestions? I've worked with trig in before (atan2 is annoying as hell) and I'm not particularly excited about that part of working on this little game, but it's something I feel like doing.

hopefully someone here can help me, and help my computer before it ends up sitting outside my window traveling via the most direct route it can take.
MaximBonne is offline   Reply With Quote
Old 03-13-2010, 10:44 AM   #2
Senior Member
Join Date: May 2009
Posts: 177

Flash needs radians but the value of 45 is in degrees. Try something like this:

ActionScript Code:
var nAngle:Number = 45; var nSpeed:Number = 10; ship._rotation = nAngle; ship.onEnterFrame = function(){     var nVectX:Number = Math.cos(nAngle * (Math.PI/180)) * nSpeed;     var nVectY:Number = Math.sin(nAngle * (Math.PI/180)) * nSpeed;     this._x += nVectX;     this._y += nVectY; }
asgrunt is offline   Reply With Quote
Old 03-13-2010, 05:47 PM   #3
Registered User
Join Date: Dec 2008
Posts: 7

That worked like a charm. It's embarassing, I knew about the use of radians in flash but for some reason it never occured to me that they would apply to this.
MaximBonne is offline   Reply With Quote

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT. The time now is 04:11 PM.

Follow actionscriptorg on Twitter


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2015, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.