FAQ Search
Memberlist Usergroups
Profile
  Forum Statistics Register
 Log in to check your private messages
Log in to check your private messages
Moonpod Homepage Starscape Information Mr. Robot Information Free Game Downloads Starscape Highscore Table
Starscape Physics
Post new topic   Reply to topic    Discussion Pod Forum Index -> Independent Game Development View previous topic :: View next topic  
 Author
Message
Heko



Joined: 29 Jul 2004
Posts: 11



PostPosted: Wed Apr 13, 2005 3:53 pm    Post subject: Starscape Physics Reply with quote

I'm currently writing my own game and I was wondering as to how you did physics on starscape? Did you write an entire physics engine or you did the usual approach with set pixel speeds?

I.e, did you assign weights/masses and weapon recoil etc., or did you just use for example 20 pixel max speed and 5 pixel acceleration?

And you use lookup tables or just usual sin/cos conversions?
Back to top
View user's profile
Rup



Joined: 19 May 2003
Posts: 363
Location: London, UK



PostPosted: Wed Apr 13, 2005 7:23 pm    Post subject: Reply with quote

As an aside, if you want to start from someone else's, there's the Torque 2D engine ($100, rigid-body physics, collisions, rendering etc.) or PopCap's framework (free, probably some physics?). And probably plenty more on SourceForge if you're willing to go GPL.

But it's probably more educational to write your own and you'll have finer control over the feel of your game. And I haven't had a decent opportunity to use my maths in years, bah.

I'm probably a decade out of date - what's the sin/cos thing? I remember everyone used to use 0-255 for angles and a table of 0x100 * sin for a turn and a quarter.
Back to top
View user's profile
Poo Bear
Pod Team
Pod Team


Joined: 14 Oct 2002
Posts: 4121
Location: Sheffield, UK



PostPosted: Thu Apr 14, 2005 5:58 am    Post subject: Reply with quote

Using some simple physics stuff is dead easy and actually saves time, all you need are Newtons 3 laws of motion for most stuff and you can't go wrong. Plus you can always bend a few things to get the desired behaviour.

Code:

1. if in a state of uniform motion then remain in that state unless acted upon by an external force.

2. the relationship between applied forces, mass and acceleration is F=m.a

3. for every action there is an equal and opposite reaction.


A bit of thermodynamics.

Code:
The first law of conservation of energy states that energy cannot be modified or destroyed only modified.



Then you need a touch of vector maths as they make a lot of calculations quite easy.

http://chortle.ccsu.ctstateu.edu/VectorLessons/vectorIndex.html


Pulling all that together in Starscape you get this:

Movement
1. during the update phase every object accumulates instant force vectors upon themselves trying to move them in various ways.

2. the summed instantaneous force divided by each objects mass gives an acceleration on the object for the entire time of that update (25fps always gives a nice elapsed time of 40mSecs).

3. the acceleration is then added to our velocity vector and then discarded, it is only this velocity that is remembered. The velocity vector is then checked to make sure its magnitude does not exceed terminal velocity (this is just some arbitrary value to stop anything going too fast).

4. if this frame's instantaneous forces were near zero then the velocity is multiplied by a fractional value to simulate friction, this is why things slow down fairly quickly.

5. if you hit the top or bottom edge of the screen then negate the y component of our velocity, if you hit the left or right edge then negate the x component.


Collision response is a bit more complicated, but I'll explain it if anyone is interested.
Back to top
View user's profile Visit poster's website
OvermindDL1



Joined: 29 Mar 2004
Posts: 138



PostPosted: Thu Apr 14, 2005 2:21 pm    Post subject: Reply with quote

Actually, I'd be interested in how you pull that stuff off.
Back to top
View user's profile Visit poster's website
Poo Bear
Pod Team
Pod Team


Joined: 14 Oct 2002
Posts: 4121
Location: Sheffield, UK



PostPosted: Thu Apr 14, 2005 5:56 pm    Post subject: Reply with quote

Collision Response

So we've summed all our forces and calculated our velocities and now we have new positions, but the collision detector is telling us something bad has happened so what do we do?

So take 2 objects in collision:

1. Get a normal vector from the centre of obj1 to obj2, a "normal vector" just means it has a magnitude of 1. This is the direction of collision, imagine two pool balls hitting each other and drawing a line from the centre of ball1 to ball2.

2. Get an energy vector for each object, this is just velocity * mass. This is where the object wanted to go before the collision happened.

