public CCRotateAroundToState(CCRotateAroundTo action, CCNode target)
            : base(action, target)
        {
            DistanceAngle = action.DistanceAngle;

            Origin            = action.Origin;
            StartPosition     = target.Position;
            RotationDirection = action.RotationDirection;

            offsetX = StartPosition.X - Origin.X;
            offsetY = StartPosition.Y - Origin.Y;

            // Calculate the Starting Angle of the target in relation to the Origin in which it is to rotate

            // Math.Atan2 returns the mathematical angle which is counter-clockwise [-Math.PI, +Math.PI]
            StartAngle = CCMathHelper.ToDegrees((float)Math.Atan2(offsetY, offsetX));
            // Map value [0,360]
            StartAngle = (StartAngle + 360) % 360;


            // Now we work out how far we actually have to rotate
            DiffAngle = DistanceAngle - StartAngle;
            // Map value [0,360] and take into consideration the direction of rotation - CCW or CW
            DiffAngle = (DiffAngle + 360 * RotationDirection) % 360;

            theta = CCMathHelper.ToRadians(DiffAngle);
        }
Beispiel #2
0
        public void UpdateBodyTransform()
        {
            if (PhysicsBody != null)
            {
                b2Vec2 pos = PhysicsBody.Position;

                float x = pos.x * ptmRatio;
                float y = pos.y * ptmRatio;

                if (IgnoreAnchorPointForPosition)
                {
                    x += AnchorPointInPoints.X * ptmRatio;
                    y += AnchorPointInPoints.Y * ptmRatio;
                }

                // Make matrix
                float radians = PhysicsBody.Angle;
                if (radians != 0)
                {
                    Rotation = CCMathHelper.ToDegrees(-radians);
                }

                PositionX = x;
                PositionY = y;
            }
        }
Beispiel #3
0
        public void SetZoomFactor(float zoomFactor)
        {
            zoomFactor = CCMathHelper.Clamp(zoomFactor, MinZoomFactor, MaxZoomFactor);
            ZoomFactor = zoomFactor;
            var currentWidth  = StartVisibleArea.Width / zoomFactor;
            var currentHeight = StartVisibleArea.Height / zoomFactor;

            OrthographicViewSizeWorldspace = new CCSize(currentWidth, currentHeight);
            ZoomUpdated?.Invoke(this, ZoomFactor);
        }
Beispiel #4
0
        public void reset()
        {
            _speed = 50;
            _pivot = new CCPoint(-1, 1);
            _rotationOrientation = ROTATE_NONE;
            Rotation             = -90;
            _targetRotation      = -90;
            float angle = CCMathHelper.ToRadians(RotationX);

            _vector = new CCPoint(_speed * (float)Math.Cos(angle),
                                  -_speed * (float)Math.Sin(angle));
        }
Beispiel #5
0
        public virtual void UpdateVelocity(CCPoint point)
        {
            // Calculate distance and angle from the center.
            float dx = point.X - PositionX;
            float dy = point.Y - PositionY;

            dycache = dy;
            float dSq = dx * dx + dy * dy;

            if (dSq <= DeadRadiusSq)
            {
                Velocity      = CCPoint.Zero;
                Degrees       = 0.0f;
                StickPosition = Center;
                return;
            }

            float angle = (float)Math.Atan2(dy, dx); // in radians

            if (angle < 0)
            {
                angle += CCMathHelper.TwoPi;
            }

            float cosAngle;
            float sinAngle;

            if (isDPad)
            {
                float anglePerSector = 360.0f / CCMathHelper.ToRadians(NumberOfDirections); //  NumberOfDirections * ((float)Math.PI / 180.0f);
                angle = (float)Math.Round(angle / anglePerSector) * anglePerSector;
            }

            cosAngle = CCMathHelper.Cos(angle);
            sinAngle = CCMathHelper.Sin(angle);

            // NOTE: Velocity goes from -1.0 to 1.0.
            if (dSq > JoystickRadiusSq || isDPad)
            {
                dx = cosAngle * joystickRadius;
                dy = sinAngle * joystickRadius;
            }

            Velocity = new CCPoint(dx / joystickRadius, dy / joystickRadius);
            Degrees  = CCMathHelper.ToDegrees(angle);

            // Update the thumb's position
            var newLoc = new CCPoint(dx + ContentSize.Width / 2, dy + ContentSize.Height / 2);

            StickPosition = newLoc;
        }
