Here's how you might do it:
Approximating movement
Every physics object needs these vectors:
- Position: Where the object is.
- Speed: How its position is changing.
- Acceleration: How its speed is changing.
So, intuitively, you need to do this sort of thing every frame to every physics object a
:
a.speed += a.acceleration
a.position += a.speed
Pitfalls: This is known as the Euler method of integration. This approximation is better for small speeds and accelerations and with small time-steps. In a game, this is normally true. But if an object moves really fast toward a thin wall or directly toward another moving object, it may skip collision with that object when its position is incremented by a large enough amount to cross it entirely. If you have fast-moving objects, you may need Continuous Collision Detection to get away with this.
Collision
Collision can be approximated with this sort of sequence:
- Detection: It is detected that two bodies intersect.
- Resolution: The position of each body is changed so they no longer intersect.
- Physics: The speed of each body is changed to account for momentum, friction and restitution (bounciness).
This is known as Discrete Collision Detection, as it happens at discrete points in time. Each step deserves attention:
Step 1: Collision detection (Are things colliding?)
For two circles: Just check if their centres are more than the sum of their radii apart.
For two rectangles: Check if any of their corner coordinates are within the other rectangle.
For anything involving more complex polygons, you'll need the Separating-Axis-Theorem, which is a topic for another question.
Pitfalls: There are really many other collision types you might want to consider. Some are really complex: Convex polygons might intersect multiple times!
Step 2: Collision resolution (Moving them apart.)
A simple method to resolve a collision is translation by minimum penetration, using values obtained from collision detection. This essentially involves moving the objects apart with the minimum possible distance of movement.
As an example, consider circle-circle collision where one of the circles is fixed in place. The minimum displacement vector has the same direction as the difference between their midpoint vectors. Its length is the difference between the sum of their radii and the distance between the centres.
This idea easily generalises to other shapes.
Pitfalls: If both colliding objects are moveable, you may have to move both of them by some fraction of the overall distance to get a believable approximation of their movement. They'll obviously have to move in different directions.
Step 3: Collision physics (Other changes.)
The most obvious thing that tends to happen is that one or two of the objects changes direction i.e. rotates its speed vector. This is pretty easy to implement.
Pitfalls: Objects might be elastic, but they might not be, in which case the total energy in the system is reduced. You might need drag due to air resistance. Friction (particularly static friction) are notoriously complex to implement.
Some conclusions
Simulation of reality is exponentially harder the more if it you want to simulate. You're walking into a minefield of countless edge cases, mindblowingly difficult formulae and ever-growing stacks of research papers.
If your needs are simple or you're eager to learn, try it.
If your needs are complex or you don't like maths, get a physics engine to gloss over the details for you. Box2D and Bullet are pretty popular in games.