I need to create something of a virtual town, it will be from the perspective of above looking down (GTA 1 style) in 2D. On one layer i have the buildings etc and on a layer beneath that i want some people to randomly be moving around.
I have some script in a controller mc that controls some random movement using three frames:
frame1:
code:
// set number of steps to reach target
loops = 0;
// create a random target
_root.target_x = Math.random()*550;
_root.target_y = Math.random()*400;
// find the difference and divide by 20 : will be distance of each step
_root.xdiv = (_root.target_x-_root.circle._x)/20;
_root.ydiv = (_root.target_y-_root.circle._y)/20;
// increment loop and step again distance of single step
loops++;
_root.circle._x += _root.xdiv;
_root.circle._y += _root.ydiv;
// if 20 steps then we are at target so set a new one in Frame 1
// if not then go back to frame 2 and step again
if (loops<20) {
gotoAndPlay(2);
} else {
gotoAndPlay(1);
}
This works okay but for realism i need some changes, firstly as the mc will be a small human i need for them to be facing the direction they are travelling. I presume this will be worked out using trajectories or something but i'm useless with maths.
I also would like for them to have a set speed rather than using a set number of steps, they will move at this speed to the target until they reach it at which point a new target is set.
would really appreciate some help on this, thanks.
i guess the trajectory will be worked out using the starting _x and _y position and the target _x and _y position and then using _rotation(?) to rotate the mc to the correct angle. As i say though i'm no good with maths so i'm not sure what equations i should be looking for.
Your on the right track, but you need to split the movie clips stage into quads, rotation works like 0=90 = top right, 90-180 = bottin right, -180 - -90 = bottom left, -90-0 = top left. So by splitting those into 4 quads you add or subtract 90 from your degress to get the right direction.. You can apply the code I gave to you to a movie clip and it will rotate it to the angle of the mouse.
// set number of steps to reach target
loops = 0;
// create a random target
_root.target_x = Math.random()*550;
_root.target_y = Math.random()*400;
// find the difference and divide by 20 : will be distance of each step
_root.xdiv = (_root.target_x-_root.circle._x)/20;
_root.ydiv = (_root.target_y-_root.circle._y)/20;
/***********************************************/
// work out the angle of motion
xdiff = _root.target_x-_root.circle._x;
ydiff = _root.target_y-_root.circle._y;
xsquare = xdiff*xdiff;
ysquare = ydiff*ydiff;
distance = Math.sqrt(ydiff+xdiff);
angle = Math.atan(ydiff/xdiff);
degrees = angle*180/Math.PI;
/***********************************************/
// work out whether mc moving up-right
if ((_root.target_x>_root.circle._x) && (_root.target_y>_root.circle._y)) {
degrees = degrees+90;
}
// work out whether mc moving down-left
if ((_root.target_x<_root.circle._x) && (_root.target_y<_root.circle._y)) {
degrees = degrees-90;
}
// work out whether mc moving down-right
if ((_root.target_x>_root.circle._x) && (_root.target_y<_root.circle._y)) {
degrees = degrees+90;
}
// work out whether mc moving up-left
if ((_root.target_x<_root.circle._x) && (_root.target_y>_root.circle._y)) {
degrees = degrees-90;
}
// rotate the mc
_root.circle._rotation = degrees;
Hi.
It looks like both of your problems can be solved with trigonometry. As I explain some stuff, I'll assume that you have at least a rough understanding of algebra. To make your characters face the direction they're walking, you can use the Math.atan2 method to find the angle in radians that your character should be facing, based on his x and y speed. So your code would look something like this:
_root.circle.radians = Math.atan2(_root.ydiv,_root.xdiv)
_root.circle._rotation = _root.circle.radians*180/Math.PI;
The *180/Math.PI is needed because all math methods use radians instead of degrees. There are 360 degrees in a circle and 2*pi radians in a circle so the ratio is 360/(2*pi) which simplifies to 180/pi.
You can also use a set speed pretty easily. I'll leave assigning the speed up to you and make it _root.circle.speed You'll also need to find the distance between the person's current position and their destination. Do this using the pythagorean theorem (A^2+B^2=C^2) where A is xdiv and B is ydiv. C will be the direct distance between the two. Now you can determine the number of loops you'll need:
maxLoops = C/_root.circle.speed;
To make your character move each time you go through the loop, use Math.cos and Math.sin. Here's a great example of the two, made by jbum http://www.krazydad.com/bestiary/circle.swf where sine is blue and cosine is green. Play with it for a while to get a feel for how they work and how inputting an angle will effect the resulting cosine/sine values.
Now to make your character move...
circle._x += Math.cos(circle.radians)*circle.speed;
circle._y += Math.sin(circle.radians)*circle.speed;
Neal
[edit] Oops. I started writing this, went to the store, and finished it when I got back. You should still find the info in here valuable anyway. The characters will actually face exactly the direction they're walking with this instead of being rounded off into quarters.[/edit]