|
|
|
#1 |
|
Actionscripter
Join Date: Jan 2004
Location: Montreal, Quebec
Posts: 128
|
Random movement direction
Hey all,
I was playing around with a big patch of code that I read about before in one of the other threads here. It basically made it so that a movie clip bounces back when it collides with the boundaries of the movie. Here is the complete code:
Now I am using a circle for my movie clip, and I was wondering why it always starts by moving in the same direction. I don't see any part of the code that specifies what angle it would start moving... Any help would be great, Thanks. |
|
|
|
|
|
#2 |
|
Senior Member
Join Date: Feb 2000
Location: Stockholm, Sweden
Posts: 15,137
|
This is the direction...
speedX = 15; // horizontal speed speedY = 15;
__________________
-Pelle Piano // Image Gallery www.studiobild.com // Photo Blog http://talesofthepixel.blogspot.com |
|
|
|
|
|
#3 |
|
Actionscripter
Join Date: Jan 2004
Location: Montreal, Quebec
Posts: 128
|
Yeah, but that also changes the speed of the ball, so isn't there a way to make it so that I can have a seperate set of coordinates for just the direction it will move?
|
|
|
|
|
|
#4 |
|
Senior Member
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
|
If you use sine and cosine, you can create a velocity for the ball at a particular angle and speed.
speed = 10; // set desired speed angle = random(360); // set desired angle speedX = Math.cos(angle*Math.PI/180)*speed; speedY = Math.sin(angle*Math.PI/180)*speed; In my own programs, I typically use the variable names vx and vy instead of speedX and speedY, to more clearly indicate that these represent velocity in a particular direction. If you'd like to figure out the current speed and angle from speedX, and speedY, there's a way to do that to (using Math.atan2 and Math.sqrt). |
|
|
|
|
|
#5 |
|
Actionscripter
Join Date: Jan 2004
Location: Montreal, Quebec
Posts: 128
|
I see, but do you mind explaining to my exactly what cosine and sine are? I've heard of them, just never really knew what they were used for
Thanks.
|
|
|
|
|
|
#6 |
|
Senior Member
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
|
Kind of hard to explain in a paragraph - but basically, they provide a way to convert a radius and an angle to an x,y position on the edge of a circle.
Given a circle whose center is cx,cy and whose radius is r, and given an angle a, then the position corresponding to that angle on the edge of the circle is given by x = cx+cos(a)*r y = cy+sin(a)*r some folks switch the cos/sin around, and may or may not negate the y value - it all depends on where you want to start on the circle and which direction you rotate it (clockwise or counter-clockwise). In the following movie, sine is shown as a blue line, and cosine as a green line. Drag the mouse around inside the circle to play with them. Sine/Cosine Illustration However, to use these functions, the angle, often expressed as a number from 0 - 360 degrees, must be converted to radians, which go from 0 - 2*PI. And yes, this has something to do with the fact that the circumfrence of a circle is equal to 2*PI*r). To convert degrees to radians, multiply by PI/180, which is what I did in my original example. I've left out a lot of info, which you will find in any trigonometry text, or you might want to search the Physics forum. - Jim Last edited by jbum; 09-20-2004 at 02:29 AM. |
|
|
|
![]() |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | |
|
|