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.
Answer
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:
- Project the ship position onto the XZ plane
- Get the direction to the ship (e.g. (ship_pos - radar_pos).normalize())
- Calculate the angle between the radar's forward direction and this direction
- 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.
No comments:
Post a Comment