Exemple #1
0
 public TestEntry(
     CollisionEntity entityA,
     CollisionEntity entityB
     )
 {
     this.EntityA = entityA;
     this.EntityB = entityB;
 }
        /// <summary>
        /// this method is a bit hacky since I did not know where to place it. I may move it to a better
        /// place somewhen in the future
        /// </summary>
        /// <param name="ray"></param>
        /// <param name="entity"></param>
        public bool GetIntersectionPoint(
            ref Ray3 ray,
            Entity entity,
            out Vector3 outIsectPt
            )
        {
            // find the matching collision entity (in order to get the alignedbox3tree
            CollisionEntity collisionEntity = testList.GetCollisionEntity(entity);

            if (collisionEntity == null)
            {
                outIsectPt = Vector3.Zero;
                return(false);
            }

            Debug.Assert(collisionEntity.VolumeType == VolumeType.AlignedBox3Tree);
            if (collisionEntity.VolumeType != VolumeType.AlignedBox3Tree)
            {
                outIsectPt = Vector3.Zero;
                return(false);
            }

            // get world transform of from the given entity
            Matrix          worldTransform;
            AlignedBox3Tree tree = (AlignedBox3Tree)collisionEntity.Volumes[0];

            OrientationHelper.CalculateWorldTransform(entity, out worldTransform);

            // transform the ray into the coordinate system of the entity
            Matrix  worldToEntityTransform = Matrix.Invert(worldTransform);
            Matrix  inverseTransposeWorldToEntityTransform = Matrix.Transpose(Matrix.Invert(worldToEntityTransform));
            Vector3 entitySpaceRayOrigin    = Vector3.Transform(ray.Origin, worldToEntityTransform);
            Vector3 entitySpaceRayDirection = Vector3.Transform(ray.Direction, inverseTransposeWorldToEntityTransform);

            entitySpaceRayDirection.Normalize();
            Ray3 entitySpaceRay = new Ray3(entitySpaceRayOrigin, entitySpaceRayDirection);

            float t; // parameter on ray, outIsectPt = ray.Origin + t * ray.Direction;
            bool  intersection = GetIntersectionPoint(ref entitySpaceRay, tree.Root, tree.Positions, out t);

            if (intersection)
            {
                outIsectPt = Vector3.Transform(entitySpaceRay.Origin + t * entitySpaceRay.Direction, worldTransform);
            }
            else
            {
                outIsectPt = Vector3.Zero;
            }
            return(intersection);
        }
Exemple #3
0
        public void Remove(CollisionEntity entity)
        {
            Debug.Assert(inCollisionDetection == 0);

            for (int i = 0; i < collisionList.Count; ++i)
            {
                if (collisionList[i].EntityA == entity ||
                    collisionList[i].EntityB == entity)
                {
                    collisionList.RemoveAt(i);
                    --i;
                }
            }

            entityList.Remove(entity);
        }
Exemple #4
0
        public void Add(CollisionEntity entity)
        {
            Debug.Assert(inCollisionDetection == 0);

            foreach (CollisionEntity otherEntity in entityList)
            {
                /*if (entity.Entity.HasString(CommonNames.Kind) && entity.Entity.GetString(CommonNames.Kind) == "pillar" &&
                 *  otherEntity.Entity.HasString(CommonNames.Kind) && otherEntity.Entity.GetString(CommonNames.Kind) == "pillar")
                 * {
                 *  continue;
                 * }
                 * if (
                 *  (
                 *      entity.Entity.HasString(CommonNames.Kind) && entity.Entity.GetString(CommonNames.Kind) == "pillar" &&
                 *      otherEntity.Entity.HasString(CommonNames.Kind) && otherEntity.Entity.GetString(CommonNames.Kind) == "powerup") ||
                 *  (otherEntity.Entity.HasString(CommonNames.Kind) && otherEntity.Entity.GetString(CommonNames.Kind) == "pillar" &&
                 *  entity.Entity.HasString(CommonNames.Kind) && entity.Entity.GetString(CommonNames.Kind) == "powerup"))
                 * {
                 *  continue;
                 * }
                 * if (
                 *  (entity.Entity.HasString(CommonNames.Kind) && entity.Entity.GetString(CommonNames.Kind) == "island" &&
                 *  otherEntity.Entity.HasString(CommonNames.Kind) && otherEntity.Entity.GetString(CommonNames.Kind) == "powerup") ||
                 *  (otherEntity.Entity.HasString(CommonNames.Kind) && otherEntity.Entity.GetString(CommonNames.Kind) == "island" &&
                 *  entity.Entity.HasString(CommonNames.Kind) && entity.Entity.GetString(CommonNames.Kind) == "powerup"))
                 * {
                 *  continue;
                 * }*/

                collisionList.Add(new TestEntry(entity, otherEntity));
            }

            /*Console.WriteLine("number of collision tests: {0}", collisionList.Count);
             * if (collisionList.Count == 200)
             * {
             *  foreach (TestEntry entry in collisionList)
             *  {
             *      Console.WriteLine("colliding {0} with {1}", entry.EntityA.Entity.Name, entry.EntityB.Entity.Name);
             *  }
             * }*/

            entityList.Add(entity);
        }