I've been having some troubles recently trying to work with frame independent movement (based on 300 pixels per second) and having the object collide with another. I'm still pretty new to this idea, but I don't know how I should go about having the character move back from the object if the object is moving based on time. If you could help me out here I'd really appreciate it! Thanks!
9 Months Ago
Related Article:[HELP] SDL game error!
is a solved C++ discussion thread by evilguyme that has 2 replies and was last updated 1 year ago.
You would probably be better off using an off-the-shelf library for that, like the Open Dynamics Engine (ODE). It's a simple but pretty solid engine for doing computer game physics realism (not for real simulations, of course). The basic idea with this is that you control your objects (characters, vehicles, projectiles, etc.) by specifying initial velocities and applying force to them (e.g., steering, thrust, etc.). Then, the physics engine takes care of the rest (realizing the motion). ODE has simple collision detection routines too, like primitive to primitive collisions (box-box, box-sphere, etc.). I don't think that it does 2D physics though, if that is what you need.
For doing "frame independent movement", it usually involves at least two threads of execution, one for rendering and one for physics simulations (or motion time-integration). The physics simulation is there to update the position (and orientation) of your models with time. The basis for this is ordinary differential equations (ODE) and numerical integration. First, given a system of rigid bodies (e.g., solid objects), there are laws of classical mechanics that help you to compute the acceleration (and angular acceleration) of a body given its state (position, orientation, velocity and angular velocity) and external forces applied to it (gravity, friction, thrusts, etc.), these are called the governing equations (and in rigid-body dynamics, the easiest form is the Newton-Euler Equations), and they are ordinary differential equations (usually). Collisions come in as a large impact force as two bodies collide (and often, additional techniques are employed to deal with collision responses), and that impact is added to the rest. At every time-step, you take the external forces, current states, and impact forces, and use them to compute the acceleration (and angular accel.) of the bodies, and you use that in a numerical integration scheme to obtain an increment in motion for the next point in time.
By making sure that you can accomplish all your "physics" calculations within the actual time-step used for integration, you can achieve real-time "simulation" of the dynamics of the system. Doing this on a thread separated from the rendering thread ensures that you can do this with much smaller time-steps than the time-step at which the rendering is done. I would say that at the most, your integration time-step should be of about 0.01 second (100 Hz), but the lower the better (1e-4 to 1e-6 are really good). Usually, to achieve real-time performance, you have to give up a significant part of the actual precision of the calculations, this is what the ODE library does.