Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 01-22-2008, 03:44 AM   #1
XaeroDegreaz
www.smartsocket.net
 
Join Date: Apr 2007
Posts: 150
Send a message via MSN to XaeroDegreaz Send a message via Yahoo to XaeroDegreaz
Question Move movieClip towards rotation angle

Hi, I'm trying to move a movieClip based on it's _rotation property.

I have a little triangle that could represent a spaceship perhaps.

If the triangle is facing up (0 degrees) I want it to move straight up when I press the UP key. If it is facing 45 degrees, I want it to move forward in that direction.

Could someone help me with the math involved in moving a movieClip towards its rotation angle?

Thanks in advance.
XaeroDegreaz is offline   Reply With Quote
Old 01-22-2008, 03:52 AM   #2
Durnus
Senior Member
 
Join Date: Mar 2007
Posts: 257
Default

Okay, I'll give this a shot. Pretty simple, I just get my directions messed up a lot

Someone can fix this if they want to, but this is basically it:

Code:
//Let's assume your movieclip is called MC, and you changed _rotation to change your object's angle.

//Say, this var represents distance in pixels.
var SPEED = 5;

//It would be one pair of these two lines, I'm not sure which:

MC._x += Math.cos(MC._rotation)*SPEED;
MC._y += Math.sin(MC._rotation)*SPEED;
//or
MC._x += Math.cos(MC._rotation/180*Math.PI)*SPEED;
MC._y += Math.sin(MC._rotation/180*Math.PI)*SPEED;
But, just so you know:
Up is not zero degrees. Right is. I think down is 90, and it goes around that way, but I'm not sure.

I hope my code helped, it probably won't work, but at least it's something.
Durnus is offline   Reply With Quote
Old 01-22-2008, 03:59 AM   #3
XaeroDegreaz
www.smartsocket.net
 
Join Date: Apr 2007
Posts: 150
Send a message via MSN to XaeroDegreaz Send a message via Yahoo to XaeroDegreaz
Default

Yeah, I kinda have something like that.

I can tell it's trying to work, because the triangle does change directions based on angle. However, it doesn't go the correct way
XaeroDegreaz is offline   Reply With Quote
Old 01-22-2008, 04:02 AM   #4
ASWC
Super Moderator
 
ASWC's Avatar
 
Join Date: Dec 2007
Location: Greenville, SC
Posts: 6,528
Default

speedx =Math.sin(this._rotation*(Math.PI/180))*2;
speedy = Math.cos(this._rotation*(Math.PI/180))*2*-1;


this._x+=speedx*speed;
this._y+=speedy*speed;

Use what you want as a "speed" variable.
__________________
aswebcreations
Super Duper!
ASWC is offline   Reply With Quote
Old 01-22-2008, 04:04 AM   #5
XaeroDegreaz
www.smartsocket.net
 
Join Date: Apr 2007
Posts: 150
Send a message via MSN to XaeroDegreaz Send a message via Yahoo to XaeroDegreaz
Default

You the man brotha. That's exactly what I needed!

Thanks a lot
XaeroDegreaz is offline   Reply With Quote
Old 01-22-2008, 04:09 AM   #6
ASWC
Super Moderator
 
ASWC's Avatar
 
Join Date: Dec 2007
Location: Greenville, SC
Posts: 6,528
Default

Hey Durnus, just so you know the _rotation in Flash output values from -180 to 180, instead of from 0 to 360, so when writing function for that you have to translate everything into values you can use.
__________________
aswebcreations
Super Duper!
ASWC is offline   Reply With Quote
Old 01-22-2008, 04:13 AM   #7
Durnus
Senior Member
 
Join Date: Mar 2007
Posts: 257
Default

I didn't know that, but cos(180+90) would output the same out cos(-90), correct? (Assuming we are using degree mode.) So therefore, how did you know I didn't know it was from -180 to 180? :P

You did catch the - for the y. I didn't get that, I always debug the signs after I write the whole thing. I never get stuff right the first time.
Durnus is offline   Reply With Quote
Old 01-22-2008, 04:17 AM   #8
ASWC
Super Moderator
 
ASWC's Avatar
 
Join Date: Dec 2007
Location: Greenville, SC
Posts: 6,528
Default

I just do a lot of stuff like that in Flash. I knew you didn't know since your code couldn't work but was close enough.
In fact if you add 180 then you have a 0 to 360.
__________________
aswebcreations
Super Duper!
ASWC is offline   Reply With Quote
Old 01-22-2008, 04:20 AM   #9
XaeroDegreaz
www.smartsocket.net
 
Join Date: Apr 2007
Posts: 150
Send a message via MSN to XaeroDegreaz Send a message via Yahoo to XaeroDegreaz
Default

Now I need to come up with something clean for acceleration and deceleration.

-_- Man, I hate math.

Most I do in math is count grams and ounces.
XaeroDegreaz is offline   Reply With Quote
Old 01-22-2008, 04:21 AM   #10
ASWC
Super Moderator
 
ASWC's Avatar
 
Join Date: Dec 2007
Location: Greenville, SC
Posts: 6,528
Default

By the way I can tell you are doing a Asteroid clone, am I right?
__________________
aswebcreations
Super Duper!
ASWC is offline   Reply With Quote
Reply


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 Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
move movieclip saravanan ActionScript 2.0 0 06-01-2007 11:08 AM
goto ...where? Haso2000 ActionScript 1.0 (and below) 7 03-27-2007 04:27 PM
making a character move in an angle connman ActionScript 2.0 2 10-28-2005 01:44 AM
move movieclip to random locations monkeybrainz ActionScript 1.0 (and below) 1 08-02-2004 12:46 PM
How to move a MovieClip to another MovieClip? Didd ActionScript 1.0 (and below) 12 06-29-2003 05:15 PM


All times are GMT. The time now is 07:02 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, 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.