A mouse joint is used to make a point on a body track a specified world point. This a soft constraint with a maximum force. This allows the constraint to stretch and without applying huge forces. NOTE: this joint is not documented in the manual because it was developed to be used in the testbed. If you want to learn how to use the mouse joint, look at the testbed.
Наследование: Joint
Пример #1
0
        private Body BuildHexagon(BodyRenderer renderer, Color color, Vector2 pos, float radius, float spacing)
        {
            var poly = ShapeFactory.Hexagon(Vector2.Zero, radius);
            var body = Physic.World.CreatePolygon(new Vertices(poly), 1, pos);
            body.SetCollisionType(HexPhysicTypes.Ground);

            if (radius < 5)
            {
                body.BodyType = BodyType.Dynamic;
                body.AngularDamping = 5f;
                /*var distanceJoint = K2.Physic.JointFactory.CreateFixedDistanceJoint(body, Vector2.Zero, pos);
                    distanceJoint.Frequency = 10;
                    distanceJoint.Length = radius * .2f;
                    distanceJoint.DampingRatio = 1;*/

                var j = new FixedMouseJoint(body, pos)
                        {
                            Frequency = 1000,
                            MaxForce = 100,
                            DampingRatio = 1,
                            Breakpoint = 150,
                        };
                Physic.World.AddJoint(j);
            }

            renderer.Add(body, color);

            return body;
        }
Пример #2
0
        public void clearLock()
        {
            if(fixedMouseJoint != null)
            {
                world.RemoveJoint(fixedMouseJoint);
                fixedMouseJoint = null;
            }

            if (idleJoint != null)
            {
                return;
            }

            selectedEntryIndex = (int)Math.Round(entries.Count * normalizeOrientation(wheelBody.Rotation) / (2 * Math.PI), 0, MidpointRounding.AwayFromZero) % entries.Count;
            selectedEntryPosition = getSeletedEntryPosition();
            Fixture savedFixture = world.TestPoint(selectedEntryPosition);
            if (savedFixture != null)
            {
                Body body = savedFixture.Body;
                idleJoint = new FixedMouseJoint(body, new Vector2(radius, 0));

                idleJoint.DampingRatio = 0f;
                idleJoint.MaxForce = 3000.0f * body.Mass;
                world.AddJoint(idleJoint);
                body.Awake = true;
                idleJoint.WorldAnchorB = selectedEntryPosition;
            }
        }
        protected override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            MouseState ms = Mouse.GetState();
            Vector2 position = camera.ConvertScreenToWorld(new Vector2(ms.X,ms.Y),true,false);
            
            if (ms.LeftButton == ButtonState.Pressed && _fixedMouseJoint == null)
            {
                Fixture savedFixture = world.World.TestPoint(position);
                if (savedFixture != null)
                {
                    Body body = savedFixture.Body;
                    _fixedMouseJoint = new FixedMouseJoint(body, position);
                    _fixedMouseJoint.MaxForce = 1000.0f * body.Mass;
                    world.World.AddJoint(_fixedMouseJoint);
                    body.Awake = true;
                }
            }

            if (ms.LeftButton == ButtonState.Released && _fixedMouseJoint != null)
            {
                world.World.RemoveJoint(_fixedMouseJoint);
                _fixedMouseJoint = null;
            }

            if (_fixedMouseJoint != null)
            {
                _fixedMouseJoint.WorldAnchorB = position;
            }
        }        
Пример #4
0
		public override void update()
		{
			if( enableMousePicking )
			{
				if( Input.leftMouseButtonPressed )
				{
					var pos = Core.scene.camera.screenToWorldPoint( Input.mousePosition );
					var fixture = world.testPoint( FSConvert.displayToSim * pos );
					if( fixture != null && !fixture.body.isStatic && !fixture.body.isKinematic )
						_mouseJoint = fixture.body.createFixedMouseJoint( pos );
				}

				if( Input.leftMouseButtonDown && _mouseJoint != null )
				{
					var pos = Core.scene.camera.screenToWorldPoint( Input.mousePosition );
					_mouseJoint.worldAnchorB = FSConvert.displayToSim * pos;
				}

				if( Input.leftMouseButtonReleased && _mouseJoint != null )
				{
					world.removeJoint( _mouseJoint );
					_mouseJoint = null;
				}
			}

			world.step( MathHelper.Min( Time.deltaTime, minimumUpdateDeltaTime ) );
		}
