Beispiel #1
0
        public void ConstraintProperties()
        {
            var anchorA = new Vect(1, 1);
            var anchorB = new Vect(0.5, 0.5);

            var constraint = new PinJoint(bodyA, bodyB, anchorA, anchorB);

            Assert.IsNull(constraint.Space, "#1");

            space.AddConstraint(constraint);

            Assert.AreSame(space, constraint.Space, "#1.2");

            Assert.AreSame(bodyA, constraint.BodyA, "#2");
            Assert.AreSame(bodyB, constraint.BodyB, "#3");

            Assert.AreEqual(anchorA, constraint.AnchorA, "#2.1");
            Assert.AreEqual(anchorB, constraint.AnchorB, "#3.1");

            constraint.MaxForce = 1.2;

            Assert.AreEqual(1.2, constraint.MaxForce, "#4");

            constraint.ErrorBias = 10.0;

            Assert.AreEqual(10.0, constraint.ErrorBias, "#5");

            constraint.CollideBodies = false;
            Assert.IsFalse(constraint.CollideBodies, "#6");

            constraint.CollideBodies = true;
            Assert.IsTrue(constraint.CollideBodies, "#7");

            var sb = new StringBuilder();

            constraint.PreSolve  = (c, s) => sb.Append("PreSolve");
            constraint.PostSolve = (c, s) => sb.Append("PostSolve");

            space.Step(0.2);

            constraint.PreSolve  = null;
            constraint.PostSolve = null;

            space.Step(0.2);


            string solve = sb.ToString();

            Assert.AreEqual("PreSolvePostSolve", solve, "#8");
        }
        private void MouseLeftButtonDown(Vector2 position)
        {
            Vect mousePosition = MouseToSpace(position);

            // give the mouse click a little radius to make it easier to click small shapes.
            double radius = 5.0;

            PointQueryInfo info = space.PointQueryNearest(mousePosition, radius, GrabbableFilter);

            if (info == null)
            {
                return;
            }

            Shape shape = info.Shape;
            Body  body  = shape.Body;

            double mass = body.Mass;

            if (double.IsInfinity(mass))
            {
                return;
            }

            // Use the closest point on the surface if the click is outside of the shape.
            Vect nearest = info.Distance > 0.0 ? info.Point : mousePosition;

            cursorJoint           = new PivotJoint(cursorBody, body, Vect.Zero, body.WorldToLocal(nearest));
            cursorJoint.MaxForce  = 5000.0;
            cursorJoint.ErrorBias = Math.Pow(1.0 - 0.15, 60.0);

            space.AddConstraint(cursorJoint);

            demo[currentDemo].OnMouseLeftButtonDown(chipmunkDemoMouse);
        }