Exemple #1
0
    private void AddItem(float velocityX, float velocityY, float elapsed)
    {
        VelocityHistory item = new VelocityHistory
        {
            VelocityX = velocityX,
            VelocityY = velocityY,
            Seconds   = elapsed
        };

        history.Enqueue(item);
        if (history.Count > maxHistory)
        {
            history.Dequeue();
        }
        float totalSeconds = 0.0f;

        //VelocityX = VelocityY = 0.0f;
        this.Veloctiy = Vector2.zero;
        foreach (VelocityHistory h in history)
        {
            totalSeconds += h.Seconds;
        }
        foreach (VelocityHistory h in history)
        {
            float weight = h.Seconds / totalSeconds;
            //VelocityX += (h.VelocityX * weight);
            //VelocityY += (h.VelocityY * weight);
            this.Veloctiy.x += (h.VelocityX * weight);
            this.Veloctiy.y += (h.VelocityY * weight);
        }
        timer.Reset();
        timer.Start();
    }
Exemple #2
0
        private bool AddItem(float velocityX, float velocityY, float elapsed)
        {
            bool significantVelocityChange = false;

            VelocityHistory item = new VelocityHistory
            {
                VelocityX = velocityX,
                VelocityY = velocityY,
                Seconds   = elapsed,
                Angle     = (float)Math.Atan2(velocityY, velocityX)
            };

            if (history.Count != 0 && (velocityX != 0.0f || velocityY != 0.0f))
            {
                // detect change in velocity
                VelocityHistory prev = history[history.Count - 1];
                float           diff = Math.Abs(item.Angle - prev.Angle);
                if (diff > Math.PI)
                {
                    diff = (2.0f * (float)Math.PI) - diff;
                }
                if (diff > Math.PI * 0.1f)
                {
                    significantVelocityChange = true;
                    history.Clear();
                }
            }
            history.Add(item);

            if (history.Count > maxHistory)
            {
                history.RemoveAt(0);
            }

            float totalSeconds = 0.0f;

            VelocityX = VelocityY = 0.0f;
            foreach (VelocityHistory h in history)
            {
                totalSeconds += h.Seconds;
            }
            foreach (VelocityHistory h in history)
            {
                float weight = h.Seconds / totalSeconds;
                VelocityX += (h.VelocityX * weight);
                VelocityY += (h.VelocityY * weight);
            }

            timer.Reset();
            timer.Start();

            return(significantVelocityChange);
        }