Пример #5
0
        public static FixedMouseJoint CreateFixedMouseJoint(World world, Body body, Vector2 worldAnchor)
        {
            FixedMouseJoint joint = new FixedMouseJoint(body, worldAnchor);

            world.Add(joint);
            return(joint);
        }
Пример #6
0
    public virtual void MouseDrag()
    {
        // mouse press
        if(Input.GetMouseButtonDown(0) && mouseJoint == null)
        {
            Body body = GetBodyAtMouse();
            if(body != null)
            {
                FVector2 target = new FVector2(MouseXWorldPhys, MouseYWorldPhys);
                mouseJoint = JointFactory.CreateFixedMouseJoint(FSWorldComponent.PhysicsWorld, body, target);
                mouseJoint.CollideConnected = true;
                mouseJoint.MaxForce = 300f * body.Mass;
                body.Awake = true;
            }
        }
        // mouse release
        if(Input.GetMouseButtonUp(0))
        {
            if(mouseJoint != null)
            {
                FSWorldComponent.PhysicsWorld.RemoveJoint(mouseJoint);
                mouseJoint = null;
            }
        }

        // mouse move
        if(mouseJoint != null)
        {
            FVector2 p2 = new FVector2(MouseXWorldPhys, MouseYWorldPhys);
            mouseJoint.WorldAnchorB = p2;
        }
    }
Пример #7
0
		protected override Joint CreateJoint(Body bodyA, Body bodyB)
		{
			if (bodyA == null) return null;
			FixedMouseJoint mj = new FixedMouseJoint(bodyA, Vector2.Zero);
			Scene.PhysicsWorld.AddJoint(mj);
			return mj;
		}
Пример #8
0
        protected override void CreateExtraJoints(World world)
        {
            rightHandSpring = new FixedMouseJoint(_lowerRightArm, _lowerRightArm.Position);
            rightHandSpring.MaxForce = 1000.0f * _lowerRightArm.Mass;
            world.AddJoint(rightHandSpring);

            leftHandSpring = new FixedMouseJoint(_lowerLeftArm, _lowerLeftArm.Position);
            leftHandSpring.MaxForce = 1000.0f * _lowerLeftArm.Mass;
            world.AddJoint(leftHandSpring);
        }
Пример #9
0
        public override void LoadContent(ContentManager content, World physics)
        {
            base.LoadContent(content, physics);

            this.physics.Position = ConvertUnits.ToSimUnits(50, 50);
            this.physics.CollisionCategories = Category.Cat1;
            this.physics.CollidesWith = Category.Cat1;

            fixture = new FixedMouseJoint(this.physics, ConvertUnits.ToSimUnits(new Vector2(50)));
            fixture.MaxForce = float.MaxValue;

            physics.AddJoint(fixture);
        }