Beispiel #6
0
        public void HandleInput(CCPoint touchPoint)
        {
            desiredLocation = touchPoint;

            float differenceY = touchPoint.Y - this.PositionY;
            float differenceX = touchPoint.X - this.PositionX;

            float angleInDegrees = -1 * CCMathHelper.ToDegrees(
                (float)System.Math.Atan2(differenceY, differenceX));

            var rotateAction = new CCRotateTo(0.3f, angleInDegrees);

            this.AddAction(rotateAction);

            var      moveAction   = new CCMoveTo(1f, touchPoint);
            CCAction easingAction = new CCEaseSineInOut(moveAction);

            this.AddAction(easingAction);
        }
Beispiel #7
0
        void UpdateAbsolutePoints()
        {
            var absolutePosition = this.PositionWorldspace;

            var rotationInClockwiseRadians = CCMathHelper.ToRadians(this.Rotation);

            // In this context, CocosSharp uses clockwise rotation, the opposite of mathematical rotation
            // So let's invert it so we get rotation in counterclockwise units:
            var rotationInRadians = -rotationInClockwiseRadians;

            CCPoint rotatedXAxis = new CCPoint((float)System.Math.Cos(rotationInRadians), (float)System.Math.Sin(rotationInRadians));
            CCPoint rotatedYAxis = new CCPoint(-rotatedXAxis.Y, rotatedXAxis.X);

            for (int i = 0; i < points.Length; i++)
            {
                absolutePoints [i] =
                    absolutePosition +
                    (rotatedXAxis * points [i].X) +
                    (rotatedYAxis * points [i].Y);
            }
        }
Beispiel #8
0
        public void UpdateBodyTransform()
        {
            b2Vec2 pos = PhysBody.Position;

            float x = pos.x * App.PTM_RATIO;
            float y = pos.y * App.PTM_RATIO;

            if (IgnoreAnchorPointForPosition)
            {
                x += AnchorPointInPoints.X * App.PTM_RATIO;
                y += AnchorPointInPoints.Y * App.PTM_RATIO;
            }

            // Make matrix
            float radians = PhysBody.Angle;

            if (Math.Abs(radians) > EPSILON)
            {
                Rotation = CCMathHelper.ToDegrees(-radians);
            }

            PositionX = x;
            PositionY = y;
        }
Beispiel #9
0
        void PositionMonkey()
        {
            // We start our rotation with an offset of 230,0 from the origin 0 degrees
            // and randomly position the monkey
            var startingAngle = CCRandom.GetRandomInt(-360, 360);

            monkey.Position = CCPoint.RotateByAngle(origin + new CCPoint(230, 0), origin, CCMathHelper.ToRadians(startingAngle));
            positioned      = true;
        }
        protected override void Draw()
        {
            base.Draw();

            CCColor4B color = new CCColor4B(1.0f, 1.0f, 1.0f, 1.0f);

            CCDrawingPrimitives.Begin();

            switch (_lineType)
            {
            case lineTypes.LINE_NONE:
                break;

            case lineTypes.LINE_TEMP:

                //CCDrawingPrimitives.Begin();
                CCDrawingPrimitives.DrawLine(_tip, _pivot, color);
                CCDrawingPrimitives.DrawCircle(_pivot, 10, CCMathHelper.ToRadians(360), 10, false, color);
                //CCDrawingPrimitives.End();

                break;

            case lineTypes.LINE_DASHED:

                //CCDrawingPrimitives.Begin();
                CCDrawingPrimitives.DrawCircle(_pivot, 10, (float)Math.PI, 10, false, color);
                //CCDrawingPrimitives.End();

                int segments = (int)(_lineLength / (_dash + _dashSpace));

                float t = 0.0f;
                float x_;
                float y_;

                for (int i = 0; i < segments + 1; i++)
                {
                    x_ = _pivot.X + t * (_tip.X - _pivot.X);
                    y_ = _pivot.Y + t * (_tip.Y - _pivot.Y);

                    //CCDrawingPrimitives.Begin();
                    CCDrawingPrimitives.DrawCircle(new CCPoint(x_, y_), 4, (float)Math.PI, 6, false, color);
                    //CCDrawingPrimitives.End();

                    t += (float)1 / segments;
                }
                break;
            }

            //draw energy bar
            color = new CCColor4B(0.0f, 0.0f, 0.0f, 1.0f);

            CCDrawingPrimitives.DrawLine(
                new CCPoint(_energyLineX, _screenSize.Height * 0.1f),
                new CCPoint(_energyLineX, _screenSize.Height * 0.9f),
                color);

            color = new CCColor4B(1.0f, 0.5f, 0.0f, 1.0f);

            CCDrawingPrimitives.DrawLine(
                new CCPoint(_energyLineX, _screenSize.Height * 0.1f),
                new CCPoint(_energyLineX, _screenSize.Height * 0.1f + _energy * _energyHeight),
                color);

            CCDrawingPrimitives.End();
        }
