How do I predict where a RigidBody3D projectile will land in 3D?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By elacim

Hello!
For my 3D game project, I want to be able to predict where a projectile will land so I can display an indicator on that predicted position.
As a run-through of what I’ve tried:

  • simulate _physics_process
  • simulate a RigidBody3D with test_move/move_and_collide
  • followed a few cannon Unity tutorials which are supposed to simulate projectile motion (but none seem to give me the result I desire except for in 2D).

As none of these have worked, I’ve moved onto projectile motion equations. I would like to know if there’s either a way to fix my issues around the equations or if there is a better way to approach this problem. I would like to note that I am using PhysicsDirectSpaceState3D.intersect_shape(query) on a sphere with a radius of ~2, where I translate the query by the points as they’re calculated in order to figure out if it has collided, and then I use get_rest_info(query) to find the collision point.


Here is my current approach using these projectile motion formulas -
(v0=initial velocity Vector3, t=time/simulated time passed, and g=gravity)
x = v0.x * t * cos(theta),
y = v0.x * t * sin(theta) - 1/2 * g * t^2, and
z = ????
A few problems with this though is: I have no idea how to calculate the z nor how to get the theta.
I seriously do not know how to even approach calculating the z and after many hours of looking at kinematic equation explanations, I feel even more lost. So many people online say that the third dimension is easy to calculate but no one seems to explain how.
I am also unsure of how to actually find the theta in a 3D space. Would the theta be start position vector3.angle_to(launch velocity vector3)? Using the start_position as the local_position of the player (which is Vector3.ZERO) makes the angle 0 which obviously isn’t helpful. Would the Vector3 global_position.angle_to(to_global(launch velocity)) solve this?


I’m unsure of how to attach a project so I’ve tried to supply as much as possible here:
Player.gd script with the main projectile prediction script:
https://pastebin.com/GGnuGuBi (from a unity tutorial follow along)
https://pastebin.com/hurQiLSi (my current attempt)

Videos:
Video of the bug
2D version of this that I have working
Unity video I based my attempts off of

Thanks for your time :slight_smile: