Esempio n. 1
0
        public static void DrawQuadBezier(CCPoint origin, CCPoint control, CCPoint destination, int segments, CCColor4B color)
        {
            var   vertices = new VertexPositionColor[segments + 1];
            float factor   = CCDirector.SharedDirector.ContentScaleFactor;

            float t = 0.0f;

            for (int i = 0; i < segments; i++)
            {
                float x = CCSplineMath.QuadBezier(origin.X, control.X, destination.X, t);
                float y = CCSplineMath.QuadBezier(origin.Y, control.Y, destination.Y, t);

                vertices[i]          = new VertexPositionColor();
                vertices[i].Position = new Vector3(x * factor, y * factor, 0);
                vertices[i].Color    = new Color(color.R, color.G, color.B, color.A);
                t += 1.0f / segments;
            }
            vertices[segments] = new VertexPositionColor
            {
                Position = new Vector3(destination.X * factor, destination.Y * factor, 0),
                Color    = new Color(color.R, color.G, color.B, color.A),
            };

            BasicEffect basicEffect = CCDrawManager.PrimitiveEffect;

            basicEffect.Projection = CCDrawManager.ProjectionMatrix;
            basicEffect.View       = CCDrawManager.ViewMatrix;
            basicEffect.World      = CCDrawManager.WorldMatrix;

            foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
            {
                pass.Apply();
                basicEffect.GraphicsDevice.DrawUserPrimitives(PrimitiveType.LineStrip, vertices, 0, segments);
            }
        }
Esempio n. 2
0
        public override void Update(float time)
        {
            if (m_pTarget != null)
            {
                float xa = 0;
                float xb = m_sConfig.ControlPoint1.X;
                float xc = m_sConfig.ControlPoint2.X;
                float xd = m_sConfig.EndPosition.X;

                float ya = 0;
                float yb = m_sConfig.ControlPoint1.Y;
                float yc = m_sConfig.ControlPoint2.Y;
                float yd = m_sConfig.EndPosition.Y;

                float x = CCSplineMath.CubicBezier(xa, xb, xc, xd, time);
                float y = CCSplineMath.CubicBezier(ya, yb, yc, yd, time);

                CCPoint currentPos = m_pTarget.Position;
                CCPoint diff       = currentPos - m_previousPosition;
                m_startPosition = m_startPosition + diff;

                CCPoint newPos = m_startPosition + new CCPoint(x, y);
                m_pTarget.Position = newPos;

                m_previousPosition = newPos;
            }
        }
        /// <summary>
        /// draws a cubic bezier path
        /// @since v0.8
        /// </summary>
        public static void DrawCubicBezier(CCPoint origin, CCPoint control1, CCPoint control2, CCPoint destination, int segments, CCColor4B color)
        {
            var vertices = new VertexPositionColor[segments + 1];

            float t = 0;

            for (int i = 0; i < segments; ++i)
            {
                float x = CCSplineMath.CubicBezier(origin.X, control1.X, control2.X, destination.X, t);
                float y = CCSplineMath.CubicBezier(origin.Y, control1.Y, control2.Y, destination.Y, t);

                vertices[i]          = new VertexPositionColor();
                vertices[i].Position = new Vector3(x, y, 0);
                vertices[i].Color    = new Color(color.R, color.G, color.B, color.A);
                t += 1.0f / segments;
            }
            vertices[segments] = new VertexPositionColor
            {
                Color    = new Color(color.R, color.G, color.B, color.A),
                Position = new Vector3(destination.X, destination.Y, 0)
            };

            BasicEffect basicEffect = CCDrawManager.PrimitiveEffect;

            basicEffect.Projection = CCDrawManager.ProjectionMatrix;
            basicEffect.View       = CCDrawManager.ViewMatrix;
            basicEffect.World      = CCDrawManager.WorldMatrix;

            foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
            {
                pass.Apply();

                basicEffect.GraphicsDevice.DrawUserPrimitives(PrimitiveType.LineStrip, vertices, 0, segments);
            }
        }
