3
\$\begingroup\$

Right now I have spaceship orbiting a small planet. I'm trying to make an effective control system for that spaceship, but it always end up spinning out of control. After spinning the ship to change direction, the thrusters thrust the wrong way.

Normal airplane controls don't work, since the ship is able to leave the atmosphere and go to other planets, in the journey going "upside-down". Could someone please enlighten me on how to get thrusters to work the way they are supposed to?

\$\endgroup\$
4
  • \$\begingroup\$ What perspective are we talking about here? Full 3D, top-down 2D, what? \$\endgroup\$ Jan 29, 2012 at 0:21
  • \$\begingroup\$ 3D. Preferably 3rd-person. \$\endgroup\$
    – tyjkenn
    Jan 29, 2012 at 6:47
  • \$\begingroup\$ are you using an inertial trust system or something less realistic? \$\endgroup\$
    – FxIII
    Jan 29, 2012 at 13:52
  • \$\begingroup\$ Right now I've just been using rigidbody.AddForce() for the forward thruster, if that's what you mean. For turning, I've been using rigidbody.AddTorque(), but that doesn't seem to be working well. I'm no expert in physics, but if you could point me toward the concepts I would need to understand to get this to work, it would be much appreciated. \$\endgroup\$
    – tyjkenn
    Jan 29, 2012 at 20:13

1 Answer 1

7
\$\begingroup\$

I think your use of RigidBody physics for controlling the ship is an interesting idea (and might even be a very "correct" way of approaching this), however it's probably much easier to implement the logic yourself. In your last comment, you asked about the basic concepts:

Inertia / Momentum

Space ships have inertia, and since space is (for our purposes) frictionless, they will simply continue to travel in a straight line with their current speed. This direction and speed is their inertia, and can be represented by a 3D vector (a Vector3 in Unity). This vector is typically referred to as the object's "velocity". In Unity terms, you will want to update your ship's position by adding this vector to it during every Update() call (remember to scale it by multiplying the vector by the deltaTime to cater for frame rate changes). Basically, our object will remember its velocity, and rotation and thrust will be used to change the velocity.

Thrust

Whenever thrust is applied, it is simply added on to the velocity vector. The direction of this thrust vector is the opposite direction to the exhaust of your engine. If we're talking about a "main engine", and assuming we're looking forward, this would simply be the current rotation of your spaceship. This update of the velocity happens during Update() method, before you apply the velocity to the current position. You'd scale the thrust vector appropriately with deltaTime, engine strength, the thrust level input from the pilot, and anything else you'd wish to add. Remember that we're already "applying" the velocity to the position of the ship, so by updating the velocity, the position of the ship will be affected.

Rotation

If we assume that we simply wish to rotate the ship in response to some user input (I mean: the rotation thrust is not going to be applied to the velocity as a "side-effect"), then things are relatively simple. Just as with aircraft, we have 3 primary axes around which you can rotate (refresher on Wikipedia), the pitch, yaw and roll axes. Fortunately, these correspond to the X, Y, and Z axes in the local space of the ship, meaning that we can perform a transform.RotateAround(SHIPCENTRE, AXIS, ANGLE).

If I remember correctly, transform.RotateAround operates in world coordinates, meaning that your rotation axis will need to be rotated into the ship's local space. It would be something like:

transform.RotateAround(transform.Position, transform.TransformDirection(Vector3.up), 20 * Time.deltaTime);

The above should give you some idea of how a standard inertia and thrust based control system works.

\$\endgroup\$
1
  • \$\begingroup\$ Brilliant answer and guidance put down in a very readable form. I couldn't put it better myself! \$\endgroup\$ Mar 19, 2012 at 12:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .