How do I have two objects bounce off each other?

0 favourites
  • 3 posts
From the Asset Store
Hand-painted tiles, objects, animated objects, and background to build a colorful Mayan civilization environment.
  • Hi,

    How do I have two objects bounce off each other when they collide? Is there a simple way to do this or do I need to build collision detection, if statements about the direction the objects were facing, etc.., I am using pathfinding and not bullets.

    In other words, think bumper cars at a carnival. How do I create a slight bounce into the other direction for two objects? The objects use pathfinding to find each other and move. My thought is to create a condition for collision detection then build sub events based on the direction the objects were moving in when they collided, then adjust their X and/or Y values to move them slightly to the opposite direction.

    Just wondering if there's an easier way or I am missing something. Well, both then. Haha

    Thank you much

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You have the right idea. To bounce you’d need the angle between where the objects hit. But that can be simplified to just the angle from one object to another. Then you can do the bounce in two ways. One is to use the angle of motion and the other is to use xy velocities.

    The angle of motion way.

    Var n=0
    A collides with B
    — set n to angle(a.x,a.y,b.x,b.y)
    — a: set angle of motion to 2*(n+90)-self.angleofmotion
    y)
    — b: set angle of motion to 2*(n+90)-self.angleofmotion

    I’m short on time to write the xy way. But it is expandable to handle differing masses.

    However the one snag in all this is the pathfinding behavior. Does it even let you change the velocity? It mostly just moves along the found path and doesn’t really handle deviations from that path.

    Maybe have do the enemies with two objects. One with pathfinding and the other with physics that gets a force applied to move toward the pathfinding object. Maybe stop the pathfinding object when it’s too far from the pathfinder so it can catch up

  • Thank you. I will check it out. It makes sense.

    I did the x,y as a simple test the other day and it works. The objects continue along their path like a bumper car or pinball machine bounce over and over again which is fine. I could also run a find path after the collision and the x,y adjustment or a stop path finding for a "dizzy" or "stunned" effect using a wait N seconds before starting again.

    So many possibilities! Fun!

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