public bool Place(PhysicsSimulator pS, MouseState mouseStateCurrent, MouseState mouseStatePrevious, Vector2 mPoint) { if (mouseStatePrevious.LeftButton == ButtonState.Released && mouseStateCurrent.LeftButton == ButtonState.Pressed) { joint = JointFactory.Instance.CreateFixedRevoluteJoint(pS, rec.rBody, mPoint); return false; } return true; }
/// <exception cref="InvalidOperationException">Fixed joints cannot be created on static bodies</exception> public FixedRevoluteJoint CreateFixedRevoluteJoint(Body body, Vector2 anchor) { FixedRevoluteJoint revoluteJoint = new FixedRevoluteJoint(body, anchor); if (body.isStatic) { //throw new InvalidOperationException("Fixed joints cannot be created on static bodies"); revoluteJoint.Enabled = false; // if you create a joint of a static body it is created as disabled. } return revoluteJoint; }
public void Load(GraphicsDevice graphicsDevice, PhysicsSimulator physicsSimulator) { _rectangleTexture = DrawingHelper.CreateRectangleTexture(graphicsDevice, RectangleWidth, RectangleHeight, Color.White, Color.Black); int radius; if (AttachPoint == 0 | AttachPoint == 2) { radius = RectangleHeight; } else { radius = RectangleWidth; } _circleTexture = DrawingHelper.CreateCircleTexture(graphicsDevice, radius, Color.White, Color.Black); //body is created as rectangle so that it has the moment of inertia closer to the final shape of the object. Body = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, RectangleWidth, RectangleHeight, 1f); _rectangleGeom = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, Body, RectangleWidth, RectangleHeight); _rectangleGeom.FrictionCoefficient = .5f; _rectangleGeom.CollisionGroup = CollisionGroup; Vector2 offset = Vector2.Zero; switch (AttachPoint) { case 0: { offset = new Vector2(-RectangleWidth/2f, 0); //offset to rectangle to left break; } case 1: { offset = new Vector2(0, -RectangleHeight/2f); //offset to rectangle to top break; } case 2: { offset = new Vector2(RectangleWidth/2f, 0); //offset to rectangle to right break; } case 3: { offset = new Vector2(0, RectangleHeight/2f); //offset to rectangle to bottom break; } } Body.Position = Position - offset; _circleGeom = GeomFactory.Instance.CreateCircleGeom(physicsSimulator, Body, radius, 20, offset, 0); _circleGeom.FrictionCoefficient = .5f; _circleGeom.CollisionGroup = CollisionGroup; _revoluteJoint = JointFactory.Instance.CreateFixedRevoluteJoint(physicsSimulator, Body, Position); physicsSimulator.Add(_revoluteJoint); SpringFactory.Instance.CreateFixedAngleSpring(physicsSimulator, Body, SpringConstant, DampingConstant); }
public override void LoadContent() { bodyA = BodyFactory.Instance.CreateRectangleBody(100, 25, 5); bodyB = BodyFactory.Instance.CreateRectangleBody(100, 25, 5); bodyA.Position = new Vector2(250, 300); bodyB.Position = new Vector2(350, 300); geomA = GeomFactory.Instance.CreateRectangleGeom(bodyA, 100, 25); geomB = GeomFactory.Instance.CreateRectangleGeom(bodyB, 100, 25); weldJoint = new WeldJoint(bodyA, bodyB, new Vector2(300, 300)); weldJoint.Broke += weldJoint_Broke; weldJoint.Breakpoint = 5.0f; PhysicsSimulator.Add(bodyA); PhysicsSimulator.Add(bodyB); PhysicsSimulator.Add(geomA); PhysicsSimulator.Add(geomB); PhysicsSimulator.Add(weldJoint); brush = new PolygonBrush(Vertices.CreateRectangle(100, 25), Color.White, Color.Black, 2.0f, 0.5f); brush.Load(ScreenManager.GraphicsDevice); bodyC = BodyFactory.Instance.CreatePolygonBody(Vertices.CreateGear(50, 20, .50f, 10), 10); bodyC.Position = new Vector2(500, 200); geomC = GeomFactory.Instance.CreatePolygonGeom(bodyC, Vertices.CreateGear(50, 20, .50f, 10), 1.5f); bodyD = BodyFactory.Instance.CreatePolygonBody(Vertices.CreateGear(50, 20, .50f, 10), 10); bodyD.Position = new Vector2(613, 200); geomD = GeomFactory.Instance.CreatePolygonGeom(bodyD, Vertices.CreateGear(50, 20, .50f, 10), 1.5f); geomC.CollisionGroup = 2; geomD.CollisionGroup = 3; geomC.FrictionCoefficient = 0f; geomD.FrictionCoefficient = 0f; PhysicsSimulator.Add(bodyC); PhysicsSimulator.Add(geomC); PhysicsSimulator.Add(bodyD); PhysicsSimulator.Add(geomD); gearBrushA = new PolygonBrush(Vertices.CreateGear(50, 20, .50f, 10), Color.White, Color.Black, 0.5f, 0.5f); gearBrushA.Load(ScreenManager.GraphicsDevice); gearBrushB = new PolygonBrush(Vertices.CreateGear(50, 20, .50f, 10), Color.White, Color.Black, 0.5f, 0.5f); gearBrushB.Load(ScreenManager.GraphicsDevice); revJointA = JointFactory.Instance.CreateFixedRevoluteJoint(bodyC, bodyC.Position); revJointB = JointFactory.Instance.CreateFixedRevoluteJoint(bodyD, bodyD.Position); PhysicsSimulator.Add(revJointA); PhysicsSimulator.Add(revJointB); table = new List<Table>(); table.Add(new Table(new Vector2(200, 450), 200, 50)); table[0].Load(PhysicsSimulator, ScreenManager.GraphicsDevice); base.LoadContent(); }