Exemple #1
0
        public override void Update(float dt)
        {
            base.Update(dt);


            if (CCMouse.Instance.HasPosition)
            {
                // turn the control body based on the angle relative to the actual body
                cpVect mouseDelta = cpVect.cpvsub(CCMouse.Instance.Position, tankBody.GetPosition());
                float  turn       = cpVect.cpvtoangle(cpVect.cpvunrotate(tankBody.GetRotation(), mouseDelta));
                tankControlBody.SetAngle(tankBody.GetAngle() - turn);

                // drive the tank towards the mouse
                if (cpVect.cpvnear(CCMouse.Instance.Position, tankBody.GetPosition(), 30))
                {
                    tankControlBody.SetVelocity(cpVect.Zero);                     // stop
                }
                else
                {
                    float direction = (cpVect.cpvdot(mouseDelta, tankBody.GetRotation()) > 0 ? 1 : -1);
                    tankControlBody.SetVelocity(cpVect.cpvrotate(tankBody.GetRotation(), new cpVect(30 * direction, 0)));
                }
            }


            space.Step(dt);
        }
        //public override void Visit()
        //{
        //    base.AdditionalTransform = NodeToBodyTransform();
        //    base.Visit();
        //}

        // returns the transform matrix according the Chipmunk Body values
        public CCAffineTransform NodeToBodyTransform()
        {
            var angle = CCPoint.ForAngle(-CCMathHelper.ToRadians(RotationX));

            cpVect rot = (IgnoreBodyRotation ? new cpVect(angle.X, angle.Y) : _body.GetRotation()); //TODO: CHECK ROT
            double x   = _body.GetPosition().x + (double)rot.x * -AnchorPointInPoints.X - (double)rot.y * (-AnchorPointInPoints.Y);
            double y   = _body.GetPosition().y + (double)rot.y * -AnchorPointInPoints.X + (double)rot.x * (-AnchorPointInPoints.Y);

            return(new CCAffineTransform((float)rot.x, (float)rot.y, (float)-rot.y, (float)rot.x, (float)x, (float)y));
        }
Exemple #3
0
 /// <summary>
 /// Updates the transforms position to the physics bodys position
 /// NOTE: When settings the position directly of a transform, you are also setting the position of the physics object
 /// This create unwanted behavoir so use this function to set the position without changing the rigidbodys transform
 /// </summary>
 public void UpdateToBodyTransform()
 {
     if (HasPhysicsAttached)
     {
         cpVect pos = AttachedPhysicsBody.GetPosition();
         _position = new Vector2(pos.x * Physics.PHYSICS_TRANSFORM_SCALE, pos.y * Physics.PHYSICS_TRANSFORM_SCALE);
         _rotation = AttachedPhysicsBody.GetAngle();
     }
 }
Exemple #4
0
        void eachBody(cpBody body)
        {
            cpVect pos = body.GetPosition();

            if (pos.y < -400 || cp.cpfabs(pos.x) > 340)
            {
                body.SetPosition(new cpVect(
                                     RandomHelper.next(-300, 300),
                                     RandomHelper.next(350, 900)));
            }
        }
Exemple #5
0
        void planetGravityVelocityFunc(cpBody body, cpVect gravity, float damping, float dt)
        {
            // Gravitational acceleration is proportional to the inverse square of
            // distance, and directed toward the origin. The central planet is assumed
            // to be massive enough that it affects the satellites but not vice versa.
            cpVect p      = body.GetPosition();
            float  sqdist = cpVect.cpvlengthsq(p);
            cpVect g      = cpVect.cpvmult(p, -gravityStrength / (sqdist * cp.cpfsqrt(sqdist)));

            body.UpdateVelocity(g, damping, dt);
        }
    //UpdatePosition then to Rendering game
    public void UpdatePosition(float dt)
    {
        mRigidbody2D.UpdatePosition(dt);

        // X.Tools.LogUtils.Debug(X.Tools.LogTag.Test, this.gameObject.name + ":" + mRigidbody2D.GetPosition().ToString());
        cpPos = mRigidbody2D.GetPosition();
        mPostion.Set(cpPos.x, cpPos.y);
        mAngle = cp.cpfangle(mRigidbody2D.GetAngle());
        //this.transform.SetPositionAndRotation(new Vector3(cpPos.x * 0.01f, cpPos.y * 0.01f, 0f), Quaternion.Euler(0f,0f, mAngle));//z-Euler
        this.transform.position = mPostion * XSpaceManager.UnitsPerPixel;
        this.transform.rotation = Quaternion.Euler(0f, 0f, mAngle);
    }
