I am trying to make a game that lets you fly spacecraft in 3d, using Newtonian physics. Going well so far, except I cannot seem to get the guided missile to work. Have tried a bunch of things and none of them work.
If I try and set rotation or set a transform (as per some examples I found), the missile locks in place and will not move (I have a feeling this may be related to how the docs say not to directly set things like the velocity every frame?) But, I am wanting the missiles to be a little slow at turning anyway (rather than perfectly track a target), so have been using add_torque( ) to try and steer the missile towards its target.
At first I tried converting to local coordinates and applying x and y torques, though the code got messy so I switched back to trying the, hopefully simpler, directly working with global coordinates. But in both cases, the missiles seem to in completely the wrong directions. Also, why is there not a "to global coordinates" and "to local coordinates" on everything? Transforms do give you more power, but are much harder to work with and make for much messier, harder-to-read code.
Some of the things I tried are below. Any suggestions would be appreciated. TIA.
target_heading = translation - target_object.translation
current_heading = global_transform.basis.z
target_heading = current_heading.normalized() - target_heading.normalized()
add_torque(target_heading * TORQUE)
and
if target_heading.x > 0:
add_torque(Vector3(-TORQUE, 0, 0))
elif target_heading.x > 0:
add_torque(Vector3(TORQUE, 0, 0))
if target_heading.y > 0:
add_torque(Vector3(0, -TORQUE, 0))
elif target_heading.y > 0:
add_torque(Vector3(0, TORQUE, 0))
if target_heading.z > 0:
add_torque(Vector3(0, 0, -TORQUE))
elif target_heading.z > 0:
add_torque(Vector3(0, 0, TORQUE))