# 2d navigation for physics-based car

i'm making a game, one npc is a car with spear in head, he try to hit the enemy (yellow pot in image) with the spear point.

here is the promble ,the car is physics-based , it has veloctiy ,max angular velocity ,can turn around the car's center, the npc can only change drive direction. since the positions of car and enemy can be very random (the enemy will not move once respawn), how can the car make the spear hit?

• is this an IA? just try with a regulation loop. if e is left, turn left, if e is right turn right. it will oscillate but many cases would eventually hit. It could end up rotating forever around e though. actually I think it's how I did in extreme carnage so you can see the result in action sourceforge.net/projects/extremecarnage Commented May 25, 2020 at 8:29
• You might find this past Q&A about maneuvering AI ships for a broadside attack useful. Here your goal state is different, you want to be facing in toward the target rather than matching velocity parallel to it, but the rest of the algorithm should work similarly. Commented May 25, 2020 at 11:44

There are a few ways to achieve that, here's what I found to be the easiest:

By saving the location of your npc and the center of the car you can calculate the direction using a vector (x,y). In this image, the direction you want to drive in is (c-a, d-b). If you don't have the direction that the npc is driving at, you can calculate it by saving the previous position and the current position and getting a vector the same way we did before.

Note: To make sure the vectors are the same length you can use this: $$Normalize (x,y) \rightarrow \sqrt{x^2+y^2} = 1$$

By the end you cant the two vectors to be the same (in the same direction). If you want your npc to have a smooth rotation towards the enemy, use a smooth variable and set it to a small value, then, add this value until you reach the direction you want.

Alternatively, instead of a vector you can create a linear line between these two points by using the linear line formula:

Linear line between two points (a,b) (c,d): $$y = mx + b \rightarrow y-a = \frac{d-b}{c-a}(x-b) , when - m= \frac{d-b}{c-a}$$

And then you want the "m" to be the same.

Note: If you'er using a game engine, try to find some useful functions that can do that for you (for example, "LookAt" in unity).