Thread: Moving in rectangular coord with vehicle via user clicks

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    16

    Moving in rectangular coord with vehicle via user clicks

    I am trying to set up a program where I have a car and I want to make it move wherever the user decides. For now, I only have text printing out telling me what is happening to make sure I can do the movement.

    For now, I am using rectangular coordinates and I want to move the robot to a destination. First, it turns to its destination, then moves forward till it hits the destination. Is there a common algorithm for deciding how to turn turn in rectangular coordinates? Currently, I just have a lot of if statements and I would think this is a common enough problem.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    tan theta = m, where theta is the angle with the x-axis (you don't say which direction you're facing) and m is the slope of the line from you to the target. So you will need to take account of which quadrant you're heading to (NE, NW, SE, SW).

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    16
    Quote Originally Posted by tabstop View Post
    tan theta = m, where theta is the angle with the x-axis (you don't say which direction you're facing) and m is the slope of the line from you to the target. So you will need to take account of which quadrant you're heading to (NE, NW, SE, SW).

    I'm currently using something like that now. I just find the angle to get from one coordinate to another, but the difficulty lies in turning with respect to where it currently is. I just went through every possibility using if statements, but it seems like there's an easier way to do this. It works, but I'd like to use as few instructions as possible.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And subtracting the angle that you are currently facing doesn't work because?

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    For 2D:

    Code:
    //Compute dist to target
    float diffx = (player.x - target.x);
    float diffy = (player.y - target.y);
    float total_dist = sqrtf( ( diffx * diffx) + (diffy * diffy) );
    
    //Normalize diffx and diffy
    float norm_x = diffx / total_dist;
    float norm_y = diffy / total_dist;
    
    //Final z rotation and 2D player movement vector
    float z_rot = atan2f(norm_y / norm_x);
    player.move.x  = norm_x;
    player.move.y = norm_y;

  6. #6
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by tabstop View Post
    So you will need to take account of which quadrant you're heading to (NE, NW, SE, SW).
    atan2 takes care of that for you.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    16
    thanks for all the help. The main problem was whether there was a common way to store the relative angle, but I'll just stick with all the if statements. Time to look into ascii art to get this graphically.

  8. #8
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    Are there any obstacles that the vehicle encounters along the way? Doing a straight line path is great and all....but in most games it wont work.

    Check out Dijksta's algorithm or the A* algorithm (A* is actually just Dijkstra's algorithm with a heuristic involved).
    My Website

    "Circular logic is good because it is."

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    16
    I was planning on doing something small (ie ascii art vs. a real gui) and building up. I'll likely add obstacles and work with those two algorithms.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. open explorer to dir when user double clicks
    By willc0de4food in forum Windows Programming
    Replies: 4
    Last Post: 04-18-2006, 01:26 AM
  2. Balloon tip question: Trap when the user clicks on "X"
    By Joelito in forum Windows Programming
    Replies: 4
    Last Post: 11-18-2005, 10:35 PM
  3. Instant messenger app help
    By AusTex in forum C Programming
    Replies: 2
    Last Post: 05-01-2005, 12:41 AM
  4. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM