Can someone help me with calculating collision angles using speed? (basically physics)

0 favourites
  • 8 posts
From the Asset Store
Change the size and position of everything without calculating anything!
  • I have a top-down game and with no gravity. I'm not using Physics Behavior. I'd like to calculate correctly the redirection angles after 2 objects collide. Ideally I'd like to take their speed and mass into account to change their speed accordingly. And by mass I mean a handful of mass sizes: small, medium, large, giant. But if that's too intensive, then I would settle for just the correct angles.

    currently I'm taking the two objects and comparing their moving angles. If the other is within 180 degrees of its own moving angle I apply "force" in the form of acceleration to the player and then have the Other use "Bounce off".

    It looks wonky sometimes but works most of the time, however I would love to see how the calculations would look using physics formulas to redirect angles and speeds.

    These are the variables I have:

    • Other.Bullet.Speed
    • Other.Bullet.AngleOfMotion
    • Other.mass
    • Player.CustomMovement.Speed
    • Player.CustomMovement.MovingAngle
    • Player.mass

    (gravity off)

    On Collision

    Thanks for the help in advance!

    EDIT: even if all object had the same mass that would be totally fine for me. I'm just really curious how difficult the math would be to do this.

    Tagged:

  • Here’s the actual math to do it. Equation 5 to calculate the impulse and 1a,1b to set the new speeds.

    en.m.wikipedia.org/wiki/Collision_response

    You don’t have to do the angular stuff so it can be simpler.

    Sprite collides with other

    Set a to angle(sprite.x,sprite.y,other.x,other.y)

    set j to -(1+e)*((other.vx-sprite.vx)*cos(a)+(other.vy-sprite.vy)*sin(a))/(1/Sprite.mass+1/other.mass)

    Sprite: set vx to self.vx-j/self.mass*cos(a)

    Sprite: set vy to self.-j/self.mass*sin(a)

    Other: set vx to self.vx+j/self.mass*cos(a)

    Other: set vy to self.vy+j/self.mass*sin(a)

    That’s with three variables

    a = the angle from sprite to other

    e = bounce amount. 0 to 1 range

    j = the impulse

    Also I used vx and vy for the velocities. You can get that from angle and speed with:

    Vx=speed*cos(angle)

    Vy=speed*sin(angle)

    And vice versa

    Speed = distance(0,0,vx,vy)

    Angle = angle(0,0,vx,vy)

    Anyways hope that helps.

  • R0J0hound Thanks for the reply!

    getting close, but somehow the player is going the opposite direction than it should.

  • NEVERMIND! I had a calculation error in the new vx/vy

    omg, it looks so awesome! thanks R0J0hound!!!!

  • Glad it works! I always thought it was a cool equation.

    There are a few ways to improve it later.

    For one I just used the angle from one object to the other for the collision normal. Realistically it should be the angle from one contact surface to the other.

    The other thing you can do is actually calculate the point or points of collision and then adding the rotational motion stuff.

  • R0J0hound I do use the Rotation behavior on the "other", not the player since they have movement control. It would be cool to figure out a new rotation speed after the collision, but it seems much more complicated! Also I do not have others colliding with others since I have a couple hundred of them out at a time, it seems like it would effect performance. I already have enough jank!

    I'm very happy with this! I could look at this all day! thanks so much again!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Here is an update with rotations, contact point calculations and friction. Although the friction model seems to not be working because when objects are pushed apart it causes sliding. Maybe next time I'll go deeper down the physics rabbit hole.

    dropbox.com/s/fssu2ujynyj3zia/simple_event_based_physics.capx

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