Exemple #7
0
        public override void Update(long gametime)
        {
            physic.UpdateVelocity(space.GetGravity(), 1, 0.01f);
            physic.UpdatePosition(1);
            shp.Update(trsf);
            cpVect position = physic.GetPosition();

            InvokeOnMainThread(() => {
                SetPosition(position);
            });

            angle = (physic.GetAngle() % 360) / 57.2958f;
            if (position.y > 500)
            {
                physic.ApplyImpulse(new cpVect(1, -150), new cpVect(0.1f, 0.1f));
            }
        }
Exemple #8
0
        public override void Update(float dt)
        {
            base.Update(dt);

            float coef = (2.0f + ChipmunkDemoKeyboard.y) / 3.0f;
            float rate = ChipmunkDemoKeyboard.x * 30.0f * coef;

            motor.SetRate(rate);
            motor.SetMaxForce(rate > 0 ? 1000000.0f : 0.0f);

            space.Step(dt);

            for (int i = 0; i < numBalls; i++)
            {
                cpBody ball = balls[i];
                cpVect pos  = ball.GetPosition();

                if (pos.x > 320.0f)
                {
                    ball.SetVelocity(cpVect.Zero);
                    ball.SetPosition(new cpVect(-224.0f, 200.0f));
                }
            }
        }
        public override void OnEnter()
        {
            base.OnEnter();

            SetSubTitle("This unicycle is completely driven and balanced by a single cpSimpleMotor.\nMove the mouse to make the unicycle follow it.");

            space.SetIterations(30);
            space.SetGravity(new cpVect(0, -500));

            {
                cpShape shape      = null;
                cpBody  staticBody = space.GetStaticBody();

                shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(-3200, -240), new cpVect(3200, -240), 0.0f));
                shape.SetElasticity(1.0f);
                shape.SetFriction(1.0f);
                shape.SetFilter(NOT_GRABBABLE_FILTER);

                shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(0, -200), new cpVect(240, -240), 0.0f));
                shape.SetElasticity(1.0f);
                shape.SetFriction(1.0f);
                shape.SetFilter(NOT_GRABBABLE_FILTER);

                shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(-240, -240), new cpVect(0, -200), 0.0f));
                shape.SetElasticity(1.0f);
                shape.SetFriction(1.0f);
                shape.SetFilter(NOT_GRABBABLE_FILTER);
            }


            {
                float radius = 20.0f;
                float mass   = 1.0f;

                float moment = cp.MomentForCircle(mass, 0.0f, radius, cpVect.Zero);

                wheel_body = space.AddBody(new cpBody(mass, moment));
                wheel_body.SetPosition(new cpVect(0.0f, -160.0f + radius));

                cpShape shape = space.AddShape(new cpCircleShape(wheel_body, radius, cpVect.Zero));
                shape.SetFriction(0.7f);
                shape.SetFilter(new cpShapeFilter(1, cp.ALL_CATEGORIES, cp.ALL_CATEGORIES));
            }

            {
                float cog_offset = 30.0f;

                cpBB bb1 = new cpBB(-5.0f, 0.0f - cog_offset, 5.0f, cog_offset * 1.2f - cog_offset);
                cpBB bb2 = new cpBB(-25.0f, bb1.t, 25.0f, bb1.t + 10.0f);

                float mass   = 3.0f;
                float moment = cp.MomentForBox2(mass, bb1) + cp.MomentForBox2(mass, bb2);

                balance_body = space.AddBody(new cpBody(mass, moment));
                balance_body.SetPosition(new cpVect(0.0f, wheel_body.GetPosition().y + cog_offset));

                cpShape shape = null;

                shape = space.AddShape(cpPolyShape.BoxShape2(balance_body, bb1, 0.0f));
                shape.SetFriction(1.0f);
                shape.SetFilter(new cpShapeFilter(1, cp.ALL_CATEGORIES, cp.ALL_CATEGORIES));

                shape = space.AddShape(cpPolyShape.BoxShape2(balance_body, bb2, 0.0f));
                shape.SetFriction(1.0f);
                shape.SetFilter(new cpShapeFilter(1, cp.ALL_CATEGORIES, cp.ALL_CATEGORIES));
            }

            cpVect anchorA  = balance_body.WorldToLocal(wheel_body.GetPosition());
            cpVect groove_a = cpVect.cpvadd(anchorA, new cpVect(0.0f, 30.0f));
            cpVect groove_b = cpVect.cpvadd(anchorA, new cpVect(0.0f, -10.0f));

            space.AddConstraint(new cpGrooveJoint(balance_body, wheel_body, groove_a, groove_b, cpVect.Zero));
            space.AddConstraint(new cpDampedSpring(balance_body, wheel_body, anchorA, cpVect.Zero, 0.0f, 6.0e2f, 30.0f));

            motor = space.AddConstraint(new cpSimpleMotor(wheel_body, balance_body, 0.0f));
            motor.SetPreSolveFunc((s) => motor_preSolve(motor, s));

            {
                float width  = 100.0f;
                float height = 20.0f;
                float mass   = 3.0f;

                cpBody boxBody = space.AddBody(new cpBody(mass, cp.MomentForBox(mass, width, height)));
                boxBody.SetPosition(new cpVect(200, -100));

                cpShape shape = space.AddShape(cpPolyShape.BoxShape(boxBody, width, height, 0.0f));
                shape.SetFriction(0.7f);
            }


            Schedule();
        }
