Manual collisions

0 favourites
  • 7 posts
  • Hello. For a while now I've been doing some tests with some theories or physics laws, and I wanted to make manual collisions, without physics behavior, since it's way too complicated for something this simple - I don't need friction, rotation or collision polygons just to name a few.

    I've tried a bunch of formulas, both that I have tried to come up with and some I've made with a little advice from some people online. So far none have worked.

    I came across this, which someone said would be able to explain how to find the velocity of an object after collision with another, but all I can see is:

    And that involves the end result of the second particle, which means I can't use it for actually calculating one of the answers in the first place.

    Does anyone know what I should do?

    Thanks.

    Sumy

  • Can you describe what your end result would be like? How do you imagine it working?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Prominent

    Basically the conservation of momentum.

    Two objects collide and are given force apart relative to their mass.

    If two objects with mass 1 collide at 10m/s, both receive velocity of 5m/s in opposite directions.

  • Sumyjkl , I think the answer to that is on the page you linked..

    I'll try to rewrite it in more gamer language.

    obj1_finalVelocityX = ((obj1_mass - obj2_mass) / (obj1_mass + obj2_mass)) * obj1_currentVelocityX + ((2 * obj2_mass) / (obj1_mass + obj2_mass)) * obj2_currentVelocityX

    obj2_finalVelocityX = ((obj2_mass - obj1_mass) / (obj1_mass + obj2_mass)) * obj2_currentVelocityX + ((2 * obj1_mass) / (obj1_mass + obj2_mass)) * obj1_currentVelocityX

    That is just for the X velocities, and you can do the same for Y.

    Depending upon how the objects collide(which side) you'd adjust either the X velocities or the Y velocities. So you'd need a way to determine which side is colliding.

    One way is to determine how far the object overlaps each other in the x and y axis, and depending upon which depth is shorter, use that axis.

    The RIGHT side of obj1, should check with the LEFT side of obj2. abs(obj1.boundingboxright-obj2.boundingboxleft).

    The LEFT side of obj1 should check with the Right side of obj2. abs(obj1.boundinboxleft-obj2.boundingboxright).

    Similarly for the Top, and Bottom..

    Then check which value is smallest, to determine which velocities to adjust.

  • Prominent

    Hmm, tried it and seemed to do something similar to what I was having trouble with before.

    Since I'm working with particles, everything is considered circular.

    Here's the capx I'm working on if you'd like to have a look at it. It's completely un-optimized, and incredibly rudimentary, so be ready for that.

    https://www.mediafire.com/?1mtseq1sjstesb3

    Edit:

    Wow. Tested something just then, and it worked. Mathematically, this formula would describe each particle's velocity if mass was equal:

    v = A.vel - (total magnitude of velocity) * ('polarity')

    Polarity meaning whether it is positive or negative.

    ∴ v = A.vel - (|A.vel|+|B.vel|) * (A.vel / |A.vel|)

    This works for perfectly straight collisions of objects with equal mass. Currently figuring out how to add mass into it.

    Edit: must have been imagining it. Doesn't do anything at all.

  • For circular collisions, you'll need to find the normal of the surface where the collision occurs and the vector perpendicular to it. Then you have to project the velocities onto those vectors and apply the momentum stuff to the projected vectors I think..

  • The two equations you need to calculate collision impulse are:

    Total momentum before = total momentum after

    m1*v1i + m2*v2i = m1*v1f + m2*v2f

    "i" means initial and "f" means final.

    We also need another equation so we can solve for the two unknowns (v1f and v2f). Since we want perfectly elastic collisions we can use:

    total kinetic energy before = total kinetic energy after

    0.5*m1*v1i^2 + 0.5*m2*v2i^2 = 0.5*m1*v1f^2 + 0.5*m2*v2f^2

    So with some algebra I was able to calculate it as:

    v1f = (v1i*(m1-m2) + 2*v2i*m2)/(m1+m2)

    v2f = (-v2i*(m1-m2) + 2*v1i*m1)/(m1+m2)

    Now those velocities are in only one dimension. For 2d collisions if we only need to consider the speeds along the normal between the two objects, which is still is one dimensional.

    So the steps would be:

    1. calculate the normal between the two objects.

    --- normalx=cos(angle from object1 to object2)

    --- normaly=sin(angle from object1 to object2)

    2. calculate v1i and v2i only in direction of the normal.

    --- v1i = normalx*object1.velocityx+normaly*object1.velocityy

    3. use the equations above to calculate v1f and v2f.

    4. apply v1f and v2f back onto the object's velocties.

    --- add normalx*(v1f-v1i) to object1 velocityx

    --- add normaly*(v1f-v1i) to object1 velocityy

    Working example:

    https://dl.dropboxusercontent.com/u/542 ... ounce.capx

    You can also incorporate a coefficient of restitution into the impulse equation so you can anything from a "perfectly elastic collision" to an "inelastic collision".

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)