# 2d indicator for turning a spacecraft in 3d space

This topic is 1416 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Maybe you guys can help me out. I have been trying this for days.

I have this 3D space game and want an indicator (2D arrow in the center of my picture) that points in the direction to turn my ship in 3D space. (Up, down, left, right) so that it always points to the shortest angle to the selected object, regardless whether my ship is facing the object or not.

I have the coordinates of the selected object and the player as well as the angle in which the player is facing. Basicaly;

Vector3 PlayerPos;

Vector3 SelectedPos;

Vector3 PlayerDirectionEuler; or even better Quaternion PlayerDirectionQuaternion;

I have tried so many things, asked and have had so many wrong answers, I can't even remember the 1000 things that didn't work.

The perfect result for me would be a floating point indicating the rotation of my angle in the Z axis.

Please someone give me a hand with this.... It looked so easy when I started but now I am desperate.

##### Share on other sites

I think this should work:

Do a basis change, to transform the "selectedpos" into the local space of your spaceship.

Then ignore the Z-value (or whichever value you map to the "forward" direction) of the point, and use X and Y and trig to find the angle.

In some more words:

If you have the basis vectors (right, up, forward) of your spaceships orientation, you could construct a matrix from it.

You then use this matrix to transform the point

Then just ignore Z-value of it, and use arctan2 (assuming c/c++) to find the angle from x and y-values of it.

If it is straight behind you, or in forward, you will get an undefined angle (x and y will be 0), but there isn't much you can do in this case.

Edited by Olof Hedman

##### Share on other sites

I can't remember exactly what the solution was but I know I ended up splitting the work.

I took the cross product of the direction vector (target position - player position) and the players right and left matrix.

This allowed me to decide if I had to turn left or right. ( smallest of the two angles)

I then repeated the process but using the players up and down vectors to decide if I needed to push the stick forward or pull it back.

I think that's how I ended up doing it.

##### Share on other sites

May I recommend perhaps using a 3D indicator? Something along the lines of a little ball with a point on one end at the bottom of your UI (similar to a navball), maybe. A while back I ran into this same problem for a similar type game, and found it far easier to rotate a 3D object to face the direction I wanted, but in addition to that, it made the direction a lot clearer to the player.

To note:

This method would be less helpful for say, the direction enemy shots have hit your ship, since there could be simultaneous hits (in which case indicator arrows on the edges of your screen are likely the better option). But, for the direction you need to head towards, I found it to be a fine solution.

Edited by Misantes

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 15
• 14
• 46
• ### Forum Statistics

• Total Topics
634059
• Total Posts
3015291
×