Perfect bounce from round objects. The math is killing me! Help!

MarkOnTheIronMarkOnTheIron Member Posts: 1,447
edited December 2011 in Working with GS (Mac)
We all know that the collide behavior usually don't perform well.

That's why when bouncing from straight walls I (and I think many other) use some rules to change the motion.Linear Velocity.X and Y attributes.

Now I need to get perfect bounces from a round object and I have serious problems with the math. I already managed to get the angle between the round object and the bouncing actor. I used it in some expressions involving cos and sin functions but I'm having serious troubles on putting all together in a way that works.

Since the part of my brain that learned math is long gone, I need some help on that.



________________________________
【ツ】iPhone Icon Pack (compatible with DBA Icon Creator) 【ツ】 - 【ツ】Graphic Pack【ツ】
Free GS demos: High score simple and advanced; Game Center; App Rating System; Custom Font Score and Countdown; Advanced Snap to Grid

Comments

  • MarkOnTheIronMarkOnTheIron Member Posts: 1,447
    EUREKA!!! I found the solution!
    @tshirtbooth said:
    search GS for Magnitude DEMO
    its a demo my firemaple, i think his balls do what your trying to do.
    Unfortunately I already saw that demo and there are only the rules for bouncing from straight walls, the balls bounced between themselves with the collision behavior.


    However as I said I found a solution! It took some googling and a big headache but finally I stumbled on this site that pointed me to the right direction and the result was:

    Change Attribute self.motion.Linear Velocity.X to 50*(cos((((vectortoangle(self.positionX-OtherObjectX, self.positionY-OtherObjectY))+90)*2)-(vectortoangle((self.motion.Linear Velocity.X/50), (self.motion.Linear Velocity.Y/50)))))

    Change Attribute self.motion.Linear Velocity.Y to 50*(sin((((vectortoangle(self.positionX-OtherObjectX, self.positionY-OtherObjectY))+90)*2)-(vectortoangle((self.motion.Linear Velocity.X/50), (self.motion.Linear Velocity.Y/50)))))

    What opened my eyes was this phrase:
    When two particles collide, we want them bounce off each other. Theoretically, when two circular particles collide they contact at an infinitely small point. The angle of this point is the tangent of the particle at this point. We can treat the collision as though the particles were bouncing off a flat surface with the angle of the tangent.
    image

    So, the first thing to find is the angle of the tangent between the colliding objects. After thinking on a math expression to calculate it I realized, by looking at the image above, that the tangent's angle is exactly perpendicular to the angle between the two objects. Finding the angle between the objects is simple:

    vectortoangle(self.positionX-OtherObjectX, self.positionY-OtherObjectY)

    So if the tangent is perpendicular to find it you just need to add 90:

    (vectortoangle(self.positionX-OtherObjectX, self.positionY-OtherObjectY))+90)
    We can treat the collision as though the particles were bouncing off a flat surface with the angle of the tangent. To reflect the angle of the particles on this surface, we subtract their current angle from two times the tangent: 2*tangent - angle
    The current angle the site is referring to is the angle at which the object is moving. To find it normally you would use vectorToAngle and you will input a starting position X and Y and a finish position X and Y. But that would be too simple and boring right? So I studied the motion velocities for X and Y and I saw that they were proportional to the sin and cos of the object moving angle and to its velocity. For example if the object velocity is 50 the motion velocity of X and Y would be:

    self.motion.Linear Velocity.X = 50*cos(angle) self.motion.Linear Velocity.Y = 50*sin(angle)

    To find the angle at which the object is moving then you will use:

    (vectortoangle((self.motion.Linear Velocity.X/50), (self.motion.Linear Velocity.Y/50)))

    Now you have all the elements to input to find the collision angle. Remember that sin is for the Y motion and cos is for the X motion.

    So far it's working. Please note, however, that this method can be very heavy on the performance and shouldn't be used on too many actors at once.
  • JohnPapiomitisJohnPapiomitis Member Posts: 6,256
    thanks for posting your solution mark, some good expressions there :)
  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,881
    This is great! Thanks for sharing the answer to a very challenging question.
  • MarkOnTheIronMarkOnTheIron Member Posts: 1,447
    Thanks guys. Hopefully it will help someone avoid some serious headaches :)
  • morphinegamingmachinemorphinegamingmachine Member, PRO Posts: 449
    what needs to be so precise that u cant use the circular collision option gs has built in?

    jw what game play needs that exact calculations
  • MarkOnTheIronMarkOnTheIron Member Posts: 1,447
    @morphinegamingmachine said:
    what needs to be so precise that u cant use the circular collision option gs has built in?
    As I said on my first post the collision behavior does not perform too well in certain situations. For example it doesn't maintain the angle at which your bouncing actor is moving. Also it is hard to make the object to maintain a constant speed. The last reason is that I'm a stupid perfectionist :)
    what game play needs that exact calculations?
    There are many kind of games that may take advantage from this methods. The first that come to my mind are breakout games, try to play a little with the template made by GS and you will understand the need for good bounces.

    It can also be used for many kind of puzzle games.
  • MarkOnTheIronMarkOnTheIron Member Posts: 1,447
    I've been asked by another user for a demo of this method.

    I think it's fair enough to share it with everyone. Beware that it still buggy but this is the best I was able to came up so far with GS.

    http://dl.dropbox.com/u/8244920/Bouncing.zip


    ________________________________
    【ツ】iPhone Icon Pack (compatible with DBA Icon Creator) 【ツ】 - 【ツ】Graphic Pack【ツ】
    Free GS demos: High score simple and advanced; Game Center; App Rating System; Custom Font Score and Countdown; Advanced Snap to Grid
  • PBEmpirePBEmpire Member Posts: 676
    hey there! i am having problems with a similar problem. would u mind looking at it? if u would like, email me at gamesaladapps@gmail.com
  • ORBZORBZ Member Posts: 1,304
    edited August 2012
    Hahaha, and they say GS doesn't require you to think like a programmer. :)

    Btw, if GS had vector data types this would be trivial.

    check out the atan function

    In all seriousness, I don't understand why you wouldn't just use the built in physics engine.
Sign In or Register to comment.