// Find the AbstractVehicle whose screen position is nearest the given window // coordinates, typically the mouse position. Returns NULL if there are no // AbstractVehicles. // // This works by constructing a line in 3d space between the camera location // and the "mouse point". Then it measures the distance from that line to the // centers of each AbstractVehicle. It returns the AbstractVehicle whose // distance is smallest. // // xxx Issues: Should the distanceFromLine test happen in "perspective space" // xxx or in "screen space"? Also: I think this would be happy to select a // xxx vehicle BEHIND the camera location. internal static IVehicle findVehicleNearestScreenPosition(int x, int y) { // find the direction from the camera position to the given pixel Vector3 direction = DirectionFromCameraToScreenPosition(x, y); // iterate over all vehicles to find the one whose center is nearest the // "eye-mouse" selection line float minDistance = float.MaxValue; // smallest distance found so far IVehicle nearest = null; // vehicle whose distance is smallest IEnumerable <IVehicle> vehicles = AllVehiclesOfSelectedPlugIn(); foreach (IVehicle vehicle in vehicles) { // distance from this vehicle's center to the selection line: float d = vehicle.Position.DistanceFromLine(Camera.Position, direction.FromXna()); // if this vehicle-to-line distance is the smallest so far, // store it and this vehicle in the selection registers. if (d < minDistance) { minDistance = d; nearest = vehicle; } } return(nearest); }
// draw a box around a vehicle aligned with its local space // xxx not used as of 11-20-02 public static void DrawBoxHighlightOnVehicle(IVehicle v, Color color) { if (v != null) { float diameter = v.Radius * 2; Vector3 size = new Vector3(diameter, diameter, diameter); Drawing.DrawBoxOutline(v, size.FromXna(), color); } }
public void FromXna() { Vector3 xna = new Vector3(6, 7, 8); Vector3 v = Vector3.FromXna(xna); Assert.AreEqual(xna.X, v.X); Assert.AreEqual(xna.Y, v.Y); Assert.AreEqual(xna.Z, v.Z); }
// ground plane grid-drawing utility used by several plug-ins public static void GridUtility(Vector3 gridTarget) { // Math.Round off target to the nearest multiple of 2 (because the // checkboard grid with a pitch of 1 tiles with a period of 2) // then lower the grid a bit to put it under 2d annotation lines Vector3 gridCenter = new Vector3((float)(Math.Round(gridTarget.X * 0.5f) * 2), (float)(Math.Round(gridTarget.Y * 0.5f) * 2) - .05f, (float)(Math.Round(gridTarget.Z * 0.5f) * 2)); // colors for checkboard Color gray1 = new Color(new Vector3(0.27f)); Color gray2 = new Color(new Vector3(0.30f)); // draw 50x50 checkerboard grid with 50 squares along each side Drawing.DrawXZCheckerboardGrid(50, 50, gridCenter.FromXna(), gray1, gray2); // alternate style //Bnoerj.AI.Steering.Draw.drawXZLineGrid(50, 50, gridCenter, Color.Black); }
// ground plane grid-drawing utility used by several plug-ins public static void GridUtility(System.Numerics.Vector3 gridTarget) { // Math.Round off target to the nearest multiple of 2 (because the // checkboard grid with a pitch of 1 tiles with a period of 2) // then lower the grid a bit to put it under 2d annotation lines Vector3 gridCenter = new Vector3((float)(Math.Round(gridTarget.X * 0.5f) * 2), (float)(Math.Round(gridTarget.Y * 0.5f) * 2) - .05f, (float)(Math.Round(gridTarget.Z * 0.5f) * 2)); // colors for checkboard Color gray1 = new Color(new Vector3(0.27f)); Color gray2 = new Color(new Vector3(0.30f)); // draw 50x50 checkerboard grid with 50 squares along each side Drawing.DrawXZCheckerboardGrid(50, 50, gridCenter.FromXna(), gray1, gray2); // alternate style //Bnoerj.AI.Steering.Draw.drawXZLineGrid(50, 50, gridCenter, Color.Black); }