Created a custom physics simulator for a game where all elements are physics-based

Hey everyone! I’m super excited to share a project I’ve been working on. I built a physics engine from the ground up for a game idea I had. In this game, literally everything is governed by simulated physics. It’s been a wild ride!

I started by coding basic collision detection and response. Then I added gravity, friction, and elasticity. The tricky part was getting rotational physics right. I spent days tweaking angular momentum calculations.

Here’s a snippet of my main physics update loop:

def update_physics(objects, dt):
    for obj in objects:
        obj.velocity += obj.acceleration * dt
        obj.position += obj.velocity * dt
        obj.angular_velocity += obj.torque * dt / obj.moment_of_inertia
        obj.rotation += obj.angular_velocity * dt
        resolve_collisions(obj, objects)

    apply_constraints(objects)

I’m curious if anyone else has tried something similar. What challenges did you face? Any tips for optimizing performance? I’d love to hear your thoughts!

That’s pretty cool! I’ve messed around with simple physics in Unity but never built a whole engine from scratch. How’s the performance? I imagine it gets pretty crazy with lots of objects. Have you thought about using spatial partitioning or anything to optimize collision checks? Might help if you’re planning to scale up to more complex scenes.

Wow, that’s impressive! I’ve played with physics in games but never built a whole engine. Rotational physics can be a headache. Have you tried it with ragdoll characters yet? That could be hilarious and challenging at the same time.

Nice work on the physics engine!

Have you considered adding soft body physics?

It could add some cool squishiness to your objects and make things even more realistic.

Nice work on the physics engine! I’ve played with basic physics before, but building a whole engine is next level.

Have you thought about adding destructible environments? Could be fun to see everything crumble realistically.

Physics engines are no joke! I tried something similar once but got stuck on the rotational stuff. Kudos for nailing that. Your code looks solid. Ever thought about adding fluid dynamics? Could be a fun challenge to take it to the next level.