Hi,
I'm actually developing a re-usable Game Engine for my platform game and I needed to handle some basic physics. I first decided to use Box2D but there was too many things for me and it was difficult to integrate it with my engine. So I bought a vey good book on making basics 3D Physics engines. Some things were complicated or useless in 2D so I used it as a base for doing my own engine, easy to integrate with my SFML Game engine.
I don't pretend to re-invent the wheel, tis is a veeerrrryyyyy basic engine that can handle the minimum :
- Implement Rigid Bodies with sf::Shape fixtures
- Generate forces on Bodies with the possibility for user to implement non-trivial Forces Generator : (Gravity and Draw forces generator already implemented)
- Integrate (linear) Forces, Velocity and Acceleration on bodies
- Handle collision between Bodies (if they have Convex Shapes) using Separating Axis Theorem
- Very (very very) simple rotations angular velocities/accelerations (the result isn't very convincing)
Example of code :
// A Register of global forces to apply. Will be included in a "Physic" or "World" general class :
isaac::ForceRegistry registry;
// A gravity generator :
isaac::GravityGenerator gravity(isaac::Vector2(0.f, 150.f));
// Add a body
isaac::Body cube;
cube.setPosition(300, 320);
cube.setMass(500);
// Fixtures
isaac::Fixture f1;
f1.RelativePosition = isaac::Vector2(0,0);
f1.Shape = &cubeShape; // Just a sf::RectangleShape
cube.addFixture("base",f1); // There is a key string, I think about removing it
// Applies forces
registry.add(&cube, &gravity);
// Main loop :
sf::Clock clock;
float timestep = 0.f;
while(1)
{
// Events...
registry.updateForces(timestep);
cube.intergrate(timestep);
// Draw, using the shape or a body method to get all fixtures
}
Here a short video :
http://www.dailymotion.com/video/xqh60v_isaac-simple-physics-engine_videogamesThere are a few bugs, I haven't documented every functions, so ATM I can't provide anything.