# Plotting entities on a radar

I'm trying to build a radar system like the one in the original X-Wing games. The way it works is that there are two circular radar systems, one for behind the ship, and one for infront.

So, I've written a class to represent the radar, it has a vector representing the direction the radar is facing, and a 3D position of where the radar is. Both of these things will change in realtime as the ship moves and so the radar will need updating each frame.

Now I'm trying to figure out how to transform the position of another ship onto the radar. What I need is a final 2D X/Y coordinate between -1,-1 and 1, 1 for plotting on the 2D radar screen.

Given two vectors (the direction of the ship - which the radar is facing - and the direction to the ship we are plotting), how can I calculate a 2D X-Y position on the radar? Obviously I also have access to the up and right vectors of the ship too.

My instinct says that this should be relatively straightforward, but my math skills suck.

-

Well, There is probably many ways to do this. But this is how i would do it:

1 : Get the dot product of the ship. this way we can determin if the ship is actualy infront of the player. also saving this value beacuse we are going to transform it into radians.

2 : if the ship is in front of the player, get the length of the vector. ( from player to ship )

3 : now just rotate an at vector around the up vector with the dot radians. normalize that vector and scale it by the distance. You will need to just determin if it´s on the left or right side.

So that in code would be something like this:

``````float3 v1 = playerPos - shipPos;
float radians = angel * ( 180.0f / 3.14f );
float length2 = dot(v1,v1);
float3 rotVec = RotateVector( playerCopyAt, playerUp, radians );
rotVec = normalize( rotVec ) * sqrt( length2 );

rotVec.xy * 2.0f - 1.0f
``````

Some thing like that, just wrote this from my mind so i havent actualy tested the code, but the idea will probably do.

-

As is typical, about 10 minutes after posting this question I realized that "planes" are actually a thing and this is pretty easy...

To find the position on the X axis, I can do this:

1. Project the ship position onto the XZ plane
2. Get the direction to the ship (e.g. (ship_pos - radar_pos).normalize())
3. Calculate the angle between the radar's forward direction and this direction
4. X = (angle_in_degrees / 90.0) * plane_classification (where plane_classification is -1 if the point is to the left of the YZ plane, 0 if it's on the plane, and 1 if it's to the right of it)

You can then work out the Y-axis position by reversing the planes in steps 1 and 4.

-