Пример #10
0
 public void Update(GameTime gameTime, CameraOperator cameraOperator, World world)
 {
     _sprite.Body.Center = Input.Mouse.Position;
     if (_mouseJoint != null)
     {
         if (Input.Mouse.IsButtonDown(Input.Mouse.MouseButton.Left))
         {
             _mouseJoint.WorldAnchorB = cameraOperator.Camera.PositionOnWorld(Input.Mouse.Position);
         }
         else
         {
             world.RemoveJoint(_mouseJoint);
             _mouseJoint = null;
         }
     }
     else
     {
         Vector2 cursorWorldPosition = cameraOperator.Camera.PositionOnWorld(Input.Mouse.Position);
         if (Input.Mouse.IsButtonClicked(Input.Mouse.MouseButton.Left))
         {
             Fixture fixture;
             float radiusMeters = _sprite.Body.Projection.BoundingBox.Dimensions.X.ToMeters();
             if ((fixture =
                 world.TestPoint(cursorWorldPosition - new Vector2(radiusMeters))) != null ||
                 (fixture =
                     world.TestPoint(cursorWorldPosition + new Vector2(radiusMeters))) != null ||
                 (fixture =
                     world.TestPoint(cursorWorldPosition + new Vector2(radiusMeters, -radiusMeters))) != null ||
                 (fixture =
                     world.TestPoint(cursorWorldPosition - new Vector2(radiusMeters, -radiusMeters))) != null)
             {
                 if (fixture.UserData is Coin)
                 {
                     _mouseJoint = new FixedMouseJoint(fixture.Body, cursorWorldPosition)
                     {
                         MaxForce = 200*fixture.Body.Mass
                     };
                     world.AddJoint(_mouseJoint);
                 }
             }
         }
     }
 }
        protected override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            // Process touch events
            TouchCollection touchCollection = TouchPanel.GetState();
            if (touchCollection.Count > 0)
            {
                TouchLocation tl = touchCollection[0];

                Vector2 position = camera.ConvertScreenToWorld(new Vector2(tl.Position.X,
                         tl.Position.Y),true,false);

                if (tl.State == TouchLocationState.Pressed && _fixedMouseJoint == null)
                {
                    Fixture savedFixture = world.World.TestPoint(position);
                    if (savedFixture != null)
                    {
                        Body body = savedFixture.Body;
                        _fixedMouseJoint = new FixedMouseJoint(body, position);
                        _fixedMouseJoint.MaxForce = 1000.0f * body.Mass;
                        world.World.AddJoint(_fixedMouseJoint);
                        body.Awake = true;
                    }
                }

                if (tl.State == TouchLocationState.Released && _fixedMouseJoint != null)
                {
                    world.World.RemoveJoint(_fixedMouseJoint);
                    _fixedMouseJoint = null;
                }

                if (_fixedMouseJoint != null)
                {
                    _fixedMouseJoint.WorldAnchorB = position;
                }

            }
        }
Пример #12
0
        private void MouseDown(Vector2 p)
        {
            if (_fixedMouseJoint != null)
                return;

            Fixture fixture = World.TestPoint(p);

            if (fixture != null)
            {
                Body body = fixture.Body;
                _fixedMouseJoint = new FixedMouseJoint(body, p);
                _fixedMouseJoint.MaxForce = 1000.0f * body.Mass;
                World.AddJoint(_fixedMouseJoint);
                body.Awake = true;
            }
        }
Пример #13
0
 protected virtual void JointRemoved(Joint joint)
 {
     if (_fixedMouseJoint == joint)
         _fixedMouseJoint = null;
 }
Пример #14
0
        private void HandleCursor(InputHelper input)
        {
            Vector2 position = Camera.ConvertScreenToWorld(input.Cursor);

            if ((input.IsNewButtonPress(Buttons.A) ||
                 input.IsNewMouseButtonPress(MouseButtons.LeftButton)) &&
                _fixedMouseJoint == null)
            {
                Fixture savedFixture = World.TestPoint(position);
                if (savedFixture != null)
                {
                    Body body = savedFixture.Body;
                    _fixedMouseJoint = new FixedMouseJoint(body, position);
                    _fixedMouseJoint.MaxForce = 1000.0f * body.Mass;
                    World.AddJoint(_fixedMouseJoint);
                    body.Awake = true;
                }
            }

            if ((input.IsNewButtonRelease(Buttons.A) ||
                 input.IsNewMouseButtonRelease(MouseButtons.LeftButton)) &&
                _fixedMouseJoint != null)
            {
                World.RemoveJoint(_fixedMouseJoint);
                _fixedMouseJoint = null;
            }

            if (_fixedMouseJoint != null)
            {
                _fixedMouseJoint.WorldAnchorB = position;
            }
        }
Пример #15
0
		public override Joint createJoint()
		{
			var joint = new FixedMouseJoint( bodyA, worldAnchor * FSConvert.displayToSim );
			joint.collideConnected = collideConnected;

			// conditionally set the maxForce
			if( maxForce > 0 )
				joint.maxForce = maxForce;
			
			joint.frequency = frequency;
			joint.dampingRatio = dampingRatio;
			return joint;
		}
