Precise collision detection with the border
To upgrade our detect
method, we need to replace the return statement in the if(possibleCollision)
block with the more precise detection code.
First, initialize radianAngle
to be the radian equivalent of whichever direction (in degrees) our object is facing. The Math
class uses radians as they are more mathematically useful in calculations than the easier to visualize degree measurement.
The variables cosAngle
and sinAngle
are just what the name suggests, and are used in the block of code which follows this one.
Tip
It is worth mentioning that the Math.cos()
and Math.sin()
methods are relatively time consuming. We can speed up our collision detection class by precomputing 360 values for both sin
and cos
and then using a simple lookup method instead of this calculation.
However, we maintain our goal of over 60 frames per second, so don't do so here.
Delete the return statement and add this code in the if(possibleCollision)
block:
if (possibleCollision...