A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: random character movement

  1. #1
    Senior Member
    Join Date
    May 2004
    Location
    UK
    Posts
    200

    random character movement

    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;



    frame2:

    code:

    // increment loop and step
    loops++;
    _root.circle._x += _root.xdiv;
    _root.circle._y += _root.ydiv;



    Frame3
    code:

    // 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.

  2. #2
    Senior Member
    Join Date
    May 2004
    Location
    UK
    Posts
    200
    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.

  3. #3
    Senior Member
    Join Date
    May 2004
    Location
    UK
    Posts
    200
    i've adapted the first frame of as with this:

    code:

    /***********************************************/
    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) //That is the distance
    angle = Math.atan(ydiff / xdiff);
    degrees = angle * 180 / Math.PI;
    _root.circle._rotation = degrees;



    I'm getting some _rotation but not at the correct angle.. any ideas?

  4. #4
    Member
    Join Date
    Jul 2003
    Posts
    75
    Hey dude, try this

    Code:
    _root.person.onEnterFrame = function(){
    	mousePoint = new Object();
    	mousePoint.x = _root._xmouse;
    	mousePoint.y = _root._ymouse;
    	this.globalToLocal(mousePoint)
    	moveDeg = (((math.atan(mousePoint.y/mousePoint.x))*180)/math.PI);
    	if(mousePoint.x > 0 && mousePoint.y < 0){
    		rotate = 90 + moveDeg;
    	};
    	if(mousePoint.x > 0 && mousePoint.y > 0){
    		rotate = 90 + moveDeg;
    	};
    	if(mousePoint.x < 0 && mousePoint.y < 0){
    		rotate = -90 + moveDeg;
    	};
    	if(mousePoint.x < 0 && mousePoint.y > 0){
    		rotate = -90 + moveDeg;
    	};
    	
    	this.angle._rotation = rotate;
    }
    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.
    Plz help me

  5. #5
    Senior Member
    Join Date
    May 2004
    Location
    UK
    Posts
    200
    thanks for the input, not too sure how to apply that to my movie tho, heres my fla if you've got a minute to take a look.

    Thanks.
    Attached Files Attached Files

  6. #6
    Senior Member
    Join Date
    May 2004
    Location
    UK
    Posts
    200
    something that is confusing me is that my degrees traces are only returning values between -90 and 90

    does that seem right?

  7. #7
    Senior Member
    Join Date
    May 2004
    Location
    UK
    Posts
    200
    got it!

    full code for frame 1:

    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;

    /***********************************************/
    // 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;


    Last edited by seengee; 05-08-2005 at 03:56 PM.

  8. #8
    Bacon-wrapped closures Nialsh's Avatar
    Join Date
    Dec 2003
    Location
    Houston!
    Posts
    338
    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]
    Last edited by Nialsh; 05-08-2005 at 04:39 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center