Esempio n. 4
0
        public override void Update(float time)
        {
            int   p;
            float lt;

            // border
            if (time == 1)
            {
                p  = m_pPoints.Count - 1;
                lt = 1;
            }
            else
            {
                p  = (int)(time / m_fDeltaT);
                lt = (time - m_fDeltaT * p) / m_fDeltaT;
            }

            // Interpolate
            var     c   = m_pPoints.Count - 1;
            CCPoint pp0 = m_pPoints[Math.Min(c, Math.Max(p - 1, 0))];
            CCPoint pp1 = m_pPoints[Math.Min(c, Math.Max(p + 0, 0))];
            CCPoint pp2 = m_pPoints[Math.Min(c, Math.Max(p + 1, 0))];
            CCPoint pp3 = m_pPoints[Math.Min(c, Math.Max(p + 2, 0))];

            CCPoint newPos = CCSplineMath.CCCardinalSplineAt(pp0, pp1, pp2, pp3, m_fTension, lt);

            UpdatePosition(newPos);
        }
        public static void DrawCardinalSpline(List <CCPoint> config, float tension, int segments)
        {
            var vertices = new VertexPositionColor[segments + 1];

            int   p;
            float lt;
            float deltaT = 1.0f / config.Count;

            for (int i = 0; i < segments + 1; i++)
            {
                float dt = (float)i / segments;

                // border
                if (dt == 1)
                {
                    p  = config.Count - 1;
                    lt = 1;
                }
                else
                {
                    p  = (int)(dt / deltaT);
                    lt = (dt - deltaT * p) / deltaT;
                }

                // Interpolate
                // Interpolate
                int     c   = config.Count - 1;
                CCPoint pp0 = config[Math.Min(c, Math.Max(p - 1, 0))];
                CCPoint pp1 = config[Math.Min(c, Math.Max(p + 0, 0))];
                CCPoint pp2 = config[Math.Min(c, Math.Max(p + 1, 0))];
                CCPoint pp3 = config[Math.Min(c, Math.Max(p + 2, 0))];

                CCPoint newPos = CCSplineMath.CCCardinalSplineAt(pp0, pp1, pp2, pp3, tension, lt);

                vertices[i].Position.X = newPos.X;
                vertices[i].Position.Y = newPos.Y;
                vertices[i].Color      = Color.White;
            }

            BasicEffect basicEffect = CCDrawManager.PrimitiveEffect;

            basicEffect.Projection = CCDrawManager.ProjectionMatrix;
            basicEffect.View       = CCDrawManager.ViewMatrix;
            basicEffect.World      = CCDrawManager.WorldMatrix;
            foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
            {
                pass.Apply();
                basicEffect.GraphicsDevice.DrawUserPrimitives(PrimitiveType.LineStrip, vertices, 0, segments);
            }
        }
Esempio n. 6
0
        public override void Update(float time)
        {
            int   p;
            float lt;

            // eg.
            // p..p..p..p..p..p..p
            // 1..2..3..4..5..6..7
            // want p to be 1, 2, 3, 4, 5, 6
            if (time == 1)
            {
                p  = m_pPoints.Count - 1;
                lt = 1;
            }
            else
            {
                p  = (int)(time / m_fDeltaT);
                lt = (time - m_fDeltaT * p) / m_fDeltaT;
            }

            // Interpolate
            var     c   = m_pPoints.Count - 1;
            CCPoint pp0 = m_pPoints[Math.Min(c, Math.Max(p - 1, 0))];
            CCPoint pp1 = m_pPoints[Math.Min(c, Math.Max(p + 0, 0))];
            CCPoint pp2 = m_pPoints[Math.Min(c, Math.Max(p + 1, 0))];
            CCPoint pp3 = m_pPoints[Math.Min(c, Math.Max(p + 2, 0))];

            CCPoint newPos = CCSplineMath.CCCardinalSplineAt(pp0, pp1, pp2, pp3, m_fTension, lt);

            // Support for stacked actions
            CCNode  node = m_pTarget;
            CCPoint diff = node.Position - m_previousPosition;

            if (diff.X != 0 || diff.Y != 0)
            {
                m_accumulatedDiff = m_accumulatedDiff + diff;
                newPos            = newPos + m_accumulatedDiff;
            }

            UpdatePosition(newPos);
        }