Beispiel #11
0
        void CreateGeometry()
        {
            var windowSize = Layer.VisibleBoundsWorldspace.Size;

            // Draw 10 circles
            for (int i = 0; i < 10; i++)
            {
                drawBuffer.DrawSolidCircle(windowSize.Center, 10 * (10 - i),
                                           new CCColor4F(CCRandom.Float_0_1(), CCRandom.Float_0_1(), CCRandom.Float_0_1(), 1));
            }

            // Draw polygons
            CCPoint[] points = new CCPoint[]
            {
                new CCPoint(windowSize.Height / 4, 0),
                new CCPoint(windowSize.Width, windowSize.Height / 5),
                new CCPoint(windowSize.Width / 3 * 2, windowSize.Height)
            };
            drawBuffer.DrawPolygon(points, points.Length, new CCColor4F(1.0f, 0, 0, 0.5f), 4, new CCColor4F(0, 0, 1, 1));

            // star poly (triggers buggs)
            {
                const float o    = 80;
                const float w    = 20;
                const float h    = 50;
                CCPoint[]   star = new CCPoint[]
                {
                    new CCPoint(o + w, o - h), new CCPoint(o + w * 2, o),                               // lower spike
                    new CCPoint(o + w * 2 + h, o + w), new CCPoint(o + w * 2, o + w * 2),               // right spike
                };

                drawBuffer.DrawPolygon(star, star.Length, new CCColor4F(1, 0, 0, 0.5f), 1, new CCColor4F(0, 0, 1, 1));
            }

            // star poly (doesn't trigger bug... order is important un tesselation is supported.
            {
                const float o    = 180;
                const float w    = 20;
                const float h    = 50;
                var         star = new CCPoint[]
                {
                    new CCPoint(o, o), new CCPoint(o + w, o - h), new CCPoint(o + w * 2, o), // lower spike
                    new CCPoint(o + w * 2 + h, o + w), new CCPoint(o + w * 2, o + w * 2),    // right spike
                    new CCPoint(o + w, o + w * 2 + h), new CCPoint(o, o + w * 2),            // top spike
                    new CCPoint(o - h, o + w),                                               // left spike
                };

                drawBuffer.DrawPolygon(star, star.Length, new CCColor4F(1, 0, 0, 0.5f), 1, new CCColor4F(0, 0, 1, 1));
            }


            // Draw segment
            drawBuffer.DrawLine(new CCPoint(20, windowSize.Height), new CCPoint(20, windowSize.Height / 2), 10, new CCColor4F(0, 1, 0, 1), DrawNodeBuffer.LineCap.Round);

            drawBuffer.DrawLine(new CCPoint(10, windowSize.Height / 2), new CCPoint(windowSize.Width / 2, windowSize.Height / 2), 40,
                                new CCColor4F(1, 0, 1, 0.5f), DrawNodeBuffer.LineCap.Round);

            CCSize size = VisibleBoundsWorldspace.Size;

            var visibleRect = VisibleBoundsWorldspace;

            // draw quad bezier path
            drawBuffer.DrawQuadBezier(new CCPoint(0, size.Height),
                                      visibleRect.Center,
                                      (CCPoint)visibleRect.Size,
                                      50, 3,
                                      new CCColor4B(255, 0, 255, 255));

            // draw cubic bezier path
            drawBuffer.DrawCubicBezier(visibleRect.Center,
                                       new CCPoint(size.Width / 2 + 30, size.Height / 2 + 50),
                                       new CCPoint(size.Width / 2 + 60, size.Height / 2 - 50),
                                       new CCPoint(size.Width, size.Height / 2), 100, 2, CCColor4B.Green);

            // draw an ellipse within rectangular region
            drawBuffer.DrawEllipse(new CCRect(100, 300, 100, 200), 8, CCColor4B.AliceBlue);

            var splinePoints = new List <CCPoint>();

            splinePoints.Add(new CCPoint(0, 0));
            splinePoints.Add(new CCPoint(50, 70));
            splinePoints.Add(new CCPoint(0, 140));
            splinePoints.Add(new CCPoint(100, 210));
            splinePoints.Add(new CCPoint(0, 280));
            splinePoints.Add(new CCPoint(150, 350));

            int   numberOfSegments = 64;
            float tension          = .05f;

            drawBuffer.DrawCardinalSpline(splinePoints, tension, numberOfSegments);

            drawBuffer.DrawSolidArc(
                pos: new CCPoint(350, windowSize.Height * 0.75f),
                radius: 100,
                startAngle: CCMathHelper.ToRadians(45),
                sweepAngle: CCMathHelper.Pi / 2, // this is in radians, clockwise
                color: CCColor4B.Aquamarine);
        }
