I'm trying to do a spaceship movement much in the same way of the old game Descent. For reference:
In short, the ship has full "6 degrees of freedom": it can move in 3 axes and roll around 3 axes. Plus some auto-corrections to make it easier to handle.
I can see several ways of doing this, but I'm not sure which way to go.
1- Using a KinematicBody.
I'm positive this will require using Quaternions, which I admit is intimidating me. I gave it a little go, using the same node structure that I use for FPS controllers, and Euler angles:
Kinematic
Spatial (roll)
Spatial (yaw)
Camera (pitch)
... other nodes
But I still had problems of the sort that when you turn the ship upside down, the controls for left and right get inverted.
2- Using a RigidBody
and applying forces to it. I gave it a little go as well, but then colliding on walls makes my ship rotate out of control. On top of that, I tried using the mouse to control the yaw and pitch (using event.relative
values) and the ship gets skewed pretty easily. This is how I did it so far:
func _physics_process(delta: float) -> void:
if Input.is_key_pressed(KEY_W): apply_central_impulse(-transform.basis.z)
if Input.is_key_pressed(KEY_S): apply_central_impulse( transform.basis.z)
if Input.is_key_pressed(KEY_A): apply_central_impulse(-transform.basis.x)
if Input.is_key_pressed(KEY_D): apply_central_impulse( transform.basis.x)
apply_torque_impulse(transform.basis.x * pitch)
apply_torque_impulse(transform.basis.y * yaw)
pitch = 0
yaw = 0
func _input(event: InputEvent) -> void:
if event is InputEventMouseMotion:
yaw = -event.relative.x*0.065
pitch = -event.relative.y*0.065
Applying forces seems quite tempting to me as a solution. It's easy enough to reason about, and takes no quaternions. If only I could keep collisions from affecting its rotations...
3- Using a Generic6DoFJoint
... somehow... I haven't given this a try. It just crossed my mind. Probably not a good idea.
Possibly there might some other ways I can't think of. Would really appreciate if someone could give some insights on this.