/// <summary> /// Chaining method for adding an obstacle to this Section. /// It initializes bounding boxes and stores in Collidable Obstacles. /// </summary> /// <param name="obstacleEntity">The entity containing the obstacle</param> /// <param name="useSubBoundingBoxes">true to use the bounding boxes of the sub-meshes</param> /// <returns></returns> public Section AddObstacleEntity(Entity obstacleEntity, bool useSubBoundingBoxes) { // Attach it in ModelEntity RootEntity.AddChild(obstacleEntity); // Get and add bb to CollidableObstacles var modelComponent = obstacleEntity.Get<ModelComponent>(); var collidableObstacle = new Obstacle { Entity = obstacleEntity }; if (useSubBoundingBoxes) { // Use bounding boxes from parts of the obstacle. foreach (var mesh in modelComponent.Model.Meshes) { var boundingBox = mesh.BoundingBox; var nodeIndex = mesh.NodeIndex; while (nodeIndex >= 0) { var node = modelComponent.Model.Skeleton.Nodes[nodeIndex]; var transform = node.Transform; var matrix = Matrix.Transformation(Vector3.Zero, Quaternion.Identity, transform.Scale, Vector3.Zero, transform.Rotation, transform.Position); Vector3.TransformNormal(ref boundingBox.Minimum, ref matrix, out boundingBox.Minimum); Vector3.TransformNormal(ref boundingBox.Maximum, ref matrix, out boundingBox.Maximum); nodeIndex = node.ParentIndex; } collidableObstacle.BoundingBoxes.Add(boundingBox); } } else { // Use bounding box of the whole model collidableObstacle.BoundingBoxes.Add(modelComponent.Model.BoundingBox); } CollidableObstacles.Add(collidableObstacle); return this; }
/// <summary> /// Chaining method for adding an obstacle to this Section. /// It initializes bounding boxes and stores in Collidable Obstacles. /// </summary> /// <param name="obstacleEntity">The entity containing the obstacle</param> /// <param name="useSubBoundingBoxes">true to use the bounding boxes of the sub-meshes</param> /// <returns></returns> public Section AddObstacleEntity(Entity obstacleEntity, bool useSubBoundingBoxes) { // Attach it in ModelEntity RootEntity.AddChild(obstacleEntity); // Get and add bb to CollidableObstacles var modelComponent = obstacleEntity.Get<ModelComponent>(); var collidableObstacle = new Obstacle { Entity = obstacleEntity }; if (useSubBoundingBoxes) { // Use bounding boxes from parts of the obstacle. foreach (var mesh in modelComponent.Model.Meshes) collidableObstacle.BoundingBoxes.Add(mesh.BoundingBox); } else { // Use bounding box of the whole model collidableObstacle.BoundingBoxes.Add(modelComponent.Model.BoundingBox); } CollidableObstacles.Add(collidableObstacle); return this; }
private static bool DetectCollision(ref BoundingBox agentBB, Obstacle obst) { var objTrans = obst.Entity.Transform; objTrans.UpdateWorldMatrix(); var objWorldPos = objTrans.WorldMatrix.TranslationVector; foreach (var boundingBox in obst.BoundingBoxes) { var minVec = objWorldPos + boundingBox.Minimum; var maxVec = objWorldPos + boundingBox.Maximum; var testBB = new BoundingBox(minVec, maxVec); if (CollisionHelper.BoxContainsBox(ref testBB, ref agentBB) != ContainmentType.Disjoint) return true; } return false; }