Beispiel #12
0
        void HandleMoveCircle(CCTouch touch)
        {
            const float        timeToTake = 1.5f;      // in seconds
            CCFiniteTimeAction coreAction = null;

            // By default all actions will be added directly to the
            // root node - it has values for Position, Scale, and Rotation.
            CCNode nodeToAddTo = drawNodeRoot;

            switch (VariableOptions [currentVariableIndex])
            {
            case "Position":
                coreAction = new CCMoveTo(timeToTake, touch.Location);
                break;

            case "Scale":
                var distance     = CCPoint.Distance(touch.Location, drawNodeRoot.Position);
                var desiredScale = distance / DefaultCircleRadius;
                coreAction = new CCScaleTo(timeToTake, desiredScale);
                break;

            case "Rotation":
                float differenceY = touch.Location.Y - drawNodeRoot.PositionY;
                float differenceX = touch.Location.X - drawNodeRoot.PositionX;

                float angleInDegrees = -1 * CCMathHelper.ToDegrees(
                    (float)Math.Atan2(differenceY, differenceX));

                coreAction = new CCRotateTo(timeToTake, angleInDegrees);

                break;

            case "LineWidth":
                coreAction = new LineWidthAction(timeToTake, touch.Location.X / 40f);
                // The LineWidthAction is a special action designed to work only on
                // LineNode instances, so we have to set the nodeToAddTo to the lineNode:
                nodeToAddTo = lineNode;
                break;
            }

            CCAction easing = null;

            switch (EasingOptions [currentEasingIndex])
            {
            case "CCEaseBack":
                if (currentInOutIndex == 0)
                {
                    easing = new CCEaseBackOut(coreAction);
                }
                else if (currentInOutIndex == 1)
                {
                    easing = new CCEaseBackIn(coreAction);
                }
                else
                {
                    easing = new CCEaseBackInOut(coreAction);
                }

                break;

            case "CCEaseBounce":
                if (currentInOutIndex == 0)
                {
                    easing = new CCEaseBounceOut(coreAction);
                }
                else if (currentInOutIndex == 1)
                {
                    easing = new CCEaseBounceIn(coreAction);
                }
                else
                {
                    easing = new CCEaseBounceInOut(coreAction);
                }

                break;

            case "CCEaseElastic":
                if (currentInOutIndex == 0)
                {
                    easing = new CCEaseElasticOut(coreAction);
                }
                else if (currentInOutIndex == 1)
                {
                    easing = new CCEaseElasticIn(coreAction);
                }
                else
                {
                    easing = new CCEaseElasticInOut(coreAction);
                }

                break;

            case "CCEaseExponential":
                if (currentInOutIndex == 0)
                {
                    easing = new CCEaseExponentialOut(coreAction);
                }
                else if (currentInOutIndex == 1)
                {
                    easing = new CCEaseExponentialIn(coreAction);
                }
                else
                {
                    easing = new CCEaseExponentialInOut(coreAction);
                }

                break;

            case "CCEaseSine":
                if (currentInOutIndex == 0)
                {
                    easing = new CCEaseSineOut(coreAction);
                }
                else if (currentInOutIndex == 1)
                {
                    easing = new CCEaseSineIn(coreAction);
                }
                else
                {
                    easing = new CCEaseSineInOut(coreAction);
                }

                break;
            }

            nodeToAddTo.AddAction(easing ?? coreAction);
        }
