Exemple #1
0
        /// <summary>
        /// Creates a new virtual 3D instance in the world and returns its index.
        /// </summary>
        public int AddInstance(PositionVelocityTime throwVector)
        {
            Virtual3dObject virtual3DObject = new Virtual3dObject()
            {
                StartPosition = throwVector.Position, InitialVelocity = throwVector.Velocity.ToMetersPerSecond(), Index = numInstancesCreated
            };

            lock (Instances)
                Instances.Add(virtual3DObject);
            numInstancesCreated++;
            return(virtual3DObject.Index);
        }
        static PositionVelocityTime GetThrowVector(CircularQueue <PositionVelocityTime> queue)
        {
            //` ![](D0721EC9212F4FB8CFAB94D72FC8257E.png;;;0.02560,0.02560)

            DateTime     earliestValidTime   = DateTime.Now - TimeSpan.FromSeconds(0.4);
            const double minVelocityForThrow = 725;
            const double maxPercentThrowingSpeedForRelease = 0.6;
            bool         escapeVelocityReached             = false;
            double       fastestThrowingSpeed    = 0;
            bool         weAreThrowing           = false;
            Vector       fastestVelocity         = new Vector(0, 0, 0);
            Vector       throwingPoint           = new Vector(0, 0, 0);
            DateTime     throwingTime            = DateTime.MinValue;
            DateTime     escapeVelocityReachTime = DateTime.MinValue;

            lock (queue)
                for (int i = 0; i < queue.Count; i++)
                {
                    PositionVelocityTime checkPt = queue[i];
                    if (checkPt.Time < earliestValidTime)                      // Data is too stale
                    {
                        continue;
                    }

                    float speed = checkPt.Velocity.Magnitude;
                    if (speed > minVelocityForThrow)
                    {
                        if (IsValidThrowDirection(checkPt.Velocity))
                        {
                            if (escapeVelocityReachTime == DateTime.MinValue)
                            {
                                escapeVelocityReachTime = checkPt.Time;
                            }
                            escapeVelocityReached = true;
                            if (speed > fastestThrowingSpeed)
                            {
                                fastestThrowingSpeed = speed;
                                fastestVelocity      = checkPt.Velocity;
                                throwingPoint        = checkPt.Position;
                                throwingTime         = checkPt.Time;
                                continue;
                            }
                        }
                    }
                    else if (escapeVelocityReachTime != DateTime.MinValue)
                    {
                        // velocity is slower than minVelocityForThrow and we've set fastestThrowingSpeed
                        if (checkPt.Time - escapeVelocityReachTime < TimeSpan.FromSeconds(0.08))
                        {
                            fastestVelocity         = new Vector(0, 0, 0);
                            throwingPoint           = new Vector(0, 0, 0);
                            throwingTime            = DateTime.MinValue;
                            escapeVelocityReachTime = DateTime.MinValue;
                            escapeVelocityReached   = false;
                        }
                    }

                    if (!escapeVelocityReached)
                    {
                        continue;
                    }

                    // HACK: We're not really calculating this correctly.
                    if (speed < fastestThrowingSpeed * maxPercentThrowingSpeedForRelease || checkPt.Velocity.Dot(fastestVelocity) < 0)
                    {
                        if (checkPt.Time - escapeVelocityReachTime > TimeSpan.FromMilliseconds(240))
                        {
                            weAreThrowing = true;
                        }
                    }
                }

            if (!weAreThrowing)
            {
                return(new PositionVelocityTime());
            }

            return(new PositionVelocityTime()
            {
                Position = throwingPoint, Velocity = fastestVelocity, Time = throwingTime
            });
        }