Exemple #10
0
        void motor_preSolve(cpConstraint motor, cpSpace space)
        {
            float dt = space.GetCurrentTimeStep();

            float target_x = CCMouse.Instance.Position.x;

            paint = new shape
            {
                point1 = new cpVect(target_x, -1000.0f),
                point2 = new cpVect(target_x, 1000.0f),
            };

            float max_v      = 500.0f;
            float target_v   = cp.cpfclamp(cp.bias_coef(0.5f, dt / 1.2f) * (target_x - balance_body.GetPosition().x) / dt, -max_v, max_v);
            float error_v    = (target_v - balance_body.GetVelocity().x);
            float target_sin = 3.0e-3f * cp.bias_coef(0.1f, dt) * error_v / dt;

            float max_sin = cp.cpfsin(0.6f);

            balance_sin = cp.cpfclamp(balance_sin - 6.0e-5f * cp.bias_coef(0.2f, dt) * error_v / dt, -max_sin, max_sin);
            float target_a     = (float)Math.Asin(cp.cpfclamp(-target_sin + balance_sin, -max_sin, max_sin));
            float angular_diff = (float)Math.Asin(cpVect.cpvcross(balance_body.GetRotation(), cpVect.cpvforangle(target_a)));
            float target_w     = cp.bias_coef(0.1f, dt / 0.4f) * (angular_diff) / dt;

            float max_rate = 50.0f;
            float rate     = cp.cpfclamp(wheel_body.GetAngularVelocity() + balance_body.GetAngularVelocity() - target_w, -max_rate, max_rate);

            motor.SetRate(cp.cpfclamp(rate, -max_rate, max_rate));
            motor.SetMaxForce(8.0e4f);
        }
