Enjoy this 2d space simulation game ! Fly your ship, mine asteroids, assemble parts to build your own ship. When you find light-speed engine, fly to unknown areas. Finally, build your own autonomous space-station !
How is the ship physically simulated ? In fact, this is very simple !
Posted by matfrem on Apr 8th, 2012
The physic library used is Box2d, one of the most famous 2d physic libraries. Originally in C++, it has been ported to C# so available for Unity.
Each ship is read from a text file. Then all the parts are added to a physical body. The physical body of Box2d behaves nicely (I removed the gravity as we are in space, you know). Important hing here is center of mass. Computed from the barycenter of all weighted pieces.
Now the point is adding forces where thrusters are. A simple AI decides what thrusters to power, depending whether the player wants to go forward, backwards, rotate or strafe.
Let's say you want to go forward. All you have to do to know what thrusters power on is compute the angle between thrusters and direction you want to go. If angle is between -10 and -170 degrees, fire the thruster !
Exact same computing for strafing, only the direction is different. The computing of an angle can be done using a DOT product between vectors (in fact it's the cosine, but anyway)
But what about turning ? Easy too !
Imagine a line beween center of mass and center of thruster (the red line). Image another line that indicates thruster direction (the blue line). To know in what direction the ship will go if you power this thruster, just "follow" from the blue to the red line and you got your direction ! (the black arrows).
This can be done easily using CROSS product.
Of course, depending on where the thrusters are, ship cannot always do all these moves ! Moreover, moves are usually not absolutes; when you rotate, you also move a biit. when you strafe, you will rotate a bit too..... That's the fun !
I hope you have been interested by this article... anyway follow the game, more to come !