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); }
/// <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(); } }
//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); }
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)); } }