-
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:
code: onClipEvent (load) {
// when the clip loads, give it the folloing properties
speedX = 15;
// horizontal speed
speedY = 15;
// vertical speed
score1 = 0;
// score for player 1 is 0
score2 = 0;
// score for player 2 is 0
}
onClipEvent (enterFrame) {
// when a new frame is reached ...
// remeber that the x-axis is horizontal
this._X += speedX;
// give it horizontal velocity X
this._y += speedY;
// give it vertical velocity Y
if (this._X>=485) {
// if the mc reg point is at or equal to 485
speedX = -speedX;
// reverse velocity X
} else if (this._X<=15) {
// else if mc reg is at or equal to 15
speedX = -speedX;
// reverse velocity X
}
// remeber that the y-axis is vertical
if (this._Y>=285) {
// if mc reg is at or equal to 285
speedY = -speedY;
// reverse velocity Y
} else if (this._Y<=15) {
// else if mc reg is at or equal to 15
speedY = -speedY;
// reverse velocity Y
}
}
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.
-
Senior Member
This is the direction...
speedX = 15;
// horizontal speed
speedY = 15;
-
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?
-
Senior Member
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).
-
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.
-
Senior Member
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 01:29 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|