Posts

Showing posts with the label Physics

2D Physics Engine Tutorial – Part 3

Image
In part 2 we had a look at the collision detection part of physics engines. In this post we take a look at the collision response part and see what kind of mechanisms it involves. Movement Physics engines have the ability to move objects around both in linear and angular motion. It is done using linear and angular velocity (with speed and acceleration ), force and integrators . Velocity Velocity is a vector that describes the rate of change in position; the magnitude of the vector is the speed and the direction of the vector is the direction in which the speed is applied. Velocity definition v is the velocity vector, Δx is the displacement and Δt is the change in time. Force Force exist in many ways, but generally it is the agent that cause a body to change in motion or make a mass change its velocity. Force has both magnitude and direction and thus it can be expressed as a vector. Newton’s second law F is the force vector, m is the ...

2D Physics Engine Tutorial – Part 2

Image
In part 1 I explained what a physics engine does and what kind of physics engines there exists. This post will focus on the collision detection internals of physics engines. Overview A physics engine does two things: Collision detection and physics reaction. They work seamless together to create the illusion of things colliding and bouncing back. Collision Detection The collision detection part of the engine is responsible for detecting when things overlap and by how much they overlap. Most physics engines have developed a two-tier architecture where you first detect if objects are near each other using a crude representation of the object. Then it performs the collision detection on the objects itself using its real representation. It basically is setup like this: Broad phase The broad phase is the one responsible for detecting objects near each other. It was developed to speed up the collision detection phase by reducing the amount of collision checks needed. Imag...

2D Physics Engine Tutorial – Part 1

Image
In this article series the focus will be on identifying the different parts of a physics engine and then how we can implement those parts as simple as possible. There are tons of 2D and 3D engines available on the internet – this is not going to be a contestant to any of those engines, it will purely function as an educational physics engine to better grasp the concepts behind it. What does a physics engine do Almost all games need some way of detecting if two objects collide and respond using the proper reaction. It might be Mario standing on top of a platform or Max Payne shooting a bad guy – they both need collision detection and physics. Wikipedia definition A physics engine is a computer program that simulates physics models, using variables such as mass, velocity, friction, and wind resistance. It can simulate and predict effects under different conditions that would approximate what happens in real life or in a fantasy world. Its main uses are in scientific simulatio...

Hybrid narrow phase using convexity detection

While writing Convex polygon based collision detection I came up with the idea of using a hybrid narrow phase to maximize usability and performance. It might have been thought of before and if you stumble upon this, I would like to hear your thoughts about it. It basically consist of taking the pairs found in the broad phase and checking the convexity of the polygons before sending them down to the narrow phase. As I described in the earlier post, SAT only works on convex polygons because it uses the separating axis between two non-colliding convex polygons. This limits the users of the engine to use convex polygons only and thus resort to methods of convex decomposition to get concave polygon support. If the polygons were checked for their convexity before being send to the narrow phase, the engine could send the pair of potentially colliding polygons to a narrow phase of its choice - depending on the composition of the pair: Convex vs. concave – Send to concave narrow phase...

Convex polygon based collision detection

Image
A lot of people have asked me: Why use a convex polygon only engine like Box2D? In this post I will provide some info on why convex polygons is the optimal polygon type to use. What does convex mean? Convex polygons needs to satisfy the following requirements: All internal angles must be below 180 degrees Every line segment between two vertices can be contained fully inside or at the edge the polygon A non-convex polygon is called a concave polygon and it must have an internal angle measuring greater than 180 degrees. As you might have thought about yourself; concave polygons can be quite handy. Convex-only engines can’t represent a simple player polygon like Pac-Man . Why use convex-only? There are really only one big juicy reason: performance Physics engines are able to take quite a few shortcuts and use cheap algorithms to determine the closest vertex, shortest distance and a lot more. An algorithm like Separating Axis Theorem and the Gilbert – Johnson –...