I made this application that lets the user draw a line on the screen and the ball bounces off the line realistically with the angle it would bounce at in real life and stuff. But it only worked for a straight line. Now I’m trying to make it curved.
I have decided to feet clear from the calculus method as the matrices involved seem scarier than needed. What I am doing instead is in a -(void)touchesMoved event having it record the current point of the touch in a CGPoint array. Then to find the collision, I have a for loop like for (int i = 2; i <= numberOfPoints; i++) { That flies through all values for i as the numberOfPoints increases. So for collisions I assumed I would get the slope of the line between i and i-1, then get the perpendicular distance from the ball to that line and when it nears zero a collision has ocurred, just as I did in the straight line version.
This does not work however. Does anyone have any tips at all for a better way to do this? I have really stressed all of my options for this one
Using i and i-1 to calculate the slope simplifies it too much. I would suggest:
Widening the distance between the two points to calculate the slope (i and i-4 for example). The reason is that to the human eye, the difference is imperceptible for two consecutive points. The eye will perceive the same slope for points slightly further apart. However, points which are further apart might provide you with a more accurate slope.
If the point of collision is i, try averaging the slopes between (i,i-1), (i,i-2), (i,i-3), (i,i+1), (i,i+2), (i,i+3). I feel that this should also give you a better result.
If I were you, I’d use option 2.
You can also use a mixture of the two options by choosing sets of points that are further apart and then averaging those slopes.
Also, as a side note, I’m assuming you know about this already, but have you heard of Box2D
EDIT:
One more thing… if you can figure out a way to alias the user’s curved line segment into multiple straight line segments, you can simply use your old algorithm.