Beispiel #13
0
 public void setRotationFromVector()
 {
     _targetRotation = CCMathHelper.ToDegrees((float)Math.Atan2(-_vector.Y, _vector.X));
     //this->setTargetRotation(CC_RADIANS_TO_DEGREES( atan2(-_vector.y, _vector.x) ) );
 }
Beispiel #14
0
        private void HandleMoveCircle(CCTouch touch)
        {
            const float        timeToTake = 1.5f;      // in seconds
            CCFiniteTimeAction coreAction = null;

            CCNode nodeToAddTo = drawNodeRoot;

            switch (VariableOptions [currentVariableIndex])
            {
            case "Position":
                coreAction = new CCMoveTo(timeToTake, touch.Location);

                break;

            case "Scale":
                var distance     = CCPoint.Distance(touch.Location, drawNodeRoot.Position);
                var desiredScale = distance / DefaultCircleRadius;
                coreAction = new CCScaleTo(timeToTake, desiredScale);

                break;

            case "Rotation":
                float differenceY = touch.Location.Y - drawNodeRoot.PositionY;
                float differenceX = touch.Location.X - drawNodeRoot.PositionX;

                float angleInDegrees = -1 * CCMathHelper.ToDegrees(
                    (float)System.Math.Atan2(differenceY, differenceX));

                coreAction = new CCRotateTo(timeToTake, angleInDegrees);

                break;

            case "LineWidth":
                coreAction  = new LineWidthAction(timeToTake, touch.Location.X / 40.0f);
                nodeToAddTo = lineNode;
                break;
            }

            CCAction easing = null;

            switch (EasingOptions [currentEasingIndex])
            {
            case "<None>":
                // no easing, do nothing, it will be handled below
                break;

            case "CCEaseBack":
                if (currentInOutIndex == 0)
                {
                    easing = new CCEaseBackOut(coreAction);
                }
                else if (currentInOutIndex == 1)
                {
                    easing = new CCEaseBackIn(coreAction);
                }
                else
                {
                    easing = new CCEaseBackInOut(coreAction);
                }

                break;

            case "CCEaseBounce":
                if (currentInOutIndex == 0)
                {
                    easing = new CCEaseBounceOut(coreAction);
                }
                else if (currentInOutIndex == 1)
                {
                    easing = new CCEaseBounceIn(coreAction);
                }
                else
                {
                    easing = new CCEaseBounceInOut(coreAction);
                }

                break;

            case "CCEaseElastic":
                if (currentInOutIndex == 0)
                {
                    easing = new CCEaseElasticOut(coreAction);
                }
                else if (currentInOutIndex == 1)
                {
                    easing = new CCEaseElasticIn(coreAction);
                }
                else
                {
                    easing = new CCEaseElasticInOut(coreAction);
                }

                break;

            case "CCEaseExponential":
                if (currentInOutIndex == 0)
                {
                    easing = new CCEaseExponentialOut(coreAction);
                }
                else if (currentInOutIndex == 1)
                {
                    easing = new CCEaseExponentialIn(coreAction);
                }
                else
                {
                    easing = new CCEaseExponentialInOut(coreAction);
                }

                break;

            case "CCEaseSine":

                if (currentInOutIndex == 0)
                {
                    easing = new CCEaseSineOut(coreAction);
                }
                else if (currentInOutIndex == 1)
                {
                    easing = new CCEaseSineIn(coreAction);
                }
                else
                {
                    easing = new CCEaseSineInOut(coreAction);
                }

                break;
            }

            if (easing != null)
            {
                nodeToAddTo.AddAction(easing);
            }
            else
            {
                nodeToAddTo.AddAction(coreAction);
            }
        }