Sign in to follow this  
ekrax

simple physics engine

Recommended Posts

ok i've made a simple physics engine basically just for calculating collisions so it contains all the basic kinematic info for each object and it's mass. it works pretty well, but there is one thing that i just cannot fix no matter how much little exceptions i put into the code. the problem is that once an object becomes at rest it sinks into the object it's resting on. this happens also when objects collide on the x axis (im only using 2D graphics). then when i put in a few if statements that basically are like "if the right side of the object is overlapping the left side of the other object, then move the right side of the object to the left edge of the other object) well this doesn't work, now my blocks stick togther like goo. im dreading it but i think im going to have to re code the whole thing :( the engine works like this ... - test object against another object in the first objects vicinity - test right side of object - if the object overlap, fix it - calculate impulse - test left side of object - if the object overlap, fix it - calculate impulse - test top of object - if the object overlap, fix it - calculate impulse - test bottom of object - if the object overlap, fix it - calculate impulse Calculate Impulse: here is where i have another problem, i have 2 sets of code for testing the impulse becasue some of my objects are part of the level so they are not effected by gravity and have no mass, so i have 2 algorithms for testing the impulse which seems inefficient. when i calculate the impulse i do this. - find difference in velocity - calculate first magnitude based on the difference of velocity and restitution and the other objects mass (M1 = (-((restitution + other.restitution) / 2) * difference) * other.mass) - calculate second magnitude based on the difference of velocity and smoothness and the other objects mass (M2 = (-((smoothness + other.smoothness) / 2) * difference) * other.mass;) - find the resulting velocity by adding the magnitudes togther and dividing them by the sum of the 2 masses so my question is will i have to re code everything? or is there a quick fix to stop objects from sticking and sinking into each other?

Share this post


Link to post
Share on other sites

Maybe the reason they stick together after you move them apart is because you aren't moving them far enough, and there is still just a tiny overlap. thus they keep getting their positions reset?
could be something as subtle as the difference between > and >= checks on the boundries

Share this post


Link to post
Share on other sites
I've encountered sinking as well, and if I'm not mistaken, I think it has to do with the objects being perpendicular to each other, oriented from the origin.

If you are using a swept test, you can see if p1.x == p2.x or p1.y == p2.y and if so add 0.001f to one of them so there won't be any divide by zero errors. Don't know if this will work for you because it doesn't sound like you're using swept tests.

Also, it looks like you're doing too many checks. If you are just using circles, you really only need one test and then apply the impulse to both objects. You do not really need to fix their positions before applying impulse, because the impulse should take their exit velocities into account.

Share this post


Link to post
Share on other sites

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