Пример #16
0
        private void MouseDown(Vector2 p)
        {

            InputHelper inputHelper = game.inputManager.inputHelper;

            if (_fixedMouseJoint != null)
            {
                return;
            }

            savedFixture = game.farseerManager.world.TestPoint(p);

            if (savedFixture != null)
            {
                Body body = savedFixture.Body;
                if (!body.IsStatic)
                {
                    _fixedMouseJoint = new FixedMouseJoint(body, p);
                    _fixedMouseJoint.MaxForce = 1000.0f * body.Mass;
                    game.farseerManager.world.AddJoint(_fixedMouseJoint);
                    body.Awake = true;
                }
                else
                {
                    dragging = true;
                }
            }
            else // start a selection rectangle
            {
                dragStartWorld = p;
                dragStartPixel = inputHelper.MousePosition;
                selectingRectangle = true;
                dragArea = new DragArea(dragStartPixel, dragStartPixel);
            }
        }
Пример #17
0
        public void sendCreateMouseJoint(FixedMouseJoint mouseJoint)
        {
            NetOutgoingMessage om = _peer.CreateMessage();
            Body body = mouseJoint.BodyA;
            Vector2 localAnchorA = body.GetLocalVector(mouseJoint.WorldAnchorA);
            Vector2 point = mouseJoint.WorldAnchorB;

            om.Write((int)MessageType.CreateMouseJoint);
            om.Write((int)body.UserData);   // entityId
            om.Write(localAnchorA.X);
            om.Write(localAnchorA.Y);
            om.Write(point.X);
            om.Write(point.Y);

            _peer.SendMessage(om, _peer.Connections[0], NetDeliveryMethod.ReliableUnordered);
        }
Пример #18
0
 private void MouseUp()
 {
     if (_fixedMouseJoint != null)
     {
         World.RemoveJoint(_fixedMouseJoint);
         _fixedMouseJoint = null;
     }
 }
Пример #19
0
 public void Loose()
 {
     Screen.World.RemoveJoint(fixedJoint);
     fixedJoint = null;
     AttachedEntity = null;
 }
Пример #20
0
 public void Cut()
 {
     Screen.World.RemoveJoint(HalfJoint);
     Screen.World.RemoveJoint(fixedJoint);
     AttachedEntity = null;
     fixedJoint = null;
 }
        public void CreatePickSpring(sw.Point ptMouse)
        {
            xna.Vector2 pointScreen = new xna.Vector2((float)(ptMouse.X), (float)(ptMouse.Y));

            xna.Vector2 point = PhysicsCanvas.BoundaryHelper.ScreenToWorld(pointScreen);
            _mousePickSpring = new FixedMouseJoint(this.BodyObject, point);
            _mousePickSpring.MaxForce = 1000.0f * this.BodyObject.Mass;
            _mousePickSpring.Enabled = true;

            _physicsCanvas.Simulator.AddJoint(_mousePickSpring);
            this.BodyObject.Awake = true;




            xna.Vector2 pointBody = PhysicsCanvas.BoundaryHelper.WorldToScreen(this.BodyObject.Position);
            _lineShowSpring.X1 = pointBody.X;
            _lineShowSpring.Y1 = pointBody.Y;
            _lineShowSpring.X2 = pointScreen.X;
            _lineShowSpring.Y2 = pointScreen.Y;
            _lineShowSpring.Visibility = Visibility.Visible;
        }
        public void DestroyPickSpring()
        {
            if (_mousePickSpring != null)
            {
                _mousePickSpring.Enabled = false;
                _physicsCanvas.Simulator.RemoveJoint(_mousePickSpring);
                _mousePickSpring = null;

                if (_lineShowSpring != null)
                {
                    _lineShowSpring.Visibility = Visibility.Collapsed;
                }


                if (AllowStaticObjectManipulation && _bodyWasStatic)
                {
                    // save the static attributes
                    BodyObject.AngularDamping = _originalAngularDamping;
                    BodyObject.IsStatic = true;
                    _bodyWasStatic = false;
                }

                if (MousePickCompleted != null)
                    MousePickCompleted(this, _posMouseMoveStart);
            }
        }
