I am try to put AAA gunner AI into my engine, but having a problem.
I use an entity component system, so I created a AAAGunFOFComponent to hold the valid fields of fire for each weapon.
Weapons can have multiple fields of fire, you don't want to shoot the tail off your own aircraft, or the bridge of your ship.
The component is defined as heading, max pitch, min pitch, max yaw and min yaw, (range is defined by the weapon)
From this I calculate the four corners in object space and then average them to get the far point of the view volume.
I then calculate a perspective matrix from the pitch and yaw ranges, and a view matrix from the position and target.
These two give me a BoundingFrustum I can use to check for valid targets.
It almost works.
The bounding frustum is rubbish, I am not sure why.
An image can show what I mean better than a lot of words
The white line is the guns forward vector.
The red line is the target vector
The yellow the bounding frustum.
The component has the following values
YawMin Float -45
YawMax Float 45
PitchMin Float -10
PitchMax Float 75
BaseHeading Float 90
Hook String ShellStart_51
GunType String AAA\Oerlikon20mm
RoundType Int 0
Mesh String Head50_x
AmmoType String AAA\aa20mm
The pitch range is 10 degrees below horizontal to 75 degrees above, you can clearly see that does not match the display.
Here is the relevant code
float x_range = sf.YawMax - sf.YawMin;
float y_range = sf.PitchMax - sf.PitchMin;
float aspect_ratio = x_range / y_range;
Matrix p = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(x_range), aspect_ratio, near, far);
Matrix l = Matrix.CreateLookAt(start, start + target, Vector3.Up);
frustrum = new BoundingFrustum(l * p);
Any ideas guys?