Perfect bounce from round objects. The math is killing me! Help!
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
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
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
to50*(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
to50*(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:
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)
The current angle the site is referring to is the angle at which the object is moving. To find it normally you would usevectorToAngle
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.
jw what game play needs that exact calculations
It can also be used for many kind of puzzle games.
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
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.