Esempio n. 1
0
        public void DidAccelerate(CCEventAccelerate accelEvent)
        {
            CCSize winSize = Layer.VisibleBoundsWorldspace.Size;

            /*FIXME: Testing on the Nexus S sometimes ball is NULL */
            if (ball == null)
            {
                return;
            }

            CCSize ballSize = ball.ContentSize;

            CCPoint ptNow = ball.PositionWorldspace;
            CCPoint ptTemp = WorldToScreenspace(ptNow);
            var orientation = Application.CurrentOrientation;

            if (accelEvent.Acceleration.X == 0.0f && accelEvent.Acceleration.Y == 0.0f)
                return;

            CCLog.Log("Accelerate : X: {0} Y: {1} Z: {2} orientation: {3}", accelEvent.Acceleration.X, accelEvent.Acceleration.Y, accelEvent.Acceleration.Z, orientation );
            if (orientation == CCDisplayOrientation.LandscapeRight || orientation == CCDisplayOrientation.LandscapeLeft)
            {

#if ANDROID
            
                ptTemp.X += (float) accelEvent.Acceleration.Y * 9.81f;
                ptTemp.Y += (float) accelEvent.Acceleration.X * 9.81f;

#elif NETFX_CORE || WINDOWS_PHONE8
                ptTemp.X -= (float) accelEvent.Acceleration.Y * 9.81f;
                ptTemp.Y -= (float) accelEvent.Acceleration.X * 9.81f;
#elif IOS
                ptTemp.X += (float)accelEvent.Acceleration.Y * 9.81f;
                ptTemp.Y += (float)accelEvent.Acceleration.X * 9.81f;
#endif
            }

            CCPoint ptNext = ScreenToWorldspace(ptTemp);
            ptNext.X = MathHelper.Clamp(ptNext.X, (ballSize.Width / 2.0f), (winSize.Width - ballSize.Width / 2.0f));
            ptNext.Y = MathHelper.Clamp(ptNext.Y, (ballSize.Height / 2.0f), (winSize.Height - ballSize.Height / 2.0f));
            ball.Position = ball.WorldToParentspace(ptNext);
        }
Esempio n. 2
0
        public virtual void OnAccelerate(CCEventAccelerate acc)
        {
            float accelX = (float)acc.Acceleration.X * kFilterFactor + (1 - kFilterFactor) * prevX;
            float accelY = (float)acc.Acceleration.Y * kFilterFactor + (1 - kFilterFactor) * prevY;

            prevX = accelX;
            prevY = accelY;

            var v = new CCPoint(accelX, accelY);
            v = v * 200;

            if (Scene != null)
            {
                Scene.PhysicsWorld.Gravity = v;
            }
        }
        public void DidAccelerate(CCEventAccelerate accelEvent)
        {
            CCSize winSize = Layer.VisibleBoundsWorldspace.Size;

            /*FIXME: Testing on the Nexus S sometimes ball is NULL */
            if (ball == null)
            {
                return;
            }

            CCSize ballSize = ball.ContentSize;

            CCPoint ptNow = ball.PositionWorldspace;
            CCPoint ptTemp = Layer.WorldToScreenspace(ptNow);

            var orientation = Application.CurrentOrientation;

            if (accelEvent.Acceleration.X == 0.0f && accelEvent.Acceleration.Y == 0.0f)
                return;

            #if ANDROID || WINDOWS_PHONE8
            if (orientation == CCDisplayOrientation.LandscapeRight)
            {
                ptTemp.X -= (float) accelEvent.Acceleration.X * 9.81f;
                ptTemp.Y -= (float) accelEvent.Acceleration.Y * 9.81f;
            }
            else
            {
                ptTemp.X += (float) accelEvent.Acceleration.X * 9.81f;
                ptTemp.Y += (float) accelEvent.Acceleration.Y * 9.81f;
            }
            #else
            ptTemp.X += (float)accelEvent.Acceleration.X * 9.81f;
            ptTemp.Y += (float)accelEvent.Acceleration.Y * 9.81f;
            #endif

            CCPoint ptNext = Layer.ScreenToWorldspace(ptTemp);
            ptNext.X = MathHelper.Clamp(ptNext.X, (ballSize.Width / 2.0f), (winSize.Width - ballSize.Width / 2.0f));
            ptNext.Y = MathHelper.Clamp(ptNext.Y, (ballSize.Height / 2.0f), (winSize.Height - ballSize.Height / 2.0f));
            ball.Position = ball.WorldToParentspace(ptNext);
        }