I have this nice drawing:
I know the ball's center and the rectangle's center, but how do I know (programming C#) what side of the rectangle the ball is?
By my drawing, the answer in this case should be the right side.
What side of A is B on? Using this info (see Solution 3) you can determine which side of a line A point (or points) B, is on.
For your particular scenario, you need to check which of four sides of the rectangle, the circle is in. In this case, you can treat your circle as just it's centre/origin point. Now you need to look at the rectangle like this:
This will tell you if the point falls in to the bowl-shaped area on each of the four sides of the rectangle shown above.
The "correct" side is determined by you, when you construct the formula in the link given above. It depends on whether you specify a line as PQ or it's reverse, QP. This is known as winding order (see "Winding Order of Vertices").
|show 4 more comments|
I do this by giving all of my game entities a position (Vector 2D/3D), and a rotation (Matrix 3x3). Your rotation matrix will have either a column or row for its forward, up, and right vectors. In the case of a 2D game, the 'up' vector is unnecessary.
First you get the vector from your square's center to the ball's center.
Now you normalize/unitize the vector to make it represent a unit-length direction
Unitizing requires knowing the length of your vector, so we make a helper method for that as well
Ok, now that we have a direction from the square to the ball, defined earlier by the 2D vector we called
Now we have the
And finally, here's your dot-product method:
And here are some helpful links if you want to learn more about the processes involved in the above code: