//Checks if 2 objects are in sight using walls public bool isInSighten(GameObject one, GameObject two) { Vector3 pos = one.Position + (Vector3.Up / 2.0f); Vector3 dir = two.Position - one.Position; dir.Normalize(); Ray ray = new Ray(pos, dir); //Check if the ray intersects with any wall before hitting the other object float? enemyDist = ray.Intersects(two.BoundingBox); bool inSight = false; if (enemyDist != null) { inSight = true; foreach (BoundingBox box in levelCollisions.LevelCollisionBoxes) { float? distWall = ray.Intersects(box); if (distWall != null && distWall < enemyDist) { inSight = false; break; } } } return inSight; }
//Checks the same as in sight but only in the direction the first object is facing public bool isInDirection(GameObject one, GameObject two) { Matrix forwardMovement = Matrix.CreateRotationY(one.Rotation); Vector3 direction = Vector3.Transform(Vector3.Forward, forwardMovement); direction.Normalize(); Vector3 pos = one.Position + (Vector3.Up / 2.0f); Ray ray = new Ray(pos, direction); float? enemyDist = ray.Intersects(two.BoundingBox); bool inSight = false; if (enemyDist != null) { inSight = true; foreach (BoundingBox box in levelCollisions.LevelCollisionBoxes) { float? distWall = ray.Intersects(box); if (distWall != null && distWall < enemyDist) { inSight = false; break; } } } return inSight; }