-
Accurate Angles
I am working on something in which you draw a line and then watch the ball interact with the line. I have a working gravity engine and the bounce works alright, but I am looking for a way to make more accurate horizontal movement. The system I am using right now for horizontal movement moves the same distance every bounce of the ball. I am looking for a way to make the ball bounce according to the angle or slope of the area it lands on. I have an example of what I have so far. Press the space bar to start the ball motion.
http://putstuff.putfile.com/76865/9381026
-
Re: Accurate Angles
Well first of all you need to find the angle that the ball is traveling when it hits an obstacle. You can do that by storing it's coordinates before every move so you can have a record of where it was the previous frame. Then you just get the angle between those two points (I use the clickteam movement controller object for that). Since it's a ball, this makes finding the collision angle so much easier. What you do is you loop through all the edge pixels of the ball and see which ones are obstacles, then you average it up to get the normal line angle. Then you use this formula to calculate the new angle (I think it goes something like): normal line angle + (normal line angle - current angle)
Then you have to find the speed at which it is travelling by taking the distance between it's previous frame point, and it's current point (you can use the clickteam movement controller for this also). Then using the new angle you calcualted, you can then set the x and y velocities using this formula:
xVel = (sin(newangle)*speed)
yVel = (cos(newangle)*speed)
-
Re: Accurate Angles
Thanks thats really helpful!
-
Re: Accurate Angles