Home Artists Posts Import Register

Content

I've always been curious how physics work in games and decided to give it a go. I enrolled in the Pikuma 2D physics course and started learning about concepts like impulses, forces, angular velocity, collision detection and collision resolution.

Fun fact: Sector's Edge doesn't contain any physics. It's all fake! I have no idea how physics engines are meant to work.

Circles

The easiest place to begin was with circles, since they're a simple shape. This was my first attempt at proper collision detection, I think it went well:

I then tested with two circles but found the collision resolution was inverted, meaning they would stick together rather than push apart:

I then reverted the collision resolution direction, but they still wanted to hang on to each other:

Now it's working... kind of!

After reviewing the code I found that collision resolution was working correctly (the circles would no longer overlap) but the collision response (velocity) was too high.

I then discovered I could stack them vertically - so satisfying!

I then implemented mass, which meant it would be harder for smaller circles to push larger circles around. I was pretty happy with this!

Boxes

I've been curious about square and rectangular collision for as long as I can remember. I've never understood how shapes are meant to rotate when colliding - i.e. a box tipping over and falling off a platform - so by this point I was pretty excited.

The first step was to determine if two rotating boxes were overlapping (collision detection):

The next step was to move the boxes away from each other so that they are no longer overlapping (collision resolution). For non-rotated boxes this is pretty simple as there are only four directions (up, down, left, right) to move in:

However some funky maths has to be used to figure out collision resolution for rotated boxes. The physics course started to get into angular velocity and acceleration - which I don't fully understand - so by this point I was just trying to get the code to work.

I then enabled gravity to see if I'd implemented this correctly.

Apart from the insane rotation... it's kind of working! I was so close to uncovering the mystery of how boxes rotate and fall off the edge of a platform. How cool is this!?

Angular Velocity

By this point I had to take a step back because something clearly wasn't right with the way boxes were rotating:

I discovered the angular velocity was incorrectly applied at a 90 degree angle, so when the box landed on the ground it spun to the right, rather than staying upright.

Things were now looking better:

The next step was to add angular velocity to circles, which was much easier than boxes!

Friction

All the boxes are currently sliding around on ice - there's no friction!

The physics course taught that friction is simply a sideways force, i.e. when two objects collide, a force is applied perpendicular to the collision angle. This means that a box falling down and to the right would lose some horizontal velocity when landing on the ground.

Watching them sway and topple over feels pretty cool!

Polygons

So far we have boxes and circle collision. It was time to try more complex shapes like triangles and rectangles. As always, it worked on the first attempt:

After much confusion and debugging I realised this was another off-by-90-degrees bug, which meant the collision detection and resolution thought the shapes were rotated 90 degrees further than they actually were.

To ensure I didn't make this mistake again, I drew the surface normals as red lines on each polygon edge:

Time to try rectangles:

This bug was related to object mass, and caused objects to gain velocity rather than lose velocity when colliding:

I still hadn't fixed the issue with mass, but discovered that I could 'slice' circles, causing them to have backspin. I don't understand how angular velocity works, but it sure is cool!

And that's as far as I got. I was enjoying it as a side project, but it was becoming clear that there are a lot of complex physics problems to solve, and that's just in 2D. Regardless the course was definitely worth it as I gained an insight into how physics engines work under the covers.

In my next post I'll talk about my experiments with 3D physics using BepuPhysics, an open source 3D physics library.

Comments

vercidium

It's satisfying hey! BepuPhysics does all the work for me, I won't have to write any maths or physics logic. I'm given a 'physics scene' which I can add objects to, e.g. the player, terrain, rocks and trees. Then I move the player around and it will handle the collisions for me

Anonymous

Physics: the only time circles is easier than squares It's really fun seeing all the janky physics as you were making it. And those balls are satisfying!