/// <summary>
        /// GenerateLines
        /// </summary>
        /// <param name="vctStart"></param>
        /// <param name="vctEnd"></param>
        /// <param name="vctSideStep"></param>
        /// <param name="worldMatrix"></param>
        /// <param name="m_lineBuffer"></param>
        /// <param name="divideRatio"></param>
        private static void GenerateLines(Vector3 vctStart, Vector3 vctEnd, ref Vector3 vctSideStep, ref Matrix worldMatrix, ref List <MyLine> m_lineBuffer, int divideRatio)
        {
            for (int i = 0; i <= divideRatio; ++i)
            {
                Vector3 transformedStart = Vector3.Transform(vctStart, worldMatrix);
                Vector3 transformedEnd   = Vector3.Transform(vctEnd, worldMatrix);

                if (m_lineBuffer.Count < m_lineBuffer.Capacity)
                {
                    MyLine line = new MyLine(transformedStart, transformedEnd, false);
                    //@ generate Line
                    m_lineBuffer.Add(line);

                    vctStart += vctSideStep;
                    vctEnd   += vctSideStep;
                }
            }
        }
        public MyIntersectionResultLineTriangleEx(MyIntersectionResultLineTriangle triangle, MyEntity physObject, ref MyLine line)
        {
            Triangle = triangle;
            Entity   = physObject;
            InputLineInObjectSpace = line;

            NormalInObjectSpace            = MyUtils.GetNormalVectorFromTriangle(ref Triangle.InputTriangle);
            IntersectionPointInObjectSpace = line.From + line.Direction * Triangle.Distance;

            if (Entity is MyVoxelMap)
            {
                IntersectionPointInWorldSpace = IntersectionPointInObjectSpace;
                NormalInWorldSpace            = NormalInObjectSpace;

                //  This will move intersection point from world space into voxel map's object space
                IntersectionPointInObjectSpace = IntersectionPointInObjectSpace - ((MyVoxelMap)Entity).PositionLeftBottomCorner;
            }
            else
            {
                Matrix worldMatrix = Entity.WorldMatrix;
                NormalInWorldSpace            = MyUtils.GetTransformNormalNormalized(NormalInObjectSpace, ref worldMatrix);
                IntersectionPointInWorldSpace = MyUtils.GetTransform(IntersectionPointInObjectSpace, ref worldMatrix);
            }
        }
