I am wondering how extremely basic physics engines work and how I could implement them. lets say I have a player class (like a frame, except it holds the variables for the players), and I would like to add gravity. Could I go something like this

Player p = new Player();
p.addPhysics(gravity);

to add gravity to the player? Also I am wondering how easy would it be to do something like this with just gravity, and lets say different friction coefficients (ice, sand, grass, etc)?

Thanks for any help

Recommended Answers

All 3 Replies

you'd not handle it like that. Rather you'd pass the player and whatever movement input the player has initiated to the physics engine, and get a movement vector as a result.

Simple physics is not hard - store the objects current position and velocity. Use a timer to update them at regular intervals (eg 60 mSec). At each update change the position according to the velocity, and change the velocity according to gravity or friction. For gravity add a fixed downwards increment to the velocity. For friction reduce the velocity by a percentage (depending on how big the coefficient of friction is).
Then add in whatever you need for user input, bouncing off walls/obstacles, collisions etc - but that's where it gets harder!

thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.