I am creating a small demo game where you can rotate around earth and launch satellites into space. But I have some trouble with the calculations.
You can drag the mouse from the platform to a direction. This is the direction you shoot the satellite to. Because the camera is rotated around the planet, up isn't the same as forward. For the direction of the satellite, I need a Vector3 (direction/velocity).
So data I have is the forward of the platform on screen and the mosue drag direction.
So When the user drags it to (-0.7, 0.7) it means the satilatie launch direction should be (0, 0, 1). The global/World Forward direction.
So how can I translate those 2d screen position and direction to the world direction?
PlayCanvas has a very useful function we could make use of. The implementation is as follows:
We can use this function to convert the start and end points (
AandBrespectively in the diagram) of the mouse drag line to 3D lines in world space. After the conversion we must subtract the camera's world position from the two projected points, and normalize the resulting vectors.[The
zparameter is irrelevant for this purpose because we are only interested in a direction vector and not an actual point, so just set it to e.g. 1. ]So what does this give us? A plane spanned by these two vectors:
There are three criteria that the velocity direction must satisfy:
Let:
AandBproject to directional vectorsUandVrespectively.The surface normal at the launch site (the "up" direction as seen by a person standing there) be
N:Where
(ψ, φ) = (lat, long).To visualize this:
EDIT:
Small mistake in the second diagram:
U×Vshould beV×U, but the expected resultN×(U×V)is still correct.Note that
UxVis not necessarily perpendicular toN. When it is parallel toN, the blue plane "scrapes" the surface, i.e. the green lineABis tangent to the Earth's surface as per rendered on-screen, at the launch site.