Exemple #11
0
        public override void OnEnter()
        {
            base.OnEnter();
            SetSubTitle("Control the crane by moving the mouse. Right click to release.");

            space.SetIterations(30);
            space.SetGravity(new cpVect(0, -100));
            space.SetDamping(0.8f);

            cpBody  staticBody = space.GetStaticBody();
            cpShape shape;

            shape = space.AddShape(new cpSegmentShape(staticBody, new cpVect(-320, -240), new cpVect(320, -240), 0.0f));
            shape.SetElasticity(1.0f);
            shape.SetFriction(1.0f);
            shape.SetFilter(NOT_GRABBABLE_FILTER);

            // Add a body for the dolly.
            dollyBody = space.AddBody(new cpBody(10, cp.Infinity));
            dollyBody.SetPosition(new cpVect(0, 100));

            // Add a block so you can see it.
            space.AddShape(cpPolyShape.BoxShape(dollyBody, 30, 30, 0.0f));

            // Add a groove joint for it to move back and forth on.
            space.AddConstraint(new cpGrooveJoint(staticBody, dollyBody, new cpVect(-250, 100), new cpVect(250, 100), cpVect.Zero));

            // Add a pivot joint to act as a servo motor controlling it's position
            // By updating the anchor points of the pivot joint, you can move the dolly.
            dollyServo = space.AddConstraint(new cpPivotJoint(staticBody, dollyBody, dollyBody.GetPosition()));
            // Max force the dolly servo can generate.
            dollyServo.SetMaxForce(10000);
            // Max speed of the dolly servo
            dollyServo.SetMaxBias(100);
            // You can also change the error bias to control how it slows down.
            dollyServo.SetErrorBias(0.2f);

            // Add the crane hook.
            cpBody hookBody = space.AddBody(new cpBody(1, cp.Infinity));

            hookBody.SetPosition(new cpVect(0, 50));

            // Add a sensor shape for it. This will be used to figure out when the hook touches a box.
            shape = space.AddShape(new cpCircleShape(hookBody, 10, cpVect.Zero));
            shape.SetSensor(true);// cpTrue);
            shape.SetCollisionType(((int)COLLISION_TYPES.HOOK_SENSOR));

            // Add a slide joint to act as a winch motor
            // By updating the max length of the joint you can make it pull up the load.
            winchServo = space.AddConstraint(new cpSlideJoint(dollyBody, hookBody, cpVect.Zero, cpVect.Zero, 0, cp.Infinity));

            // Max force the dolly servo can generate.
            winchServo.SetMaxForce(30000);

            // Max speed of the dolly servo
            winchServo.SetMaxBias(60);

            // TODO: cleanup
            // Finally a box to play with
            cpBody boxBody = space.AddBody(new cpBody(30, cp.MomentForBox(30, 50, 50)));

            boxBody.SetPosition(new cpVect(200, -200));

            // Add a block so you can see it.
            shape = space.AddShape(cpPolyShape.BoxShape(boxBody, 50, 50, 0));
            shape.SetFriction(0.7f);
            shape.SetCollisionType(((int)COLLISION_TYPES.CRATE));

            var handler = space.AddCollisionHandler(
                (int)COLLISION_TYPES.HOOK_SENSOR,
                (int)COLLISION_TYPES.CRATE);

            handler.beginFunc = HookCrate;
            Schedule();
        }
Exemple #12
0
 void AttachHook(cpBody hook, cpBody crate)
 {
     hookJoint = space.AddConstraint(new cpPivotJoint(hook, crate, hook.GetPosition()));
 }
Exemple #13
0
        static float k_scalar_body(cpBody body, cpVect point, cpVect n)
        {
            float rcn = cpVect.cpvcross(cpVect.cpvsub(point, body.GetPosition()), n);

            return(1.0f / body.GetMass() + rcn * rcn / body.GetMoment());
        }