Reputation: 3051
I am implementing a 2D football game. For my pass algorithm, I calculate the slope between the passer and reciever with the equation m = (y2-y1)/(x2-x1)
In some conditions x2
and x1
are equal resulting in a divide by zero exception. What should I do in this case?
I did not ask this question on math.stackexchange.com because it's a programming question.
Upvotes: 0
Views: 4036
Reputation: 78316
Don't use the slope of a line for this sort of operation, for just the reason you have stumbled across. Use instead the angle of the line from passer to passee. Apply a little trigonometry. If that little trigonometry is beyond you, delete this question, do some work, then post again when you get stuck again.
EDIT
I suggest you use trigonometry for 2 reasons.
Upvotes: 4
Reputation: 200
If you would like to skip the Trig application, you could check if(x1 != x2) //whatever math needs to be done
This way, if the x's are equal, you could set your own value for a "flat pass" in the else statement and never have to worry about a divide by zero exception again.
Good luck with whatever route you choose! (I would recommend using Trig if you have the time to learn it (if necessary))
Upvotes: 1