예제 #1
0
        public List <Triangle> GeneratePhysicalVertices(List <TrackNode> nodes)
        {
            List <VertexPositionColor> verts     = new List <VertexPositionColor>();
            List <Triangle>            roadVerts = new List <Triangle>();
            TrackNode nextNode;

            //Vector3 prevLeft = GetRoadOffsetPosition(nodes[nodes.Count - 1], -nodes[nodes.Count - 1].DistanceToLeftBarrier); // nodes[0].Position + Utility.RotatePoint(new Vector2(-nodes[0].DistanceToLeftBarrier, 0), nodes[0].Orientation);
            //Vector3 prevRight = GetRoadOffsetPosition(nodes[nodes.Count - 1], nodes[nodes.Count - 1].DistanceToRightBarrier); // nodes[0].Position + Utility.RotatePoint(new Vector2(nodes[0].DistanceToRightBarrier, 0), nodes[0].Orientation);

            for (int i = 0; i < nodes.Count; i++)
            {
                TrackNode node = nodes[i];

                if (i + 1 < nodes.Count)
                {
                    nextNode = nodes[i + 1];
                }
                else
                {
                    nextNode = nodes[0];                      //join up to start line
                }
                float zPos = node.Position.Z - nextNode.Position.Z;

                Vector3 prevLeft  = GetRoadOffsetPosition(node, -node.DistanceToLeftBarrier);
                Vector3 prevRight = GetRoadOffsetPosition(node, node.DistanceToRightBarrier);

                Vector3 currentLeft  = nextNode.Position + Utility.RotatePoint(new Vector2(-node.DistanceToLeftBarrier, 0), -node.Orientation);
                Vector3 currentRight = nextNode.Position + Utility.RotatePoint(new Vector2(node.DistanceToRightBarrier, 0), -node.Orientation);

                var t  = new Triangle(nextNode.GetLeftBoundary(), node.GetLeftBoundary(), node.GetRightBoundary());
                var t2 = new Triangle(nextNode.GetLeftBoundary(), node.GetRightBoundary(), nextNode.GetRightBoundary());
                roadVerts.Add(t);
                roadVerts.Add(t2);

                var normal  = Vector3.Normalize(Vector3.Cross(t.V2 - t.V1, t.V3 - t.V1));
                var normal2 = Vector3.Normalize(Vector3.Cross(t2.V2 - t2.V1, t2.V3 - t2.V1));
                if (Math.Round(normal.X, 2) != Math.Round(normal2.X, 2) ||
                    Math.Round(normal.Y, 2) != Math.Round(normal2.Y, 2) ||
                    Math.Round(normal.Z, 2) != Math.Round(normal2.Z, 2))
                {
                }

                //roadVerts.Add(prevLeft);
                //roadVerts.Add(currentLeft);
                //roadVerts.Add(prevRight);
                //roadVerts.Add(currentLeft);
                //roadVerts.Add(prevRight);
                //roadVerts.Add(currentRight);

                prevLeft  = currentLeft;
                prevRight = currentRight;
            }

            //_vertexBuffer = new VertexBuffer(Engine.Instance.Device, VertexPositionColor.SizeInBytes * verts.Count, BufferUsage.WriteOnly);
            //_vertexBuffer.SetData<VertexPositionColor>(verts.ToArray());
            return(roadVerts);
        }
예제 #2
0
        public float GetHeightAtPoint(TrackNode node, Vector3 point)
        {
            Vector3 a = node.GetLeftBoundary();
            Vector3 b = node.GetRightBoundary();
            Vector3 c = node.Next.GetRightBoundary();
            Vector3 d = node.Next.GetLeftBoundary();
            Vector3 hitLoc;
            float   t;

            Vector3 pos = point; pos.Y += 100;

            if (Utility.FindRayTriangleIntersection(ref pos, Vector3.Down, 1000f, ref a, ref b, ref c, out hitLoc, out t))
            {
                return(hitLoc.Y);
            }
            else if (Utility.FindRayTriangleIntersection(ref pos, Vector3.Down, 1000f, ref a, ref c, ref d, out hitLoc, out t))
            {
                return(hitLoc.Y);
            }
            else
            {
                return(-9999);
            }
        }
예제 #3
0
 private void PositionCameraAtNode(TrackNode node)
 {
     _cameraNode      = node;
     _camera.Position = Engine.Instance.Random.Next() % 2 == 0 ? _cameraNode.GetLeftBoundary() : _cameraNode.GetRightBoundary();
     _camera.Position = _camera.Position + new Vector3(0, Engine.Instance.Random.Next(15, 50), 0);
 }
예제 #4
0
        private void FollowTrackOrientation(TrackNode node, TrackNode nextNode)
        {
            var closestPoint1 = Utility.GetClosestPointOnLine(node.GetLeftBoundary(), node.GetRightBoundary(), Position);
            var closestPoint2 = Utility.GetClosestPointOnLine(nextNode.GetLeftBoundary(), nextNode.GetRightBoundary(), Position);

            var   dist    = Distance2d(closestPoint1, closestPoint2);
            var   carDist = Distance2d(closestPoint1, Position);
            float ratio   = Math.Min(carDist / dist, 1.0f);

            TrackPosition = CurrentNode.Number + ratio;

            // if the road is sloping downwards and we have enough speed, unstick from ground
            if (node.Slope - nextNode.Slope > 50 && Speed > 100 && _isOnGround)
            {
                _isOnGround = false;
                _upVelocity = -0.4f;
            }

            if (_isOnGround)
            {
                Up        = Vector3.Lerp(node.Up, nextNode.Up, ratio);
                Up        = Vector3.Normalize(Up);
                Direction = Vector3.Cross(Up, Right);
                Direction.Normalize();
            }

            _currentHeightOfTrack = MathHelper.Lerp(closestPoint1.Y, closestPoint2.Y, ratio);
            if (_currentHeightOfTrack == -9999)
            {
                throw new Exception();
            }
            if (_isOnGround)
            {
                var newPosition = Position;
                newPosition.Y = _currentHeightOfTrack;
                Position      = newPosition;
            }
            //GameConsole.WriteLine("height: " + _position.Y, 0);
            //GameConsole.WriteLine("ratio: " + ratio, 1);
        }