-
3D Points Equation
I am programming a game in DirectX7 Immediate and I am having a little problem with proper collision detection.
Now the origin of my object is the middle, so to get the 4 points of a rect around the object I need to add/subtract half the width/depth.
Now my object is rotating:
0 it is pointing right,
90 pointing north sitting directly on the z axis)
180 it is pointing left
270 pointing south on the z-axis
Now, how em I suppose to add the width and depth to the origin x,z when the object is no longer sitting on angle 0.
I realize I need to use trig, but I do not know how.
-
I'm not sure about the coordinate system you're using. I'm assuming you're using a left hand coordinate system, with the x-axis pointing to the right, y-axis to the top and z-axis into the screen.
Suppose the location of the object is P. The rectangle is bounded by the points A (bottom left), B (top left), C (top right) and D (bottom right). Fruthermore, W = width and H = height of the rectangle.
When angle = 0, then A = (P.x, P.y - 0.5H, P.z + 0.5W), B = (P.x, P.y + 0.5H, P.z + 0.5W), C = (P.x, P.y + 0.5H, P.z - 0.5W), D = (P.x, P.y - 0.5H, P.z - 0.5W).
The y-values of the points remain the same all the time, since you're rotating about the y-axis. The valus of the x- and z-axes change with the angle.
Here are the formulas
A = (P.x - 0.5W * sin(angle), P.y - 0.5H, P.z + 0.5W * cos(angle)),
B = (P.x - 0.5W * sin(angle), P.y + 0.5H, P.z + 0.5W * cos(angle)),
C = (P.x + 0.5W * sin(angle), P.y + 0.5H, P.z - 0.5W * cos(angle)),
D = (P.x + 0.5W * sin(angle), P.y - 0.5H, P.z - 0.5W * cos(angle))
I hope this works, since I can't test it in DirectX. These formulas might need some sign reversals, if my assumption on the coordinate system is incorrect.