Beispiel #1
0
 public virtual CollisionBox GetCollisionBox(int x, int y, int z)
 {
     CollisionBox box = new CollisionBox();
     box.origin = new Vector3(x + 0.5f, y + 0.5f, z + 0.5f);
     box.size = new Vector3(0.5f, 0.5f, 0.5f);
     return box;
 }
        public static CollisionObject TrsyCollide(CollisionObject objectA, CollisionBox[] objects)
        {
            bool moveX = true, moveY = true, moveZ = true;

            //Get some collision boxes
            CollisionBox nextFrameBox = objectA.velocityBoxOffset,
                        currentbox = objectA.collisionBox;

            //Get the individual velocity vectors
            Vector3 stepX = new Vector3(objectA.preVelocity.X, 0, 0),
                    stepY = new Vector3(0, objectA.preVelocity.Y, 0),
                    stepZ = new Vector3(0, 0, objectA.preVelocity.Z);

            Vector3 prelocation = objectA.prePosition + objectA.collisionBox.offset;

            nextFrameBox.origin = prelocation;
            currentbox.origin = prelocation;

            //Set post stuff to pre stuff
            objectA.postPosition = objectA.prePosition;
            objectA.postVelocity = objectA.preVelocity;

            for (int i = 0; i < objects.Length; i++) {
                CollisionBox objectB = objects [i];

                nextFrameBox.origin = prelocation + stepX;
                if(Intersects (nextFrameBox, objectB)){
                    moveX = false;
                }
                nextFrameBox.origin = prelocation + stepY;
                if(Intersects (nextFrameBox, objectB)){
                    moveY = false;
                }
                nextFrameBox.origin = prelocation + stepZ;
                if(Intersects (nextFrameBox, objectB)){
                    moveZ = false;
                }
            }

            const float mult = 0.1f;
            if (moveX) {
                objectA.postPosition.X += objectA.preVelocity.X;
            }
            else
            {
                objectA.postVelocity.X *= mult;
            }
            if (moveY) {
                objectA.postPosition.Y += objectA.preVelocity.Y;
            }
            else
            {
                objectA.postVelocity.Y *= mult;
            }
            if (moveZ) {
                objectA.postPosition.Z += objectA.preVelocity.Z;
            }
            else
            {
                objectA.postVelocity.Z *= mult;
            }
            return objectA;
        }
 public static Vector3 MoveOverlap(CollisionBox moveBox, CollisionBox staticBox)
 {
     Vector3 moveMin = moveBox.min;
     Vector3 moveMax = moveBox.max;
     Vector3 staticMin = staticBox.min;
     Vector3 staticMax = staticBox.max;
     return new Vector3(Overlap(moveMin.X, moveMax.X, staticMin.X, staticMax.X),
                        Overlap(moveMin.Y, moveMax.Y, staticMin.Y, staticMax.Y),
                        Overlap(moveMin.Z, moveMax.Z, staticMin.Z, staticMax.Z));
 }
 public static bool Intersects(CollisionBox boxA, CollisionBox boxB)
 {
     return !((boxA.min.X > boxB.max.X) || (boxA.max.X < boxB.min.X) ||
              (boxA.min.Y > boxB.max.Y) || (boxA.max.Y < boxB.min.Y) ||
              (boxA.min.Z > boxB.max.Z) || (boxA.max.Z < boxB.min.Z));
 }
        public static CollisionObject Collision(CollisionObject ourObject, CollisionObject[] objects, CollisionBox[] boxes)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            bool[] move = new bool[3]{true, true, true};

            foreach(CollisionBox box in boxes)
            {
                bool[] movement;
                CollideStatic(ourObject, box, out ourObject, out movement);
                move = CompareMovement(movement, move);
            }

            for(int i = 0; i < objects.Length; i++)
            {
                CollisionObject obj = objects[i];
                bool[] movement;
                CollideMoving(ourObject, obj, out ourObject, out obj, out movement);
                move = CompareMovement(movement, move);
            }

            CollisionObject returnObject = MoveObject(move, ourObject);

            sw.Stop();
            Total += sw.ElapsedMilliseconds;
            Runs += 1;

            return returnObject;
        }
        public static void CollideStatic(CollisionObject movingObject, CollisionBox staticBox, out CollisionObject outObject, out bool[] move)
        {
            bool[] movement = new bool[]{true, true, true};
            CollisionBox nextFrameBox = movingObject.velocityBoxOffset;

            Vector3 stepX = new Vector3(movingObject.preVelocity.X, 0, 0),
            stepY = new Vector3(0, movingObject.preVelocity.Y, 0),
            stepZ = new Vector3(0, 0, movingObject.preVelocity.Z);

            Vector3 prelocation = movingObject.prePosition + movingObject.collisionBox.offset;

            nextFrameBox.origin = prelocation + stepX;
            if(Intersects (nextFrameBox, staticBox)){
                movement[0] = false;
            }
            nextFrameBox.origin = prelocation + stepY;
            if(Intersects (nextFrameBox, staticBox)){
                movement[1] = false;
            }
            nextFrameBox.origin = prelocation + stepZ;
            if(Intersects (nextFrameBox, staticBox)){
                movement[2] = false;
            }
            outObject = movingObject;
            move = movement;
        }