PDA

View Full Version : Physics Handler


Nick
2005.12.17, 06:55 PM
What is the logic behind making a class that will act as my global physics handler? I want a class that will hold pointers to all the actual objects in my world, step them through the physics, check collisions between them all, and then update the positions. Luckily this is all for 2d which makes the physics easier, but how would the class be structured? Does anyone all ready do something like this?

My reason for this is that as I get further into my game, the objects are starting to pile up and I need an easy way to step them all and detect collisions. I'm also using multiple classes for my objects (some are liquid containers, some are skeletons, some are just random objects) so that complicates matters a little more. My main goal is to make things easier. My goal is for a system that allows the following:

PhysicsHandler *aHandler;
...
aHandler->SetGravity(-9.8);
...
//create object X here
...
aHandler->AddObject(x);
...
aHandler->StepObjects(timeElapsed);


The main trouble I'm having is storing pointers to actual objects without creating new objects, and figuring out how to step them all individually, then detect collisions, then do this over and over to double check all the changes, then set the final data.

Any advice, suggestions, code samples, or whatever is really appreciated.

Skorche
2005.12.18, 05:34 PM
The main trouble I'm having is storing pointers to actual objects without creating new objects, and figuring out how to step them all individually, then detect collisions, then do this over and over to double check all the changes, then set the final data.

Not quite sure why it's difficult to store a pointer without creating another object. Passing a pointer to an object involves copying in no way.

As far as stepping through them all and colliding, what's wrong with just updating them all in the order they are stored in the list and then detecting collisions? If you want to speed up your collisions this (http://graphics.ethz.ch/~brunoh/download/CollisionDetectionHashing_VMV03.pdf) is a good way to do it. It's pretty simple, I was able to implement it in about 250 lines. Also, I just use the list of handles in the hash as the list of objects.

If you want to iterate the collisions, just make sure that everything is in a valid state after each iteration. Then just run the collision routine as many times as necessary.

kinslayer
2006.04.12, 03:29 PM
I believe that the problem here is that stepping through all objects and checking against all other objects is basically a O(n^2) algorithm.

Since this is about positions, I would suggest building a quad tree and do the O(n^2) thing in each quad.

Basically you have a number of lists representing regions in space. you can then cut down on the performance hit substantially.