# Modular spaceship control

I am developing a physics based game with spaceships.

A spaceship is constructed from circles connected by joints. Some of the circles have engines attached.

Engines can rotate around the center of circle and create thrust. I want to be able to move the ship in a direction or rotate around a point by setting the rotation and thrust for each of the ship's engines.

How can I find the rotation and thrust needed for each engine to achieve this?

-
– Byte56 Jun 27 '13 at 17:26
Dont forget to look at pid control en.wikipedia.org/wiki/PID_controller this prevents unnecessary fluctiations(temporary unstability) or unnatural targetting. – huseyin tugrul buyukisik Jun 28 '13 at 10:48

Lets imagine the simple situation of a ship with just one engine. In order to move the ship, we simply point the thruster away from the direction we want to go, and thrust out. That pushes us in the direction we want to go. It's not possible to rotate a ship composed of only one thruster.

Lets scale our ship to three engines to make it more interesting:

If we wanted to move this ship somewhere up and to the left, we'd just point all our thrusters in the opposite direction and fire:

Same solution as the single thruster that takes care of linear movement.

But what about if we're missing a thruster? We have an unequal load?

Following the same strategy will cause the ship to rotate. In many situations, this will be mostly unavoidable, and in some cases preferable since it will orient the ship for better use of the available thrusters.

One strategy for mitigating this would be equalization. Split the ship down the axis of desired movement:

Then ensure that the right side and the left side equal each other. If they don't scale the thruster output until they do. This can easily result in no thrust being applied. It's up to you to decide if you want to instead thrust out causing rotation or force the player to redesign their ship by re-organizing their thrusters.

This question describes the idea very well.

You'll need to calculate the center of mass. Then calculate the torque provided by each thruster. Lucky for you, your thrusters rotate, so you can rotate them to provide maximum torque. Maximum torque will come from a right angle to the moment arm. Applying the sum of the torques will rotate the body around the center of mass. You can also choose some other arbitrary rotation point like the bridge of the ship.

If you want to ensure that you rotate in place you can scale back the thrust of the thrusters that will cause your ship to move linearly. You can do that by adding up all the thrusters that have a positive torque and all those with a negative torque, then compare their absolute values. The group with the larger absolute value should be scaled back to match the absolute value of the other group.

You may also find a very helpful resource in Amit Patel.

He's done a 4 part series on configurable space ships here: part 1, part 2, part 3, part 4

-
There are other (more elegant) solutions to this. But ideally, if the player creates good designs, these strategies should work. This answer is only meant as a basic starting point for this kind of control. There's lots of room for improvement. – Byte56 Jun 27 '13 at 18:50

I am solved that with local search. It's working, but performance is nearly bad. Maybe it is better to use theoretical mechanics but I do not know it.

-
-1, that's a very vague answer. Can you explain more what you actually did to solve the problem? – Byte56 Sep 6 '13 at 19:21