Game Development Reference
In-Depth Information
Now that the mouse pointer is working, we can return to the intersection code.
The update loop for the state will do the intersection check.
public void Update(double elapsedTime)
{
if (_circle.Intersects(_input.MousePosition))
{
_circle.Color = new Color(1, 0, 0, 1);
}
else
{
// If the circle's not intersected turn it back to white.
_circle.Color = new Color(1, 1, 1, 1);
}
}
This shows how the intersect function will be used; all that's left is to write it.
The test requires a number of vector operations; therefore, the point object is
converted to a vector.
public bool Intersects(Point point)
{
// Change point to a vector
Vector vPoint = new Vector(point.X, point.Y, 0);
Vector vFromCircleToPoint = Position - vPoint;
double distance = vFromCircleToPoint.Length();
if (distance > Radius)
{
return false;
}
return true;
}
Run the program and observe what happens when the cursor is moved in and
out of the circle.
Rectangles
The rectangle intersection code doesn't need to be completed. The only rec-
tangles we need are buttons, and they'll always be axis aligned. This makes the
code far simpler than dealing with arbitrarily aligned rectangles.
 
Search WWH ::




Custom Search