Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with plotting x, y! 1

Status
Not open for further replies.

banditburg

Programmer
Apr 23, 2002
17
0
0
GB
I have seen on many occansions an object that can move, rotate, then move in that direction. I have tryed to recreate this illusion in my work and have found it almost impossible to make my object look as if it moves in the correct direction.

Eg. An object of a rotation of 23, every time it moves it has to work out how far to move on the x axis and how far to move on the y axis. How is this done??

Any help will be creatly appriciated!
 
Sounds like some good ole Math may be needed. Angles, radians, Sin, Cos. etc...

Else you might try a nested looping movie Clip that follows
a motion guide. rotate the MC then begin the motion.

eSearing.com
version 2.something now available
 
My program IS on a looping movie. It's hard enough.

You talk about these 'good ole math' - can you tell me which ones to use. I have looked in Flash Script Dictionary and found some intresting looking functions. Which ones could help me, and how could I use them?
 
if you do have the chance download the latest flash mx trial. There are a bunch of samples that come with it that are filled with mathematical functions that would help you out for that kind of stuff.

One of their examples is a beetle that turns in the direction you want it to move. Plenty of other beetle anymations show you how to do different things too.

Despite being flash mx I think the examples will still work with older flash versions.

I hope this helps. Gary Haran
 
There are two parts to your problem - turning the clip and then moving in the direction of the turn. One part is very easy, one is slightly harder.

1 - rotating the object, sounds like you have this nailed already but in case you don't there's a _rotation property which takes a value in degrees and rotates your object by that amount eg...

_rotation=50;

2 - moving in the new direction is done by working out the horizontal and vertical motion vectors and adding them to the position of your object on the stage. Basically the cosine of the _rotation angle is your horizontal movement and the Sine gives you the vertical. Because Flash does all of it's trionmetrical stuff in radians, not degrees, you have to figure in some conversion:

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

This will give you a better idea if you attach it to a clip:

onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT)) {
_rotation++;
}
if (Key.isDown(Key.LEFT)) {
_rotation--;
}
speed = 2;
_x += Math.cos(Math.PI/180*(_rotation-90))*speed;
_y += Math.sin(Math.PI/180*(_rotation-90))*speed;
}

The -90 value is important or else everything works at right angles to what you expect. Slainte

 
Thanks loads! I've got my program working fine now.

That coding is really helpful.

This makes lots of sense.
Code:
_x+=Math.cos(Math.PI/180*(_rotation))*speed;
_y+=Math.sin(Math.PI/180*(_rotation))*speed;
I just didn't know how to use functions like this!

Thanks for enligtening me!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top