3. Use the collision vector to attenuate the energy vector of each object. What this means is if the direction of collision is straight up and I was moving straight up before I hit you then I want to transfer all my energy into you. If I just gave you a glancing blow then I'll only transfer a tiny bit of my energy. The fraction of energy to transfer is calculated using a special bit of vector math called a "dot product", very handy thing.

4. In any collision there is always energy dissipated as sound or heat or light and we can simulate that by simply removing a very small percentage of the energy prior to our collision calculations.

5. The final step is to just add all these vectors up

Code:
resultant velocity = (initial velocity * 0.95)
                             - velocity given to the other body
                             + velocity received from the other body


This all needs to add up and balance nicely, remember there is no "free" energy in the universe, everything has to go somewhere. If you give all your energy to the other guy then you better stop and the other guy better be going a lot faster (unless he is 100x more massive than you in which case his increase in speed will be tiny).

The final fly in the ointment:

If you just implement the above then things will soon start to stick together and get upset. Objects are moving in discrete time steps, they are jumping through space so they can sometimes get inside one another. The calculations have limited floating point precision so errors will creep in. These algorithms are for spheres and not all Starscape objects are spheres. Sometimes there are a lot more than 2 objects in collision at the same time. Eek!

As a last step then move both objects along their new velocity vectors and recheck for collision. If there is a problem then that means the objects have not "escaped" each other properly so move both objects back towards their original pre-collision positions by 50% and retry, do this a couple of times and if you just cannot get a collision free position then forget it and reset to the original pre-collision positions.

What we are doing is cheating, we know that when we started before anyone moved everything was fine and nobody was touching anyone. We also know we are moving in very small steps and if certain objects miss the odd movement then nobody will notice. This lets us run a simple collision response system to solve a potentially very complex problem. As we test all objects we will always find a few that can be moved into non-colliding escape positions and the act of moving some will free up space to clear others. Before long even a very complex pile up will have resolved itself quite nicely.
Back to top
View user's profile Visit poster's website
OvermindDL1



Joined: 29 Mar 2004
Posts: 138



PostPosted: Fri Apr 15, 2005 12:00 am    Post subject: Reply with quote

Ah, yea, that should work quite well as long as speeds are slow enough and/or the sim speed is fast enough.
Back to top
View user's profile Visit poster's website
Heko



Joined: 29 Jul 2004
Posts: 11



PostPosted: Wed Apr 20, 2005 1:47 pm    Post subject: Reply with quote

Woah! Thanks for the very detailed response, this helps a lot!

So your terminal velocity/acceleration is set in pixel speeds? I.e, the fastest your best hull can go is 5 pixels per second @ 25fps?

Also, you do have mass in the equation? I.e, you assign a big mass to your big mining ship and a smaller mass to your fighter ship and take into account when you do collisions?

You should publish some sort of reading material on the entire subject of game development - it looks like you all have a lot of knowledge and I'm sure it'd be helpful to a lot of people.
Back to top
View user's profile
Poo Bear
Pod Team
Pod Team


Joined: 14 Oct 2002
Posts: 4121
Location: Sheffield, UK



PostPosted: Thu Apr 21, 2005 5:00 am    Post subject: Reply with quote

Heko wrote:
So your terminal velocity/acceleration is set in pixel speeds? I.e, the fastest your best hull can go is 5 pixels per second @ 25fps?


yes, held as a floating point value so that very small accelerations can still accumulate.

Heko wrote:
Also, you do have mass in the equation? I.e, you assign a big mass to your big mining ship and a smaller mass to your fighter ship and take into account when you do collisions?


Yes, if you follow the collision response through you should see that flying at full speed into a large asteroid will cause the asteroid to only slightly accelerate due to its huge mass.

Heko wrote:
You should publish some sort of reading material on the entire subject of game development - it looks like you all have a lot of knowledge and I'm sure it'd be helpful to a lot of people.


You're right, I wish games didn't take so damn long to make Smile
Back to top
View user's profile Visit poster's website
BluePhoenix



Joined: 08 Jun 2004
Posts: 96
Location: Between Georgia and Cuba



PostPosted: Fri May 06, 2005 11:52 pm    Post subject: Reply with quote

Poo Bear wrote:

A bit of thermodynamics.

Code:
The first law of conservation of energy states that energy cannot be modified or destroyed only modified.


Don't you mean that it can't be created or destroyed? Wink

Great info guys! This is useful stuff! I might need it someday.
Back to top
View user's profile AIM Address Yahoo Messenger
Display posts from previous:   
Post new topic   Reply to topic    Discussion Pod Forum Index -> Independent Game Development All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group