# Physics integration

This topic is 4068 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm writing a game, and I'm done with graphics, and I'm ready to do physics, but I'm not quite sure how do it. Unlike with a graphics system, there must be two way communication between physics and central game logic.(updating of position, collisions, etc.) So should I have a seperate system, so that different interactions with the physics system I use will be hidden from the main game logic? THis seems the most OO way to do it, but Physics is such an important part of the game that it might be better to make the Game logic directly control the physics system(ODE) WHat is everyone else's experience?

##### Share on other sites
If you want to do it the 'OO way', then I would suggest creating a simple method for processing the physics, and then create a few useful classes that describe things like mass, charge, or whatever you find appropriate. Each object you need would inherit or include whichever physical class is most appropriate to them, and that's how the main engine would change their properties.

But it's important to know how much physics processing you need. Pac-Man doesn't need a physics engine. A game with long range ballistics could benefit from it. Flight Sims almost require it.

To start off, let's say we have Objects In SpaceTM. You might create a class that looks like this:
class phyVector {  double x,y,z;};class phyParticle {  double mass;  phyVector position;  phyVector velocity;};

So let's say you have a space ship which orbits a planet. Each object would inherit the class phyParticle:

objPlanet earth;objShip ship;earth.mass = 6e24;ship.mass = 2e5;ship.position.x = 8e7;ship.velocity.y = 2200;

So then you might have a main physics function that takes the time passed since the last time it was called. In this case, it would determine the force of gravity exerted on the ship by the planet, which is big, and maybe the force of gravity exerted on the planet by the ship. Using these forces, it would update the velocities of each object, and then the position of the objects using their original velocities.

1. Determine forces
2. Determine new velocities
3. Determine new positions

Of course, there are other factors, depending on how complex you want to make it. Motion may be constrained. There may be collisions. You may not want to consider every effect available either. Should you calculate the gravitational pull of the ship on Pluto if it's orbiting the Earth? What if the objects can't be represented by points? You could have systems of particles to represent complex objects, with motion restraints. Can your objects deform under stress?

There's really a lot of depth you can add to a physics engine. That's why you have to figure out just how much you need. What kind of project are you doing anyways?

##### Share on other sites
One way, depending on the amount of physics in your game, is for the physics engine to have primary responsibility for the game state – i.e. all write access to 'physical' parameters of objects (velocity, position, mass etc) is through the physics engine. The input layer would then (for example) use player input to update accelerations by changing parameters within the physics layer. (I think that's what you mean by "it might be better to make the Game logic directly control the physics system", though you might well want a thin physics layer of your own on top of ODE.)

##### Share on other sites
This project is a 3d space strategy game. Spacesships themselves will be highly modular, with individual modules being the fundamental particle of my universe. Each module will perform a single function, such as thrust, or computing, or housing crew, or manufacturing new components from resources. THese modules will be linked by a simple system of structural beams. Beams are (slightly)springy, and can fail due to excess compression, extension, torque, or weapons damage(such as heat or EMP or impact). Modules will fail only due to weapons damage or acceleration. Some structural beams will carry resources like computing power, energy, or various materials(organics, metals, torpedoes, etc.) There will be asteroids, and those have other strange properties that I plan to deal with some other time.

On top of ODE, I intend to track a few extra things, like heat flow(by conduction along structural girders) between modules, flow of resources, depletion of resources, construction and attachment of new structure and modules, etc. Connections between modules through beams or resource carrier tubes will be tracked on top of ODE, but they must also be known to ODE, so that thier structural value can be appreciated. One question: would it be possible to make no mention of position outside of ODE, except for the obligatory phyVector class for communication between ODE and the scripting system?

I guess it seems that the game logic could be handled by a physics system that includes ODE. From this, I guess the real question remaining is this: what purpose could be served by having a main logic(not including menus, etc, which are part of the input system) seperate from the physics system?

##### Share on other sites
I follow a method kind of similar to Bob Janova's, and it works well for my games. I view my GameObjects as entities that are under several different influences. They each can influence themselves (e.g. try to move right), they are each influenced by the scripting engine (where I define a lot of game logic), and then finally they are each influenced by the physics engine. I keep this object-oriented by defining my GameObjects in chunks that are accessible by the different systems in different amounts. The physics engine (also ODE, actually) can read and write the position, velocity etc of the objects but can only read the mass. The scripting engine can read the position and velocity of the objects and can read and write the mass. The scripting engine can also read and write some other things like labels and models representing the object that the graphics engine can then read. It ends up working really well for me, and my code has ended up extremely clear and readable. Best of all, if I wanted to add another system of influences, it wouldn't be hard at all, and it couldn't interfere unexpectedly with the systems I already have in place.

##### Share on other sites
Thank you all. I believe that the best thing for my situation is a physics system that is modified by the scripting system and that modifies the graphics system. There is no need for a main logic system as seperate from the physics system. Communication betweeen the systems can be greatly simplified this way. There will be mehtods for getting position and orientation out of the physics system, and a main object tracker to get this information out of them and render it every frame. There will be methods to modify various values. There will be trusted and untrusted scripts that run and utilise these various methods.The scripting system will directly know about modules and structure.

##### Share on other sites

This topic is 4068 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628636
• Total Posts
2983966

• 10
• 18
• 20
• 13
• 9