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 on

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)

code:
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.

code:
V1 ^ V2

Conclusion:
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
iamscissors

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

Reply Good karma Bad karma+1 vote
matfrem Author
matfrem

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.

Reply Good karma+1 vote
coderbill
coderbill

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!!

Reply Good karma Bad karma+1 vote
matfrem Author
matfrem

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.

Reply Good karma+1 vote
Post a comment

You are not logged in, your comment will be anonymous unless you join the community. Or sign in with your social account:

Icon
SpaceBeam
Platforms
Windows, Web
Creator
matfrem
Engine
Unity
Contact
Send Message
Release date
Game watch
Start tracking
Feature
Browse
Features
Share
Related Games
SpaceBeam
SpaceBeam Futuristic Sim
Related Engines
Unity
Unity Commercial