Пример #23
0
        public virtual void HandleCursor(InputState input)
        {
            PlayerIndex player;
            Vector2 position = Camera.ConvertScreenToWorld(input.Cursor);

            if ((input.IsNewButtonPress(Buttons.A, PlayerIndex.One, out player) ||
                    input.IsNewMouseButtonPress(MouseButtons.LeftButton)) &&
                _fixedMouseJoint == null)
            {
                Fixture savedFixture = World.TestPoint(position);
                if (savedFixture != null && savedFixture.UserData is SimpleAxiosGameObject && ((SimpleAxiosGameObject)(savedFixture.UserData)).AllowAutomaticMouseJoint)
                {
                    Body body = savedFixture.Body;
                    _fixedMouseJoint = new FixedMouseJoint(body, position);
                    _fixedMouseJoint.MaxForce = 1000.0f * body.Mass;
                    World.AddJoint(_fixedMouseJoint);
                    body.Awake = true;
                }
            }

            if ((input.IsNewButtonRelease(Buttons.A, ControllingPlayer.Value, out player) ||
                    input.IsNewMouseButtonRelease(MouseButtons.LeftButton)) &&
                _fixedMouseJoint != null)
            {
                World.RemoveJoint(_fixedMouseJoint);
                _fixedMouseJoint = null;
            }

            if (_fixedMouseJoint != null)
            {
                _fixedMouseJoint.WorldAnchorB = position;
            }
        }
Пример #24
0
        private void MouseUp(Vector2 p)
        {

            InputHelper inputHelper = game.inputManager.inputHelper;
            prevWorldLoc = new Vector2();

            dragging = false;

            if (_fixedMouseJoint != null)
            {
                game.farseerManager.world.RemoveJoint(_fixedMouseJoint);
                _fixedMouseJoint = null;
            }

            if (selectingRectangle)
            {
                selectingRectangle = false;
                if (!ProjectionHelper.InsidePixelBounds(inputHelper.MousePosition)) return;
                DragArea d = new DragArea(dragStartWorld, p);
                List<Object> selected = new List<object>();
                foreach (Body b in game.farseerManager.world.BodyList)
                {
                    if (d.ContainsPixel(b.Position))
                        selected.Add(b);
                }

                foreach (Joint j in game.farseerManager.world.JointList)
                {
                    if (d.ContainsPixel(j.WorldAnchorA))
                        selected.Add(j);
                }

                if (selected.Count > 0)
                {
                    FormManager.Property.setPendingObjects(selected);
                    //if (!FormManager.Property.Visible)
                    //    FormManager.Property.Show();

                }
            }
        }
Пример #25
0
        private void Circle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //Debug.Print("Circle_MouseLeftButtonDown");
            if (_isDragging) return;
            Debug.Print("Circle_MouseLeftButtonDown - drag start");
            //Mouse.Capture(this, CaptureMode.SubTree);
            //this.CaptureMouse();
            _isDragging = true;
            var circlePoint = e.GetPosition(this);
            var screenPoint = PointToScreen(circlePoint);
            var screenPosition = GetVector2(screenPoint);
            var worldPosition = ConvertUnits.ToSimUnits(screenPosition);

            Textout.Text = "down";

            var fixture = _world.TestPoint(worldPosition);

            if (fixture != null)
            {
                Body body = fixture.Body;
                _mouseJoint = new FixedMouseJoint(body, worldPosition) { MaxForce = 1000000.0f * body.Mass };
                _world.AddJoint(_mouseJoint);
                body.Awake = true;
            }
        }
