Sign in to follow this  

How to integrate physics in a game engine

This topic is 408 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone. This is my first forum post on game dev (actually, this is my first forum post ever), so please inform me if I'm doing anything wrong.

 

I'm developing a 2d game engine of my own, but I came across this problem and I don't know how to get around it: how do I integrate a physics engine in a game engine?

Let's assume that I've a physics engine capable of AABBvsAABB testing, with a public API that exposes its methods to the rest of the game engine, and let's assume that I have an instance of a scene with a collection of objects inside. Now, let's say that one of the objects of the scene is falling. In this situation, I would look for collisions every frame in order to make my object stop when it gets on a surface, right? "Right!" I say to my hand... But, in a scenario where I'm only able to do objectVSobject tests, I should test my object with every single object of its scene in order to recognize when it collides. Not only! I should test every dynamic object of the scene in this way. It sounds crazy to me.

 

I'm deadly sure that I'm doing something terribly wrong. Maybe my objects should be able to discriminate which tests do, and do them by theirself with an internal reference to the physics engine... I don't know.

 

Any suggestion would be appreciated

Edited by fedeTibaldo

Share this post


Link to post
Share on other sites

Normally the physics engine has an efficient way of representing the location and shape of the objects so that it doesn't need to check each one against all of the others every frame. You shouldn't need to have to worry about that either.

 

If you want a more detailed answer then you'll need to be specific about the physics system that you're using.

Share this post


Link to post
Share on other sites

Normally the physics engine has an efficient way of representing the location and shape of the objects so that it doesn't need to check each one against all of the others every frame. You shouldn't need to have to worry about that either. If you want a more detailed answer then you'll need to be specific about the physics system that you're using.

 

 

I made it from scratch, so I don't have any additional information about it

Edited by fedeTibaldo

Share this post


Link to post
Share on other sites

Right, so you need to implement efficient spatial querying into your physics engine. I'm not a physics expert but the core physics loops I've seen look a bit like this:

for every object A that is motionary:
    move object A // vel += forces, pos += vel, etc
    for every object B nearby: // eg. from a quadtree, spatial hash, etc
        check for collision/penetration between A and B
        resolve if necessary, perhaps setting the object B as motionary
    if object A movement is below some threshold:
        set object A as not motionary

Sometimes it's separated out into discrete collision checking and the dynamics simulation sections - useful for cases when you care about detecting collisions but don't need things to bounce around or rebound when they touch.

Edited by Kylotan

Share this post


Link to post
Share on other sites

Right, so you need to implement efficient spatial querying into your physics engine. I'm not a physics expert but the core physics loops I've seen look a bit like this:

for every object A that is motionary:
    move object A // vel += forces, pos += vel, etc
    for every object B nearby: // eg. from a quadtree, spatial hash, etc
        check for collision/penetration between A and B
        resolve if necessary, perhaps setting the object B as motionary
    if object A movement is below some threshold:
        set object A as not motionary

Sometimes it's separated out into discrete collision checking and the dynamics simulation sections - useful for cases when you care about detecting collisions but don't need things to bounce around or rebound when they touch.

Thank you. I found out some useful data thanks to your tips

Share this post


Link to post
Share on other sites

This topic is 408 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this