Advertisement

physics engine

Started by May 19, 2002 05:45 PM
6 comments, last by Samith 22 years, 8 months ago
i want to try my hand at writing a physics engine but i dont know what im going to have to do. i dont even really understand the concept of one. i know that it has to be called a cetrain amount of times every second (i guess thats what people mean when they say they have their physics engine set to 500hz or whatever) but i really dont know much more. are there any tutorials for that type of thing anywhere? i really want one lol thanks, samith

Have no tuts on hand, but I remember an IOTD over at flipcode that used the ODE physics engine, which is open-source.

The iotd is here, and it''s really cool in action.


2D!Now
Advertisement
A physics engine calculates the movement of everything in your game. Usually they try to mimic real physics to some degree... but the most important thing is that behavior is consitent.

It's not strictly necessary to call the engine at fixed intervals, just keep measure of the time between each call.

If you've made the ubiquitous Tetris-clone, you could play with altering the way the blocks fall down, or you can download source code from the GD showcase. By default the "physics" of the block probably is something like this:

block appears at starting position: x(0), y(0) at time: t(0)

vertical velocity:
vy(t) = - 1 row / s, if theres space beneath the block
vy(t) = 0, otherwise

horizontal velocity:
vx(t) = - 1, if theres space to the left and left key pressed
vx(t) = + 1, if theres space to the right and right key pressed
vx(t) = 0, otherwise

position at time t:
y(t) = y(t-1s) + vy(t) * 1s
x(t) = x(t-1s) + xv(t) * 1s


To play around with the physics you could introduce downward acceleration:

v(y) = a(y) * (t - t(0))
a(y) = - 1/4 row / (s*s),

or some other number

Gameplay will be much different :-).

Let the arrow-keys accellerate sideways:

horizontal velocity:

vx(t) = v(t - 1s) - 1 row / s, if left key pressed
vx(t) = v(t - 1s) + 1 row / s, if right key pressed
vx(t) = 0, if attempt to move into a wall
vx(t) = v(t - 1s) , otherwise

To make control easier, dampen the motion when no keys are pressed. More ideas: make the block rebound when it hits the wall or the bottom. Make it possible to push the landed blocks sidewise, if you have enough speed and the blocks you push aren't to heavy.



---------
"It's always useful when you face an enemy prepared to die for his country. That means both of you have exactly the same aim in mind." -Terry Pratchett


[edited by - deformed rabbit on May 20, 2002 5:59:57 AM]

[edited by - deformed rabbit on May 20, 2002 7:55:58 AM]
---------"It''s always useful when you face an enemy prepared to die for his country. That means both of you have exactly the same aim in mind." -Terry Pratchett
Heres an link to a whole heap of information for building all sorts of things game and sim related.

Have a look at the section:
Physics & rigid body simulation

Hope this helps.
Im a bloody idiot. Heres the link:

http://www.codercorner.com/Links_Programming.htm



u guys are great thanks for all the help, im gonna go check out these links and stuff now

thanks again
samith
Advertisement
i have this strange feeling that im going to have to put in a vector class and do the operator overloading and stuff, then im going to have to make a car class and put all the necessary stuff in there using vectors and stuff, and then, im going to have to do something to make it all work. am i on the right track?
that IOTD engine looked quite nice. but some things struck me about it... it seemed to be based on overlapping, not collisions, and weird ''fake'' forces seemed to be going on (eg, if in the air, accelerate, and your car will start to flip up)...
I can post links to my physics test app if anyone wants to see it.

This topic is closed to new replies.

Advertisement