示例#1
0
        private static bool addSupport(Vector3 direction, RayPyramid shape1, EnvironmentObject shape2)
        {
            Vector3 newVertex = calculateSupport(direction, shape1, shape2);

            verticies.Add(newVertex);
            return(Vector3.Dot(direction, newVertex) >= 0);
        }
示例#2
0
        private static Vector3 calculateSupport(Vector3 direction, RayPyramid shape1, EnvironmentObject shape2)
        {
            Vector3 newVertex = shape1.GetSupportingPoint(direction);

            newVertex -= shape2.GetSupportPoint(-1 * direction);
            return(newVertex);
        }
示例#3
0
        public static bool CheckCollision(RayPyramid shape1, EnvironmentObject shape2)
        {
            verticies = new List <Vector3>();
            direction = shape2.GetCenter() - shape1.GetCenter();

            int result = 0;
            int count  = 0;

            while (result == 0 && count < 100)
            {
                result = evolveSimplex(shape1, shape2);
                count++;
            }

            return(result > 0);
        }
示例#4
0
        private static int evolveSimplex(RayPyramid shape1, EnvironmentObject shape2)
        {
            switch (verticies.Count)
            {
            case 0:
                direction = shape2.GetCenter() - shape1.GetCenter();
                break;

            case 1:
                direction *= -1;
                break;

            case 2:
                //ab is line segment of the two points already gotten
                Vector3 ab = verticies[1] - verticies[0];
                //a0 is from the first point to the origin
                Vector3 a0 = verticies[0] * -1;

                Vector3 temp = Vector3.Cross(ab, a0);
                //Vector3 temp = Vector3.Cross(a0, ab);

                direction = Vector3.Cross(temp, ab);
                //direction = Vector3.Cross(ab, temp);


                break;

            case 3:
                Vector3 ac = verticies[2] - verticies[0];
                ab = verticies[1] - verticies[0];

                direction = Vector3.Cross(ac, ab);


                a0 = verticies[0] * -1;
                if (Vector3.Dot(direction, a0) < 0)
                {
                    direction *= -1;
                }
                break;

            case 4:
                Vector3 da = verticies[3] - verticies[0];
                Vector3 db = verticies[3] - verticies[1];
                Vector3 dc = verticies[3] - verticies[2];

                Vector3 d0 = verticies[3] * -1;

                Vector3 abdNorm = Vector3.Cross(da, db);
                Vector3 bcdNorm = Vector3.Cross(db, dc);
                Vector3 cadNorm = Vector3.Cross(dc, da);

                if (Vector3.Dot(abdNorm, d0) > 0)
                {
                    verticies.Remove(verticies[2]);
                    direction = abdNorm;
                }
                else if (Vector3.Dot(bcdNorm, d0) > 0)
                {
                    verticies.Remove(verticies[0]);
                    direction = bcdNorm;
                }
                else if (Vector3.Dot(cadNorm, d0) > 0)
                {
                    verticies.Remove(verticies[1]);
                    direction = cadNorm;
                }
                else
                {
                    return(1);
                }

                break;
            }

            return(addSupport(direction, shape1, shape2) ? 0 : -1);
        }
示例#5
0
        public RaytracingCamera(string Name, Vector3 Position, Vector3 Direction, float viewWidth, float viewHeight, float sensitivity, int horizontalResolution, int verticalResolution, int pyramidResolution, int viewDistance)
        {
            this.Name         = Name;
            this.Position     = Position;
            this.Direction    = Direction;
            HorizontalFOV     = viewWidth;
            VerticalFOV       = viewHeight;
            this.sensitivity  = sensitivity;
            FrameWidth        = horizontalResolution;
            FrameHeight       = verticalResolution;
            PyramidResolution = pyramidResolution;
            ViewDistance      = viewDistance;
            rayPyramids       = new RayPyramid[pyramidResolution, pyramidResolution];


            //Start at the top left of the field of view
            float startingHorizontalAngle = -viewWidth / 2;
            float startingVerticalAngle   = -viewHeight / 2;

            float pyramidHorizontalAngleIncrement = viewWidth / (pyramidResolution + 1);
            float pyramidVerticalAngleIncrement   = viewHeight / (pyramidResolution + 1);

            //first we need to initialize all the ray pyramids
            for (int i = 0; i < pyramidResolution; ++i)
            {
                float verticalAngle     = startingVerticalAngle + (pyramidVerticalAngleIncrement * i);
                float nextVerticalAngle = startingVerticalAngle + (pyramidVerticalAngleIncrement * (i + 1));

                for (int j = 0; j < pyramidResolution; ++j)
                {
                    float horizontalAngle     = startingHorizontalAngle + (pyramidHorizontalAngleIncrement * j);
                    float nextHorizontalAngle = startingHorizontalAngle + (pyramidHorizontalAngleIncrement * (j + 1));

                    Vector3 point1 = Vector3.Multiply(ViewDistance, Direction);
                    //point1 = VectorMath.RotateVector3Z(point1, verticalAngle);
                    //point1 = VectorMath.RotateVector3Y(point1, horizontalAngle);

                    Vector3 point2 = Vector3.Multiply(ViewDistance, Direction);
                    //point2 = VectorMath.RotateVector3Z(point2, verticalAngle);
                    //point2 = VectorMath.RotateVector3Y(point2, nextHorizontalAngle);

                    Vector3 point3 = Vector3.Multiply(ViewDistance, Direction);
                    //point3 = VectorMath.RotateVector3Z(point3, nextVerticalAngle);
                    //point3 = VectorMath.RotateVector3Y(point3, horizontalAngle);

                    Vector3 point4 = Vector3.Multiply(ViewDistance, Direction);
                    //point4 = VectorMath.RotateVector3Z(point4, nextVerticalAngle);
                    //point4 = VectorMath.RotateVector3Y(point4, nextHorizontalAngle);

                    rayPyramids[j, i] = new RayPyramid(Position, point1, point2, point3, point4);
                }
            }

            float horizontalAngleIncrement = viewWidth / FrameWidth;
            float verticalAngleIncrement   = viewHeight / FrameHeight;

            for (int i = 0; i < FrameHeight; ++i)
            {
                float verticalAngle = startingVerticalAngle + (verticalAngleIncrement * i);

                for (int j = 0; j < FrameWidth; ++j)
                {
                    float horizontalAngle = startingHorizontalAngle + (horizontalAngleIncrement * j);

                    Ray newRay = new Ray(Position, Direction, Color.Black);
                    newRay.RotateZ(verticalAngle);
                    newRay.RotateY(horizontalAngle);

                    int rayPyramidHorizontalIndex = (int)(j / (FrameWidth / pyramidResolution));
                    int rayPyramidVerticalIndex   = (int)(i / (FrameHeight / pyramidResolution));

                    rayPyramids[rayPyramidHorizontalIndex, rayPyramidVerticalIndex].RayList.Add(newRay);
                }
            }

            AngleFromZ = (float)Math.PI / 2;


            //at this point direction is (1, 0, 0);
            //must rotate the vectors to assume the new direction
            //float dirXAngle = (Direction.X) / (float)Math.Sqrt(Math.Pow(Direction.X, 2) + Math.Pow(Direction.Y, 2) + Math.Pow(Direction.Z, 2));
        }