# Thread: Moving in rectangular coord with vehicle via user clicks

1. ## 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. 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. Originally Posted by tabstop
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. And subtracting the angle that you are currently facing doesn't work because?

5. 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. Originally Posted by tabstop
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.

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

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