dcsimg
A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: advanced player movement

  1. #1
    Member
    Join Date
    Jun 2009
    Posts
    44

    advanced player movement

    I have made a game where a ball goes in a random direction and a player(not you) has to go and "fetch" it. If you dont understand I this is what it is like below.

    (FETCHER) ball (goes in random direction past fetcher)

    instead of the fetcher just moving towards the ball which doesnt work unless the ball is going straight to the fetcher(the ball just bypasses them and the fetcher just runs after it), I want him to move sideways into the direction of the ball just like you would in real life in a cricket game or baseball etc.

    thankyou so much if you can help me.

    P.S. i am using flash 8 proffesional if that makes a difference.

  2. #2
    Senior Member Alluvian's Avatar
    Join Date
    Jun 2006
    Posts
    967
    Just to be clear, you are looking at code for a computer controlled AI of some sort to move to intercept a moving ball. Correct?

    The first thing I would try would be:
    1) construct the line that the ball is following
    2) use geometry to find the shortest distance from a point to a line (can be found in google or wiki) where the player is the point and the line is the path of the ball. Lets call the point where this shortest path, and the path of the ball intersect the 'intersect point'
    3) Start moving the player to the intersect point.
    4) If the ball passes the intersect point before the player arrives at that point, then path the player directly to the ball.

    This is not the OPTIMAL path. I am not sure how to get the BEST path. This will also only make sense if the ball is getting closer to the player. If the ball is heading away from the player then the player should probably just path to the balls current location.

    I am sure there is a way to calculate the optimal interception angle based on the ball speed and the player speed, but I don't know how to do that off the top of my head. A quick google search isn't getting it for me either. Must be looking up the wrong terms.

  3. #3
    Member
    Join Date
    Jun 2009
    Posts
    44
    ok, thanks very much this has helped me a lot, but just to make things simpler, i know that the shortest distance would be perpendicular to the line but is there any way to turn this into a direction (degrees)?

  4. #4
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,227
    If you know coordinates of 2 points you can always convert it to distance and direction.

    Lets say p1 is starting point and p2 is destination point. Find distance from p1 to p2:

    var dx = p2.x - p1.x;
    var dy = p2.y - p1.y;
    var dist = Math.sqrt(dy * dy + dx * dx);

    Find angle in radians:
    var angRad = Math.atan2(dy, dx);

    Convert to degrees:
    var ang = angRad * 180/Math.PI;

  5. #5
    Member
    Join Date
    Jun 2009
    Posts
    44
    ok. thankyou very much, this has also helped me a great deal. I probably should have explained this a bit better. I only know the angle of the line and the position of "thing" that moves towards it. I come up with some extremely simple code but it will only work for one way.
    you guys have probably already figured this out but just in case, the angle of the line + 90 is perpendicular to it and the direction that the thing needs to move in. but what if the thing is on the other side of the line, it will start to move away from it.
    your help is greatly appreciated.

  6. #6
    Senior Member Pazil's Avatar
    Join Date
    Sep 2006
    Location
    Ontario, Canada
    Posts
    913
    If you just need the cosines and sines, it's a lot faster just doing:

    var dx = p2.x - p1.x;
    var dy = p2.y - p1.y;
    var dist = Math.sqrt(dy * dy + dx * dx);

    var cos = dx / dist;
    var sin = dy / dist;

    Math.atan2 is pretty slow compared to just 2 divisions...

    P.
    WIP-ZOMBIES

    I love vegetarians! More meat for the rest of us!

  7. #7
    Member
    Join Date
    Jun 2009
    Posts
    44

    advanced player movement

    Hello All,
    I have started making a bat and ball type game where you hit a ball and a computer player gets it. What I want to know is the angle of the shortest path from the point of the player to the path of the ball (I found out tha it will be perpendicular to the path of the ball).
    The things that I know is the angle of what the ball is travelling and the point of the player.
    Any help is greatly appreciated. Thankyou all so much.

  8. #8
    Senior Member Alluvian's Avatar
    Join Date
    Jun 2006
    Posts
    967
    A quick google for information on finding the shortest distance from a point to a line gave me this:

    http://www.worsleyschool.net/science...t/method1.html

    This example gives you the equation of the line 'd', (which includes the slope of the line which can give you the angle of the line = arctan(slope)).

    And this method also shows how to solve for the intersection which you will need to know, because once the ball passes the intersection point you then have to change the path of the player to start directly chasing the ball instead.

  9. #9
    Member
    Join Date
    Jun 2009
    Posts
    44
    thankyou your help is appreciated but i am only 14 and dont really understand the math involved in it. is there any way you could explain it or is there a tutorial floating around the place?

Tags for this Thread

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