Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
    
What perspective are we talking about here? Full 3D, top-down 2D, what? –  Nicol Bolas Jan 29 '12 at 0:21
    
3D. Preferably 3rd-person. –  tyjkenn Jan 29 '12 at 6:47
    
are you using an inertial trust system or something less realistic? –  FxIII Jan 29 '12 at 13:52
    
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. –  tyjkenn Jan 29 '12 at 20:13
add comment

1 Answer 1

up vote 6 down vote accepted

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.

share|improve this answer
    
Brilliant answer and guidance put down in a very readable form. I couldn't put it better myself! –  Douglas Rae Mar 19 '12 at 12:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.