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 !

Report article RSS Feed The physical ship

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)

if (abs(V1 . V2) < 0.1) { thrust !!! }

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.

V1 ^ V2

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 !

Post comment Comments
iamscissors Apr 9 2012, 12:02pm says:

I love the math, the turning part was very interesting and well explained. keep it up!

+1 vote     reply to comment
matfrem Author
matfrem Apr 9 2012, 1:56pm replied:

Thank you. The code explaining the rotation had an edition bug. The formula is

if (CrossProd(VBlue, VRed) > 0) { turn left } else { turn right }

Of course like in the forward version, you may check the norm to ensure that exactly perpendiculars thrusters won't be powered.

+1 vote   reply to comment
coderbill Apr 9 2012, 7:29pm says:

this looks a lot like Captain Forever. Is that your inspiration or are you part of the same team? Looks cool so far: Love captain forever!!

+1 vote     reply to comment
matfrem Author
matfrem Apr 13 2012, 10:27am says:

Not same team, but sure it's listed in my inspiration list here in the game page Indiedb.com
It seems that the final aim of the game is going to be less arcade than CF, and more building structures that last in time.

+1 vote   reply to comment
Post a Comment
click to sign in

You are not logged in, your comment will be anonymous unless you join the community today (totally free - or sign in with your social account on the right) which we encourage all contributors to do.

2000 characters limit; HTML formatting and smileys are not supported - text only

Windows, Web
Developed By
Send Message
Release Date
Game Watch
Track this game
Report Abuse
Report article
Related Games
SpaceBeam Single Player Futuristic Sim
Related Engines
Unity Commercial Released May 30, 2005