Home Tutorials Forums Articles Blogs Movies Library Employment Press

 ActionScript.org Forums [AS2] Angles, Sine, and Cosine
 Register FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read

 03-13-2010, 08:56 AM #1 MaximBonne Registered User   Join Date: Dec 2008 Posts: 7 [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. Code: ```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.
 03-13-2010, 10:44 AM #2 asgrunt 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; }```
 03-13-2010, 05:47 PM #3 MaximBonne 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.

 Thread Tools Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home ActionScript Forums Group     ActionScript 3.0     ActionScript 2.0     ActionScript 1.0 (and below)     Simple Stuff (Newbies) Flash General Questions     Flash 10 General Questions     Flash 9 General Questions     Flash 8 General Questions     Other Flash General Questions Flex     Flex 2, 3 & 4     Flex 1 Extensions and Plugins     Components     JSFL - Extending Flash Desktop, Mobile and non-browser Environments     AIR (Apollo)     FlashLite / Portable Devices Development     Projectors and CDs Supporting Technologies     HTML and JavaScript     haXe     Server-Side Scripting     Flash Remoting     Flash Media Server General     Best Practices     Gaming and Game Development     Animation and Effects     Flashants Support Forum Community Boards     General Chat     Just for Kicks Challenges     Detention Flash In Action     Site Check     Cool Sites     Widgets Decommissioned     Projects and Positions CMS Forums     Announcements Board     Content Postings / Updates     Product Review Requests     CMS Technical Questions     Process Questions     Collaboration & Suggested Articles

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

///