Example #3
0
        public void CreateFromVertices(Vector3[] vertices)
        {
            Vector3 min = new Vector3(float.MaxValue);
            Vector3 max = new Vector3(float.MinValue);

            foreach (Vector3 v in vertices)
            {
                min = Vector3.Min(v, min);
                max = Vector3.Min(v, max);
            }

            MyLine line02 = new MyLine(vertices[0], vertices[2], false);
            MyLine line23 = new MyLine(vertices[2], vertices[3], false);
            MyLine line31 = new MyLine(vertices[3], vertices[1], false);
            MyLine line10 = new MyLine(vertices[1], vertices[0], false);

            MyLine line76 = new MyLine(vertices[7], vertices[6], false);
            MyLine line64 = new MyLine(vertices[6], vertices[4], false);
            MyLine line45 = new MyLine(vertices[4], vertices[5], false);
            MyLine line57 = new MyLine(vertices[5], vertices[7], false);

            MyLine line40 = new MyLine(vertices[4], vertices[0], false);
            MyLine line01 = new MyLine(vertices[0], vertices[1], false);
            MyLine line15 = new MyLine(vertices[1], vertices[5], false);
            MyLine line54 = new MyLine(vertices[5], vertices[4], false);

            MyLine line32 = new MyLine(vertices[3], vertices[2], false);
            MyLine line26 = new MyLine(vertices[2], vertices[6], false);
            MyLine line67 = new MyLine(vertices[6], vertices[7], false);
            MyLine line73 = new MyLine(vertices[7], vertices[3], false);

            MyLine line13 = new MyLine(vertices[1], vertices[3], false);
            MyLine line37 = new MyLine(vertices[3], vertices[7], false);
            MyLine line75 = new MyLine(vertices[7], vertices[5], false);
            MyLine line51 = new MyLine(vertices[5], vertices[1], false);

            MyLine line04 = new MyLine(vertices[0], vertices[4], false);
            MyLine line46 = new MyLine(vertices[4], vertices[6], false);
            MyLine line62 = new MyLine(vertices[6], vertices[2], false);
            MyLine line20 = new MyLine(vertices[2], vertices[0], false);

            Sides[0].Lines[0] = line02;
            Sides[0].Lines[1] = line23;
            Sides[0].Lines[2] = line31;
            Sides[0].Lines[3] = line10;
            Sides[0].CreatePlaneFromLines();

            Sides[1].Lines[0] = line76;
            Sides[1].Lines[1] = line64;
            Sides[1].Lines[2] = line45;
            Sides[1].Lines[3] = line57;
            Sides[1].CreatePlaneFromLines();

            Sides[2].Lines[0] = line40;
            Sides[2].Lines[1] = line01;
            Sides[2].Lines[2] = line15;
            Sides[2].Lines[3] = line54;
            Sides[2].CreatePlaneFromLines();

            Sides[3].Lines[0] = line32;
            Sides[3].Lines[1] = line26;
            Sides[3].Lines[2] = line67;
            Sides[3].Lines[3] = line73;
            Sides[3].CreatePlaneFromLines();

            Sides[4].Lines[0] = line13;
            Sides[4].Lines[1] = line37;
            Sides[4].Lines[2] = line75;
            Sides[4].Lines[3] = line51;
            Sides[4].CreatePlaneFromLines();

            Sides[5].Lines[0] = line04;
            Sides[5].Lines[1] = line46;
            Sides[5].Lines[2] = line62;
            Sides[5].Lines[3] = line20;
            Sides[5].CreatePlaneFromLines();
        }
        public static void HandleIntersection(MyEntity ship, bool shakeActive, Vector3 headPosition, Vector3 headDirection)
        {
            // Direction vector
            Vector3 direction = Target - Position;

            direction.Normalize();

            // Compute line collision - Current Position <-> Target
            var   line   = new MyLine(Target, Position, true);
            var   result = MyEntities.GetIntersectionWithLine(ref line, MySession.PlayerShip, null, true, true);
            float distance;

            if (result.HasValue)
            {
                distance = result.Value.Triangle.Distance;
                float t = distance / line.Length;
                Position = Target + (Position - Target) * t;
            }
            else
            {
                distance = (Position - Target).Length();
            }


            // Move closer near plane of camera is in collision with enviroment (approximation with bounding sphere)
            Vector3 currentPosition = Position;
            Vector3 lastBadPosition = Position;
            int     stepCount       = 4;

            for (int stepSize = stepCount; stepSize >= 1; stepSize--)
            {
                float collisionStep   = COLLISION_STEP * (1 << (stepSize - 1));
                bool  distanceChanged = false;
                while (distance > MySession.PlayerShip.WorldVolume.Radius /*GetMinDistance()*/)
                {
                    // Headshake is based on distance
                    Vector3 shakePosition = currentPosition;
                    if (shakeActive)
                    {
                        shakePosition = currentPosition + Vector3.Transform(headPosition, TargetOrientation) * distance * HEADSHAKE_POWER;
                    }

                    // Test current camera position for near plane collision
                    var      cameraBS = new BoundingSphere(shakePosition, CAMERA_RADIUS);
                    MyEntity entity   = MyEntities.GetIntersectionWithSphere(ref cameraBS, ship, null);
                    if (entity == null)
                    {
                        break;
                    }

                    // Move closer (Measure distance from ship to avoid camera shake)
                    lastBadPosition = currentPosition;
                    currentPosition = Target - direction * (int)((distance - COLLISION_STEP / 10) / collisionStep) * collisionStep;
                    distance        = (currentPosition - Target).Length();
                    distanceChanged = true;
                }

                if (!distanceChanged && stepSize == stepCount)
                {
                    break;
                }

                if (distanceChanged && stepSize > 1)
                {
                    currentPosition = lastBadPosition;
                    distance        = (currentPosition - Target).Length();
                }
            }

            float minDistance = MySession.PlayerShip.WorldVolume.Radius;

            /*
             * if (distance < minDistance)
             * {
             *  Position = Target - minDistance * direction;
             * }
             * else*/
            {
                Position = currentPosition;
            }
        }