Пример #26
0
        private void MouseDown(Vector2 p)
        {
            if (_fixedMouseJoint != null)
            {
                return;
            }

            // Make a small box.
            AABB aabb;
            Vector2 d = new Vector2(0.001f, 0.001f);
            aabb.LowerBound = p - d;
            aabb.UpperBound = p + d;

            Fixture savedFixture = null;

            // Query the world for overlapping shapes.
            World.QueryAABB(
                fixture =>
                {
                    Body body = fixture.Body;
                    if (body.BodyType == BodyType.Dynamic)
                    {
                        bool inside = fixture.TestPoint(ref p);
                        if (inside)
                        {
                            savedFixture = fixture;

                            // We are done, terminate the query.
                            return false;
                        }
                    }

                    // Continue the query.
                    return true;
                }, ref aabb);

            if (savedFixture != null)
            {
                Body body = savedFixture.Body;
                _fixedMouseJoint = new FixedMouseJoint(body, p);
                _fixedMouseJoint.MaxForce = 1000.0f * body.Mass;
                World.AddJoint(_fixedMouseJoint);
                body.Awake = true;
            }
        }
Пример #27
0
        public void AttachTo(BaseEntity entity)
        {
            if (fixedJoint != null) {
                Screen.World.RemoveJoint(fixedJoint);
            }

            AttachedEntity = entity;
            var grabJoint = joints.Last();

            var pos = grabJoint.WorldCenter;
            pos.Y += ConvertUnits.ToSimUnits(10);

            fixedJoint = new FixedMouseJoint(grabJoint, pos);
            fixedJoint.MaxForce = 1000f * 10000; //I don't really know which numbers are good here lol

            fixedJoint.Frequency = 10;

            Screen.World.AddJoint(fixedJoint);

            fixedJoint.WorldAnchorB = ConvertUnits.ToSimUnits(entity.BoundingBox.Center.X, entity.BoundingBox.Center.Y);
            grabJoint.Awake = true;
        }
Пример #28
0
        private void LoadDynamicObstacles()
        {
            _plankBody = BodyFactory.CreateRectangle(World, 300, 10, 1f);
            _plankBody.BodyType = BodyType.Dynamic;
            _plankBody.Restitution = 1f;

            _plankBodySprite = new Sprite(ScreenManager.Assets.TextureFromShape(_plankBody.FixtureList[0].Shape,
                                                                                MaterialType.Squares,
                                                                                Color.Orange, 1f));

            _fixedMouseJoint = new FixedMouseJoint(_plankBody, plankPosition.getLeftHandPosition());

            _fixedMouseJoint.MaxForce = 1000.0f * _plankBody.Mass;
            World.AddJoint(_fixedMouseJoint);
            _plankBody.Awake = true;

            UpdateDynamicObstacles();
        }
Пример #29
0
        private void CreateHandFixture(Hand hand)
        {
            Vector2 handPos = new Vector2(hand.Position.X, hand.Position.Y);
            Body handBody = BodyFactory.CreateCircle(world, handSize, 1f, handPos / MeterInPixels);
            handBody.BodyType = BodyType.Dynamic;

            // Check what hands should be colliding with
            if (handCollisionsEnabled)
            {
                handBody.OnCollision += HandCollisionCheckDelegate;
            }
            else
            {
                handBody.OnCollision += HandCollisionFalseDelegate;
            }

            FixedMouseJoint handJoint = new FixedMouseJoint(handBody, handBody.Position);
            handJoint.MaxForce = 10000f;
            world.AddJoint(handJoint);

            WorldEntity handEntity = new WorldEntity(handBody, handJoint, WorldEntity.EntityType.Hand);
            handBodies.Add(hand, handEntity);
            entities.Add(handBody, handEntity);
        }
Пример #30
0
        public void sendMoveMouseJoint(FixedMouseJoint joint)
        {
            NetOutgoingMessage om = _peer.CreateMessage();

            om.Write((int)MessageType.MoveMouseJoint);
            om.Write(joint.WorldAnchorB.X);
            om.Write(joint.WorldAnchorB.Y);
            _peer.SendMessage(om, _peer.Connections[0], NetDeliveryMethod.ReliableUnordered);
        }
Пример #31
0
 public static FixedMouseJoint CreateFixedMouseJoint(World world, Body body, Vector2 worldAnchor)
 {
     FixedMouseJoint joint = new FixedMouseJoint(body, worldAnchor);
     world.AddJoint(joint);
     return joint;
 }