public void ShortenRope(Rope rope) { if (rope.PhysicsSegments.Count <= 2) { // Rope cannot be any shorter as 2 segments is minimum return; } // Only allow shortening rope if all joints are restful for (int i = 0; i < rope.PhysicsSegmentJoints.Count; ++i) { var joint = rope.PhysicsSegmentJoints[i]; if (Vector2.Distance(joint.WorldAnchorA, joint.WorldAnchorB) > 0.3f * Rope.SegmentLength) { return; } } PhysicsWorld.RemoveJoint(rope.PhysicsEntityJoint); PhysicsWorld.RemoveJoint(rope.PhysicsSegmentJoints.Last()); rope.PhysicsSegmentJoints.Remove(rope.PhysicsSegmentJoints.Last()); RemoveBody(rope.PhysicsSegments.Last()); rope.PhysicsSegments.Remove(rope.PhysicsSegments.Last()); var ballJoint = JointFactory.CreateRevoluteJoint(PhysicsWorld, rope.PhysicsSegments.Last(), rope.AttachedEntity.PhysicsBody, Vector2.Zero, Vector2.Zero); ballJoint.CollideConnected = false; rope.PhysicsEntityJoint = ballJoint; }
public override void Update(GameTime gameTime) { if (Joint == null) { if (Object1 == null) { return; } if (Object2 == null) { Fixture referenceObject = FixtureFactory.CreateRectangle(Level.Physics, 0.1f, 0.1f, 0); referenceObject.Body.Position = Object1.fixture.Body.Position; referenceObject.Body.BodyType = BodyType.Static; Joint = JointFactory.CreateRevoluteJoint(Level.Physics, Object1.fixture.Body, referenceObject.Body, Vector2.Zero); } else { Joint = JointFactory.CreateRevoluteJoint(Level.Physics, Object1.fixture.Body, Object2.fixture.Body, Vector2.Zero); } ((RevoluteJoint)Joint).LowerLimit = MinAngle; ((RevoluteJoint)Joint).UpperLimit = MaxAngle; ((RevoluteJoint)Joint).LimitEnabled = EnableLimit; ((RevoluteJoint)Joint).MotorSpeed = MotorSpeed; ((RevoluteJoint)Joint).MotorEnabled = EnableMotor; } Circle.position = position; }
Multithread2Test() { // enable multithreading World.ContactManager.VelocityConstraintsMultithreadThreshold = 256; World.ContactManager.PositionConstraintsMultithreadThreshold = 256; World.ContactManager.CollideMultithreadThreshold = 256; //Create ground Body ground = World.CreateBody(); Body tumblerBody = World.CreateBody(new Vector2(0, 10)); tumblerBody.SleepingAllowed = false; tumblerBody.BodyType = BodyType.Dynamic; tumblerBody.CreateRectangle(1, 20, 5, new Vector2(10, 0)); tumblerBody.CreateRectangle(1, 20, 5, new Vector2(-10, 0)); tumblerBody.CreateRectangle(20, 1, 5, new Vector2(0, 10)); tumblerBody.CreateRectangle(20, 1, 5, new Vector2(0, -10)); RevoluteJoint joint = JointFactory.CreateRevoluteJoint(World, ground, tumblerBody, new Vector2(0, 10), Vector2.Zero); joint.ReferenceAngle = 0.0f; joint.MotorSpeed = 0.05f * MathHelper.Pi; joint.MaxMotorTorque = 1e8f; joint.MotorEnabled = true; }
private void CreatePlayerPhysicsObjects(Vector2 gameWorldPosition) { MainFixture = FixtureFactory.CreateRectangle(Engine.Physics.World, 0.5f, 0.5f, 1); MainFixture.CollisionFilter.CollisionCategories = (Category)(Global.CollisionCategories.Player); Bodies.Add(MainFixture.Body); MainFixture.Body.Position = Engine.Physics.PositionToPhysicsWorld(gameWorldPosition); MainFixture.Body.BodyType = BodyType.Dynamic; MainFixture.Body.SleepingAllowed = false; WheelFixture = FixtureFactory.CreateCircle(Engine.Physics.World, 0.3f, 1.0f); WheelFixture.CollisionFilter.CollisionCategories = (Category)(Global.CollisionCategories.Player); Bodies.Add(WheelFixture.Body); WheelFixture.Body.Position = MainFixture.Body.Position + new Vector2(0.0f, 0.6f); WheelFixture.Body.BodyType = BodyType.Dynamic; WheelFixture.Body.SleepingAllowed = false; WheelFixture.Friction = 0.5f; playerFAJ = JointFactory.CreateFixedAngleJoint(Engine.Physics.World, MainFixture.Body); playerFAJ.BodyB = WheelFixture.Body; wheelMotorRevJoint = JointFactory.CreateRevoluteJoint(MainFixture.Body, WheelFixture.Body, Vector2.Zero); wheelMotorRevJoint.MaxMotorTorque = 10.0f; wheelMotorRevJoint.MotorEnabled = true; Engine.Physics.World.AddJoint(wheelMotorRevJoint); }
public ChainBridge(Vector2 setPosition) { Body ground = new Body(PhysicsGameScreen.World); PolygonShape shape = new PolygonShape(1); shape.SetAsBox(0.9f, 0.125f); Body prevBody = ground; for (int i = 0; i < 11; ++i) { Body body = new Body(PhysicsGameScreen.World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(setPosition.X + 1.8f * i, setPosition.Y); body.IgnoreCollisionWith(GameDemo1.physicTextures[8].body); Fixture fix = body.CreateFixture(shape); fix.Friction = 0.6f; Vector2 anchor = new Vector2(-1, 0); JointFactory.CreateRevoluteJoint(PhysicsGameScreen.World, prevBody, body, anchor); body.Mass = 1f; prevBody = body; segment.Add(prevBody); } Vector2 anchor2 = new Vector2(1.0f, 0); JointFactory.CreateRevoluteJoint(PhysicsGameScreen.World, ground, prevBody, anchor2); }
public Character(SpriteBatch spriteBatch, Texture2D texture, Vector2 position, Rectangle tileSize, float rotation, World physicsWorld) : base(spriteBatch, texture, position, tileSize, rotation, physicsWorld, 1.0f) { position = ConvertUnits.ToSimUnits(position); //Create body that is half size of etire object float upperBody = ConvertUnits.ToSimUnits(tileSize.Height) - (ConvertUnits.ToSimUnits(tileSize.Width) / 2.0f); PhysicsBody = BodyFactory.CreateRectangle(physicsWorld, ConvertUnits.ToSimUnits(tileSize.Height), upperBody, 10.0f); //shift it up a tiny bit to keep the new objects center correct PhysicsBody.Position = position - Vector2.UnitY * (ConvertUnits.ToSimUnits(tileSize.Width) / 4); float centerOffset = position.Y - PhysicsBody.Position.Y; //Force the upper body to stay upright var fixedAngleJoint = JointFactory.CreateFixedAngleJoint(physicsWorld, PhysicsBody); //Create a wheel as wide as PhysicsBody WheelBody = BodyFactory.CreateCircle(physicsWorld, ConvertUnits.ToSimUnits(tileSize.Width) / 2.0f, 1.0f); //Position its center at the bottom of the upper body WheelBody.Position = PhysicsBody.Position + Vector2.UnitY * (upperBody / 2.0f); var motor = JointFactory.CreateRevoluteJoint(physicsWorld, PhysicsBody, WheelBody, WheelBody.Position); motor.MotorEnabled = true; motor.MaxMotorTorque = 1000f; motor.MotorSpeed = 2f; WheelBody.IgnoreCollisionWith(PhysicsBody); WheelBody.Friction = float.MaxValue; PhysicsBody.IgnoreCollisionWith(WheelBody); PhysicsBody.BodyType = BodyType.Dynamic; WheelBody.BodyType = BodyType.Dynamic; }
public Spider(World world, PhysicsGameScreen screen, Vector2 position) { //Load bodies _circle = BodyFactory.CreateCircle(world, SpiderBodyRadius, 0.1f, position); _circle.BodyType = BodyType.Dynamic; //Left upper leg _leftUpper = BodyFactory.CreateRectangle(world, _upperLegSize.X, _upperLegSize.Y, 0.1f, _circle.Position - new Vector2(SpiderBodyRadius, 0f) - new Vector2(_upperLegSize.X / 2f, 0f)); _leftUpper.BodyType = BodyType.Dynamic; //Left lower leg _leftLower = BodyFactory.CreateRectangle(world, _lowerLegSize.X, _lowerLegSize.Y, 0.1f, _circle.Position - new Vector2(SpiderBodyRadius, 0f) - new Vector2(_upperLegSize.X, 0f) - new Vector2(_lowerLegSize.X / 2f, 0f)); _leftLower.BodyType = BodyType.Dynamic; //Right upper leg _rightUpper = BodyFactory.CreateRectangle(world, _upperLegSize.X, _upperLegSize.Y, 0.1f, _circle.Position + new Vector2(SpiderBodyRadius, 0f) + new Vector2(_upperLegSize.X / 2f, 0f)); _rightUpper.BodyType = BodyType.Dynamic; //Right lower leg _rightLower = BodyFactory.CreateRectangle(world, _lowerLegSize.X, _lowerLegSize.Y, 0.1f, _circle.Position + new Vector2(SpiderBodyRadius, 0f) + new Vector2(_upperLegSize.X, 0f) + new Vector2(_lowerLegSize.X / 2f, 0f)); _rightLower.BodyType = BodyType.Dynamic; //Create joints JointFactory.CreateRevoluteJoint(world, _circle, _leftUpper, new Vector2(_upperLegSize.X / 2f, 0f)); _leftShoulderAngleJoint = JointFactory.CreateAngleJoint(world, _circle, _leftUpper); _leftShoulderAngleJoint.MaxImpulse = 3f; JointFactory.CreateRevoluteJoint(world, _circle, _rightUpper, new Vector2(-_upperLegSize.X / 2f, 0f)); _rightShoulderAngleJoint = JointFactory.CreateAngleJoint(world, _circle, _rightUpper); _rightShoulderAngleJoint.MaxImpulse = 3f; JointFactory.CreateRevoluteJoint(world, _leftUpper, _leftLower, new Vector2(_lowerLegSize.X / 2f, 0f)); _leftKneeAngleJoint = JointFactory.CreateAngleJoint(world, _leftUpper, _leftLower); _leftKneeAngleJoint.MaxImpulse = 3f; JointFactory.CreateRevoluteJoint(world, _rightUpper, _rightLower, new Vector2(-_lowerLegSize.X / 2f, 0f)); _rightKneeAngleJoint = JointFactory.CreateAngleJoint(world, _rightUpper, _rightLower); _rightKneeAngleJoint.MaxImpulse = 3; _screen = screen; //GFX AssetCreator creator = _screen.ScreenManager.Assets; _torso = new Sprite(creator.CircleTexture(SpiderBodyRadius, MaterialType.Waves, Color.Gray, 1f)); _upperLeg = new Sprite(creator.TextureFromShape(_leftUpper.FixtureList[0].Shape, MaterialType.Blank, Color.DimGray, 1f)); _lowerLeg = new Sprite(creator.TextureFromShape(_leftLower.FixtureList[0].Shape, MaterialType.Blank, Color.DarkSlateGray, 1f)); }
/// <summary> /// Attaches the player to the cart with a joint. /// </summary> /// <param name="cartBody">The cart body.</param> private void LandInCart(Body cartBody) { if (this.state == PlayerState.dead || this.cartCollisionDisabled) { return; } else { AudioManager.PlayEffect(this.enterCartSound); this.smallBody.LinearVelocity = cartBody.LinearVelocity; this.fullBody.LinearVelocity = cartBody.LinearVelocity; this.wheelBody.LinearVelocity = cartBody.LinearVelocity; this.jumpingDown = false; this.state = PlayerState.standing; this.InCart = true; Vector2 cartTopPosition = cartBody.Position + ConvertUnits.ToSimUnits(-24.0f * Vector2.UnitY); this.fullBody.Position = cartTopPosition + ConvertUnits.ToSimUnits(this.fullBodyOffset); this.smallBody.Position = cartTopPosition + ConvertUnits.ToSimUnits(this.smallBodyOffset); this.wheelBody.Position = cartTopPosition + ConvertUnits.ToSimUnits(this.wheelBodyOffset); if (this.cartJoint == null) { this.cartJoint = JointFactory.CreateRevoluteJoint(this.physicsWorld, this.smallBody, cartBody, Vector2.Zero); } } }
private Body AddNode(Body parent, Vector2 localAnchor, int depth, float offset, float a) { const float density = 20.0f; Vector2 h = new Vector2(0.0f, a); Vector2 p = parent.Position + localAnchor - h; Body body = BodyFactory.CreateRectangle(World, 0.5f * a, a * 2, density, p); body.UserData = _counter++; body.BodyType = BodyType.Dynamic; if (depth == Depth) { return(body); } FixtureFactory.AttachRectangle(offset * 2, 0.5f * a, density, new Vector2(0, -a), body); Vector2 a1 = new Vector2(offset, -a); Vector2 a2 = new Vector2(-offset, -a); Body body1 = AddNode(body, a1, depth + 1, 0.5f * offset, a); Body body2 = AddNode(body, a2, depth + 1, 0.5f * offset, a); JointFactory.CreateRevoluteJoint(World, body, body1, a1, h); JointFactory.CreateRevoluteJoint(World, body, body2, a2, h); return(body); }
/// <summary> /// Sets up all the physical properties of the StickMan object. /// </summary> /// <param name="physicsWorld">The physics world to set the objects up in.</param> private void SetUpPhysicsObjects(ref World physicsWorld) { float density = 1.1f; float restitution = 0.125f; // Upper body for standing this.fullBody = BodyFactory.CreateRectangle(physicsWorld, ConvertUnits.ToSimUnits(this.standingSprite.Height / 2.0f), ConvertUnits.ToSimUnits(this.standingSprite.Height * 0.75f), density, ConvertUnits.ToSimUnits(this.fullBodyOffset)); this.fullBody.BodyType = BodyType.Dynamic; this.fullBody.Restitution = restitution; this.fullBody.CollisionCategories = EntityConstants.StickManCategory; // Middle body for crouching this.smallBody = BodyFactory.CreateRectangle(physicsWorld, ConvertUnits.ToSimUnits(this.standingSprite.Height / 2.0f), ConvertUnits.ToSimUnits(this.standingSprite.Height * 0.25f), density, ConvertUnits.ToSimUnits(this.smallBodyOffset)); this.smallBody.BodyType = BodyType.Dynamic; this.smallBody.IgnoreCollisionWith(this.fullBody); this.smallBody.Restitution = restitution; this.smallBody.CollisionCategories = EntityConstants.StickManCategory; // Wheel for movement this.wheelBody = BodyFactory.CreateCircle(physicsWorld, ConvertUnits.ToSimUnits(this.standingSprite.Height / 4.0f), density, ConvertUnits.ToSimUnits(this.wheelBodyOffset)); this.wheelBody.BodyType = BodyType.Dynamic; this.wheelBody.IgnoreCollisionWith(this.smallBody); this.wheelBody.IgnoreCollisionWith(this.fullBody); this.wheelBody.Restitution = restitution; this.wheelBody.Friction = float.MaxValue; this.wheelBody.CollisionCategories = EntityConstants.StickManCategory; // Joints to connect the bodies. this.bodyJoint = JointFactory.CreateWeldJoint(physicsWorld, this.fullBody, this.smallBody, this.smallBody.Position); this.angleUprightJoint = JointFactory.CreateFixedAngleJoint(physicsWorld, this.smallBody); this.motorJoint = JointFactory.CreateRevoluteJoint(physicsWorld, this.smallBody, this.wheelBody, Vector2.Zero); this.motorJoint.MotorSpeed = 0.0f; this.motorJoint.MaxMotorTorque = 1000.0f; this.motorJoint.MotorEnabled = true; }
/// <summary> Creates a revolute joint between 2 given bodies - this joint ensures that the 2 bodies are always in the same place </summary> /// <param name="bodyA"></param> /// <param name="bodyB"></param> /// <param name="position"></param> /// <returns></returns> public RevoluteJoint CreateRevoluteJoint(Body bodyA, Body bodyB, Vector2 position) { RevoluteJoint joint = JointFactory.CreateRevoluteJoint(_World, bodyA, bodyB, ConvertUnits.ToSimUnits(position)); _JointList.Add(joint); return(joint); }
private Body AddNode(Body parent, Vector2 localAnchor, int depth, double offset, double a) { const double density = 20.0f; Vector2 h = new Vector2(0.0f, a); Vector2 p = parent.Position + localAnchor - h; Body body = BodyFactory.CreateRectangle(World, 0.5f * a, a * 2, density, p); body.BodyType = BodyType.Dynamic; if (depth == Depth) { return(body); } Vector2 a1 = new Vector2(offset, -a); Vector2 a2 = new Vector2(-offset, -a); Body body1 = AddNode(body, a1, depth + 1, 0.5f * offset, a); Body body2 = AddNode(body, a2, depth + 1, 0.5f * offset, a); JointFactory.CreateRevoluteJoint(World, body, body1, a1, h); JointFactory.CreateRevoluteJoint(World, body, body2, a2, h); return(body); }
public HumanoidBody(World world, Vector2 dimensions, object userData) { _torsoHeight = dimensions.Y - dimensions.X / 2f; Torso = BodyFactory.CreateRectangle( world: world, width: dimensions.X, height: _torsoHeight, density: 1f, position: Vector2.UnitX * 20, userData: userData); Torso.BodyType = BodyType.Dynamic; Wheel = BodyFactory.CreateCircle( world: world, radius: dimensions.X / 2f, density: 1f, position: Torso.Position + new Vector2(0, _torsoHeight / 2), userData: userData); Wheel.BodyType = BodyType.Dynamic; Wheel.Friction = float.MaxValue; Wheel.Restitution = float.MinValue; JointFactory.CreateFixedAngleJoint(world, Torso); Motor = JointFactory.CreateRevoluteJoint(world, Torso, Wheel, Vector2.Zero); Motor.MotorEnabled = true; Motor.MaxMotorTorque = 10; }
public void AddRope(Rope rope) { float ropeLength = (rope.AttachedEntity.Position - rope.AttachedPosition).Length(); int segmentCount = (int)Round(1 + (ropeLength / Rope.SegmentLength)); if (segmentCount < 2) { segmentCount = 2; } var ropeSegmentDir = Vector2.Normalize(rope.AttachedEntity.Position - rope.AttachedPosition); // TODO: fix the RotationFromDirection function! var ropeRotation = ropeSegmentDir.RotationFromDirection(); rope.PhysicsSegments.Clear(); for (int i = 0; i < segmentCount; i++) { var ropeSegmentLength = i > 0 ? Rope.SegmentLength : Rope.Diameter; var ropeSegmentVector = ropeSegmentDir * ropeSegmentLength; var segmentStart = rope.AttachedPosition + ropeSegmentVector * i; var segmentCenter = rope.AttachedPosition + ropeSegmentVector * (i + 0.5f); var boxWidth = Rope.Diameter; var boxHeight = i > 0 ? Rope.SegmentLength : Rope.Diameter; //var segment = BodyFactory.CreateCircle(PhysicsWorld, Rope.JointRadius, Rope.SegmentDensity);// BodyFactory.CreateRectangle(PhysicsWorld, 0.05f, Rope.SegmentLength, 0.5f); var segment = BodyFactory.CreateRectangle(PhysicsWorld, boxWidth, boxHeight, Rope.SegmentDensity); segment.BodyType = BodyType.Dynamic; segment.Friction = 0.5f; segment.Restitution = 0.2f; segment.AngularDamping = 0.2f; //1.1f; segment.LinearDamping = 0.2f; // 1.1f; segment.CollisionCategories = Category.Cat3; segment.SetTransform(segmentCenter, -ropeRotation - 0.5f * (float)Math.PI); rope.PhysicsSegments.Add(segment); if (i > 0) { var segmentJoint = JointFactory.CreateRevoluteJoint(PhysicsWorld, rope.PhysicsSegments[i - 1], segment, new Vector2(0, i == 1 ? Rope.Diameter / 2 : Rope.SegmentLength / 2), new Vector2(0, -Rope.SegmentLength / 2)); segmentJoint.CollideConnected = false; rope.PhysicsSegmentJoints.Add(segmentJoint); } else { segment.BodyType = BodyType.Static; } } //var ballAnchor = rope.AttachedEntity.Position + new Vector2(0, rope.AttachedEntity.Radius + 1f); var ballJoint = JointFactory.CreateRevoluteJoint(PhysicsWorld, rope.PhysicsSegments.Last(), rope.AttachedEntity.PhysicsBody, new Vector2(0, Rope.SegmentLength / 2), Vector2.Zero); ballJoint.CollideConnected = false; rope.PhysicsEntityJoint = ballJoint; rope.AttachedEntity.PhysicsBody.FixedRotation = false; //rope.AttachedEntity.PhysicsBody.Mass = 2f; }
public override PhysicsJoint CreateRevoluteJoint(PhysicsBody bodyA, PhysicsBody bodyB, Vector2D anchor) { RevoluteJoint farseerJoint = JointFactory.CreateRevoluteJoint(world, ((FarseerBody)bodyA).Body, ((FarseerBody)bodyB).Body, unitConverter.Convert(anchor)); return(new FarseerJoint(farseerJoint, bodyA, bodyB)); }
public CompositePhysicsObject(World world, PhysicsObject physObA, PhysicsObject physObB, Vector2 relativeJointPosition) { this.physObA = physObA; this.physObB = physObB; revJoint = JointFactory.CreateRevoluteJoint(world, physObA.fixture.Body, physObB.fixture.Body, ConvertUnits.ToSimUnits(relativeJointPosition)); physObA.fixture.IgnoreCollisionWith(physObB.fixture); physObB.fixture.IgnoreCollisionWith(physObA.fixture); }
private Vector2 _upperLegSize = new Vector2(3, 0.5f); //x=width, y=height public Spider(World world, Vector2 position) { //Load bodies Body circle = BodyFactory.CreateCircle(world, SpiderBodyRadius, 0.1f, position); circle.BodyType = BodyType.Dynamic; //Left upper leg Body leftUpper = BodyFactory.CreateRectangle(world, _upperLegSize.X, _upperLegSize.Y, 0.1f, circle.Position - new Vector2(SpiderBodyRadius, 0) - new Vector2(_upperLegSize.X / 2, 0)); leftUpper.BodyType = BodyType.Dynamic; //Left lower leg Body leftLower = BodyFactory.CreateRectangle(world, _lowerLegSize.X, _lowerLegSize.Y, 0.1f, circle.Position - new Vector2(SpiderBodyRadius, 0) - new Vector2(_upperLegSize.X, 0) - new Vector2(_lowerLegSize.X / 2, 0)); leftLower.BodyType = BodyType.Dynamic; //Right upper leg Body rightUpper = BodyFactory.CreateRectangle(world, _upperLegSize.X, _upperLegSize.Y, 0.1f, circle.Position + new Vector2(SpiderBodyRadius, 0) + new Vector2(_upperLegSize.X / 2, 0)); rightUpper.BodyType = BodyType.Dynamic; //Right lower leg Body rightLower = BodyFactory.CreateRectangle(world, _lowerLegSize.X, _lowerLegSize.Y, 0.1f, circle.Position + new Vector2(SpiderBodyRadius, 0) + new Vector2(_upperLegSize.X, 0) + new Vector2(_lowerLegSize.X / 2, 0)); rightLower.BodyType = BodyType.Dynamic; //Create joints JointFactory.CreateRevoluteJoint(world, circle, leftUpper, new Vector2(SpiderBodyRadius, 0)); _leftShoulderAngleJoint = JointFactory.CreateAngleJoint(world, circle, leftUpper); _leftShoulderAngleJoint.MaxImpulse = 3; JointFactory.CreateRevoluteJoint(world, circle, rightUpper, new Vector2(-SpiderBodyRadius, 0)); _rightShoulderAngleJoint = JointFactory.CreateAngleJoint(world, circle, rightUpper); _rightShoulderAngleJoint.MaxImpulse = 3; JointFactory.CreateRevoluteJoint(world, leftUpper, leftLower, new Vector2(_upperLegSize.X / 2, 0)); _leftKneeAngleJoint = JointFactory.CreateAngleJoint(world, leftUpper, leftLower); _leftKneeAngleJoint.MaxImpulse = 3; JointFactory.CreateRevoluteJoint(world, rightUpper, rightLower, -new Vector2(_upperLegSize.X / 2, 0)); _rightKneeAngleJoint = JointFactory.CreateAngleJoint(world, rightUpper, rightLower); _rightKneeAngleJoint.MaxImpulse = 3; }
public RevoluteWheel(World world, Car car, Vector2 position, float orientation) : base(world, position, orientation) { Joint = JointFactory.CreateRevoluteJoint(world, car.Body, Body, Vector2.Zero); Joint.MotorEnabled = true; Joint.MaxMotorTorque = 100; Joint.LimitEnabled = true; Joint.LowerLimit = -Car.MAX_WHEEL_DEFLECTION; Joint.UpperLimit = Car.MAX_WHEEL_DEFLECTION; }
public override void LoadContent() { base.LoadContent(); World.Gravity = new Vector2(0, -9.82f); _border = new Border(World, ScreenManager, Camera); /* Bridge */ //We make a path using 2 points. Path bridgePath = new Path(); bridgePath.Add(new Vector2(-15, -5)); bridgePath.Add(new Vector2(15, -5)); bridgePath.Closed = false; Vertices box = PolygonTools.CreateRectangle(0.25f / 2f, 1f / 2f); PolygonShape shape = new PolygonShape(box, 20); _bridgeBodies = PathManager.EvenlyDistributeShapesAlongPath(World, bridgePath, shape, BodyType.Dynamic, 29); _bridgeBox = new Sprite(ScreenManager.Assets.TextureFromShape(shape, MaterialType.Dots, Color.SandyBrown, 1f)); //Attach the first and last fixtures to the world JointFactory.CreateRevoluteJoint(World, HiddenBody, _bridgeBodies[0], new Vector2(0f, 0.5f)); JointFactory.CreateRevoluteJoint(World, HiddenBody, _bridgeBodies[_bridgeBodies.Count - 1], new Vector2(0, -0.5f)); PathManager.AttachBodiesWithRevoluteJoint(World, _bridgeBodies, new Vector2(0f, 0.5f), new Vector2(0f, -0.5f), false, true); /* Soft body */ //We make a rectangular path. Path rectanglePath = new Path(); rectanglePath.Add(new Vector2(-6, 11)); rectanglePath.Add(new Vector2(-6, -1)); rectanglePath.Add(new Vector2(6, -1)); rectanglePath.Add(new Vector2(6, 11)); rectanglePath.Closed = true; //Creating two shapes. A circle to form the circle and a rectangle to stabilize the soft body. List <Shape> shapes = new List <Shape>(2); shapes.Add(new PolygonShape(PolygonTools.CreateRectangle(1f / 2f, 1f / 2f, new Vector2(-0.1f, 0f), 0f), 1f)); shapes.Add(new CircleShape(0.5f, 1f)); //We distribute the shapes in the rectangular path. _softBodies = PathManager.EvenlyDistributeShapesAlongPath(World, rectanglePath, shapes, BodyType.Dynamic, 30); _softBodyBox = new Sprite(ScreenManager.Assets.TextureFromShape(shapes[0], MaterialType.Blank, Color.Silver * 0.8f, 1f)); _softBodyBox.Origin += new Vector2(2.4f, 0f); _softBodyCircle = new Sprite(ScreenManager.Assets.TextureFromShape(shapes[1], MaterialType.Waves, Color.Silver, 1f)); //Attach the bodies together with revolute joints. The rectangular form will converge to a circular form. PathManager.AttachBodiesWithRevoluteJoint(World, _softBodies, new Vector2(0f, 0.5f), new Vector2(0f, -0.5f), true, true); }
public Barrier(Texture2D texture, Unit start, Unit end, World world, Game game, Color color) : base(texture, Vector2.Zero, DrawingHelper.DrawingLevel.Low, game, world) { Color = color; EffectParticles = new ParticleEngine(30, texture, Vector2.Zero, color, Vector2.Zero, 1, 0, 10, DrawingHelper.DrawingLevel.Top, game); SyncedGameCollection.ComponentCollection.Add(EffectParticles); // Units to follow _start = start; _end = end; // create two static hiddenBodies that mirror the position of the units hiddenBody1 = BodyFactory.CreateCircle(world, texture.Width / 2, 1, _start.RigidBody.Position); //_start.RigidBody; hiddenBody2 = BodyFactory.CreateCircle(world, texture.Width / 2, 1, _end.RigidBody.Position); //_end.RigidBody; hiddenBody1.CollisionCategories = Category.None; hiddenBody2.CollisionCategories = Category.None; hiddenBody1.CollidesWith = Category.None; hiddenBody2.CollidesWith = Category.None; hiddenBody1.BodyType = BodyType.Static; hiddenBody2.BodyType = BodyType.Static; hiddenBody1.Position = _start.RigidBody.Position; hiddenBody2.Position = _end.RigidBody.Position; hiddenBody1.UserData = hiddenBody2.UserData = ""; // create path object and set it to the init position. Path barrierPath = new Path(); barrierPath.Add(start.RigidBody.Position); barrierPath.Add(end.RigidBody.Position); barrierPath.Closed = false; // create barrier particle Vertices barrierParticle = PolygonTools.CreateCircle(ConvertUnits.ToSimUnits(texture.Width * 4), 8); Shape shape = new PolygonShape(barrierParticle, 0f); // distribute barrierParticle positions along the path between the two units. _barrierBodies = PathManager.EvenlyDistributeShapesAlongPath(world, barrierPath, shape, BodyType.Dynamic, 30); // fix the shapes together with the end and start point JointFactory.CreateRevoluteJoint(world, hiddenBody1, _barrierBodies[0], new Vector2(0f, 0f)); JointFactory.CreateRevoluteJoint(world, hiddenBody2, _barrierBodies[_barrierBodies.Count - 1], new Vector2(0f, 0f)); // fix all the barrierParticles together in the path PathManager.AttachBodiesWithRevoluteJoint(world, _barrierBodies, new Vector2(0f, 0.5f), new Vector2(0f, 0.5f), false, false); // Set up OnCollision fale safe. for (int i = 0; i < _barrierBodies.Count; i++) { _barrierBodies[i].CollisionCategories = Category.Cat10; _barrierBodies[i].CollidesWith = Category.Cat5; /* should only collide with grabbables*/ _barrierBodies[i].UserData = "BARRIER"; _barrierBodies[i].OnCollision += OnCollision; } Deactivated(); }
public void ClipToLast(Body lastBody, World world) { PolygonShape shape = new PolygonShape(1f); shape.SetAsBox(ConvertUnits.ToSimUnits(8f), ConvertUnits.ToSimUnits(8f)); Fixture fix = this.body.CreateFixture(shape); fix.Friction = 0.6f; JointFactory.CreateRevoluteJoint(world, this.body, lastBody, Vector2.Zero); }
private MobileBalancedTest() { Body ground = BodyFactory.CreateBody(World, new Vector2(0, 20f)); const float a = 0.5f; Vector2 h = new Vector2(0.0f, a); Body root = AddNode(ground, Vector2.Zero, 0, 3.0f, a); JointFactory.CreateRevoluteJoint(World, ground, root, Vector2.Zero, h); }
public MobileTest() { Body ground = BodyFactory.CreateBody(World, new Vector2(0, 20f)); const double a = 0.5f; Vector2 h = new Vector2(0.0f, a); Body root = AddNode(ground, Vector2.Zero, 0, 3.0f, a); JointFactory.CreateRevoluteJoint(World, ground, root, Vector2.Zero, h); }
public JumpySpider(World world, Vector2 position) { // Body _circle = BodyFactory.CreateCircle(world, SpiderBodyRadius, 0.1f, position); _circle.BodyType = BodyType.Dynamic; // Left upper leg _leftUpper = BodyFactory.CreateRectangle(world, _upperLegSize.X, _upperLegSize.Y, 0.1f, _circle.Position - new Vector2(SpiderBodyRadius, 0f) - new Vector2(_upperLegSize.X / 2f, 0f)); _leftUpper.BodyType = BodyType.Dynamic; // Left lower leg _leftLower = BodyFactory.CreateRectangle(world, _lowerLegSize.X, _lowerLegSize.Y, 0.1f, _circle.Position - new Vector2(SpiderBodyRadius, 0f) - new Vector2(_upperLegSize.X, 0f) - new Vector2(_lowerLegSize.X / 2f, 0f)); _leftLower.BodyType = BodyType.Dynamic; // Right upper leg _rightUpper = BodyFactory.CreateRectangle(world, _upperLegSize.X, _upperLegSize.Y, 0.1f, _circle.Position + new Vector2(SpiderBodyRadius, 0f) + new Vector2(_upperLegSize.X / 2f, 0f)); _rightUpper.BodyType = BodyType.Dynamic; // Right lower leg _rightLower = BodyFactory.CreateRectangle(world, _lowerLegSize.X, _lowerLegSize.Y, 0.1f, _circle.Position + new Vector2(SpiderBodyRadius, 0f) + new Vector2(_upperLegSize.X, 0f) + new Vector2(_lowerLegSize.X / 2f, 0f)); _rightLower.BodyType = BodyType.Dynamic; //Create joints JointFactory.CreateRevoluteJoint(world, _circle, _leftUpper, new Vector2(_upperLegSize.X / 2f, 0f)); _leftShoulderAngleJoint = JointFactory.CreateAngleJoint(world, _circle, _leftUpper); _leftShoulderAngleJoint.MaxImpulse = 3f; JointFactory.CreateRevoluteJoint(world, _circle, _rightUpper, new Vector2(-_upperLegSize.X / 2f, 0f)); _rightShoulderAngleJoint = JointFactory.CreateAngleJoint(world, _circle, _rightUpper); _rightShoulderAngleJoint.MaxImpulse = 3f; JointFactory.CreateRevoluteJoint(world, _leftUpper, _leftLower, new Vector2(_lowerLegSize.X / 2f, 0f)); _leftKneeAngleJoint = JointFactory.CreateAngleJoint(world, _leftUpper, _leftLower); _leftKneeAngleJoint.MaxImpulse = 3f; JointFactory.CreateRevoluteJoint(world, _rightUpper, _rightLower, new Vector2(-_lowerLegSize.X / 2f, 0f)); _rightKneeAngleJoint = JointFactory.CreateAngleJoint(world, _rightUpper, _rightLower); _rightKneeAngleJoint.MaxImpulse = 3; //GFX _torso = new Sprite(ContentWrapper.CircleTexture(SpiderBodyRadius, "Square", ContentWrapper.Grey, ContentWrapper.Gold, ContentWrapper.Black, 1f)); _upperLeg = new Sprite(ContentWrapper.TextureFromShape(_leftUpper.FixtureList[0].Shape, ContentWrapper.Grey, ContentWrapper.Black)); _lowerLeg = new Sprite(ContentWrapper.TextureFromShape(_leftLower.FixtureList[0].Shape, ContentWrapper.Gold, ContentWrapper.Black)); _flexed = false; _timer = 0f; _leftShoulderAngleJoint.TargetAngle = ShoulderRelaxed; _leftKneeAngleJoint.TargetAngle = KneeRelaxed; _rightShoulderAngleJoint.TargetAngle = -ShoulderRelaxed; _rightKneeAngleJoint.TargetAngle = -KneeRelaxed; }
private static RevoluteJoint CreateJoint(Body carBody, Body wheelBody, Vector2 anchor, World world) { var joint = JointFactory.CreateRevoluteJoint(carBody, wheelBody, Vector2.Zero); joint.LocalAnchorA = anchor; joint.LimitEnabled = true; joint.LowerLimit = 0; joint.UpperLimit = 0; world.AddJoint(joint); wheelBody.Position = carBody.Position + anchor; return(joint); }
public PinObject(World world, Vector2 position) { // This will be used to create fasters between the ribbon and the background Body roller = BodyFactory.CreateCircle(world, .1f, 1, this); Body roller2 = BodyFactory.CreateCircle(world, .25f, 1, this); roller.BodyType = BodyType.Static; roller2.BodyType = BodyType.Dynamic; roller.Position = position; roller2.Position = position; Joint J = JointFactory.CreateRevoluteJoint(world, roller, roller2, new Vector2()); }
public static PayloadChain Connect(SpaceShip spaceShip, Payload payload, Vector2 payloadTarget) { var dynamicWorld = (DynamicEntityWorld)spaceShip.World; var world = dynamicWorld.PhysicsWorld; AABB spaceShipSize; spaceShip.Body.FixtureList.First().GetAABB(out spaceShipSize, 0); AABB payloadSize; payload.Body.FixtureList.First().GetAABB(out payloadSize, 0); var chainSize = new Vector2(1.0f); var chainSizeSingle = Math.Max(chainSize.X, chainSize.Y); var start = new Vector2(spaceShip.Position.X - spaceShipSize.Extents.Y, spaceShip.Position.Y); var length = payloadTarget.Length(); var targetVector = payloadTarget / length; var chainVector = targetVector * chainSizeSingle; start += chainVector; var chainCount = (int)Math.Ceiling(length / chainSizeSingle); var lastBody = spaceShip.Body; var chain = new PayloadChain(spaceShip.Name + "_Chain"); var elements = new List <ChainElement>(); for (var i = 0; i < chainCount; i++) { var chainBody = BodyFactory.CreateBody(world, start + i * chainVector); chainBody.BodyType = BodyType.Dynamic; var chainFixture = FixtureFactory.AttachRectangle(chainSize.X, chainSize.Y, 0.1f, Vector2.Zero, chainBody); chainFixture.CollidesWith = Category.Cat2; JointFactory.CreateRevoluteJoint(world, lastBody, chainBody, -chainVector / 2.0f); lastBody = chainBody; elements.Add(ChainElement.CreateFor(chain, elements.Count, dynamicWorld, chainBody)); } payload.Position = new Vector3(start + chainCount * chainVector + targetVector * 2.5f, 0.0f); JointFactory.CreateRevoluteJoint(world, lastBody, payload.Body, new Vector2(0.0f, -2.5f)); var ropeJoin = new RopeJoint(spaceShip.Body, payload.Body, new Vector2(0.0f, 3.0f), new Vector2(0.0f, -2.5f)); ropeJoin.CollideConnected = true; world.AddJoint(ropeJoin); return(chain); }
/// <summary> /// Connects the figure's body parts /// </summary> /// <param name="world">The physics world to add the joints to</param> private void ConnectBody(World world) { upright = JointFactory.CreateAngleJoint(world, torso, gyro); upright.MaxImpulse = maxImpulse; upright.TargetAngle = 0.0f; upright.CollideConnected = false; r_neck = JointFactory.CreateRevoluteJoint(world, head, torso, -Vector2.UnitY * 20 * MainGame.PIXEL_TO_METER); neck = JointFactory.CreateAngleJoint(world, head, torso); neck.CollideConnected = false; neck.MaxImpulse = maxImpulse; r_leftShoulder = JointFactory.CreateRevoluteJoint(world, leftUpperArm, torso, -Vector2.UnitY * 15 * MainGame.PIXEL_TO_METER); leftShoulder = JointFactory.CreateAngleJoint(world, leftUpperArm, torso); leftShoulder.CollideConnected = false; leftShoulder.MaxImpulse = maxImpulse; r_rightShoulder = JointFactory.CreateRevoluteJoint(world, rightUpperArm, torso, -Vector2.UnitY * 15 * MainGame.PIXEL_TO_METER); rightShoulder = JointFactory.CreateAngleJoint(world, rightUpperArm, torso); rightShoulder.CollideConnected = false; rightShoulder.MaxImpulse = maxImpulse; r_leftElbow = JointFactory.CreateRevoluteJoint(world, leftLowerArm, leftUpperArm, -Vector2.UnitY * 7.5f * MainGame.PIXEL_TO_METER); leftElbow = JointFactory.CreateAngleJoint(world, leftLowerArm, leftUpperArm); leftElbow.CollideConnected = false; leftElbow.MaxImpulse = maxImpulse; r_rightElbow = JointFactory.CreateRevoluteJoint(world, rightLowerArm, rightUpperArm, -Vector2.UnitY * 7.5f * MainGame.PIXEL_TO_METER); rightElbow = JointFactory.CreateAngleJoint(world, rightLowerArm, rightUpperArm); rightElbow.CollideConnected = false; rightElbow.MaxImpulse = maxImpulse; r_leftHip = JointFactory.CreateRevoluteJoint(world, leftUpperLeg, torso, Vector2.UnitY * 15 * MainGame.PIXEL_TO_METER); leftHip = JointFactory.CreateAngleJoint(world, leftUpperLeg, torso); leftHip.CollideConnected = false; leftHip.MaxImpulse = maxImpulse; r_rightHip = JointFactory.CreateRevoluteJoint(world, rightUpperLeg, torso, Vector2.UnitY * 15 * MainGame.PIXEL_TO_METER); rightHip = JointFactory.CreateAngleJoint(world, rightUpperLeg, torso); rightHip.CollideConnected = false; rightHip.MaxImpulse = maxImpulse; r_leftKnee = JointFactory.CreateRevoluteJoint(world, leftLowerLeg, leftUpperLeg, -Vector2.UnitY * 7.5f * MainGame.PIXEL_TO_METER); leftKnee = JointFactory.CreateAngleJoint(world, leftUpperLeg, leftLowerLeg); leftKnee.CollideConnected = false; leftKnee.MaxImpulse = maxImpulse; r_rightKnee = JointFactory.CreateRevoluteJoint(world, rightLowerLeg, rightUpperLeg, -Vector2.UnitY * 7.5f * MainGame.PIXEL_TO_METER); rightKnee = JointFactory.CreateAngleJoint(world, rightUpperLeg, rightLowerLeg); rightKnee.CollideConnected = false; rightKnee.MaxImpulse = maxImpulse; }
public void grabRope(RopeGrabComponent ropeGrabComponent, Body bodyToAttach) { int index = (int)Math.Floor(ropeGrabComponent.distance); float fraction = ropeGrabComponent.distance - (float)index; RopeNode node = ropeGrabComponent.ropeNode.getByIndex(index); float lengthPosition = -(node.halfLength * 2 * fraction - node.halfLength); RevoluteJoint joint; ropeGrabComponent.ropeNode = node; bodyToAttach.Position = node.body.GetWorldPoint(new Vector2(lengthPosition, 0)); joint = JointFactory.CreateRevoluteJoint(bodyToAttach.World, bodyToAttach, node.body, node.body.GetLocalPoint(bodyToAttach.Position)); ropeGrabComponent.joints.Add(bodyToAttach, joint); }
public JumpySpider(World world, Vector2 position) { flexTime = (float)(random.NextDouble() * 5000 + 2000); relaxTime = (float)(random.NextDouble() * 5000 + 2000); // Body _circle = BodyFactory.CreateCircle(world, SpiderBodyRadius, 0.1f, position); _circle.BodyType = BodyType.Dynamic; // Left upper leg _leftUpper = BodyFactory.CreateRectangle(world, _upperLegSize.X, _upperLegSize.Y, 0.1f, _circle.Position - new Vector2(SpiderBodyRadius, 0f) - new Vector2(_upperLegSize.X / 2f, 0f)); _leftUpper.BodyType = BodyType.Dynamic; // Left lower leg _leftLower = BodyFactory.CreateRectangle(world, _lowerLegSize.X, _lowerLegSize.Y, 0.1f, _circle.Position - new Vector2(SpiderBodyRadius, 0f) - new Vector2(_upperLegSize.X, 0f) - new Vector2(_lowerLegSize.X / 2f, 0f)); _leftLower.BodyType = BodyType.Dynamic; // Right upper leg _rightUpper = BodyFactory.CreateRectangle(world, _upperLegSize.X, _upperLegSize.Y, 0.1f, _circle.Position + new Vector2(SpiderBodyRadius, 0f) + new Vector2(_upperLegSize.X / 2f, 0f)); _rightUpper.BodyType = BodyType.Dynamic; // Right lower leg _rightLower = BodyFactory.CreateRectangle(world, _lowerLegSize.X, _lowerLegSize.Y, 0.1f, _circle.Position + new Vector2(SpiderBodyRadius, 0f) + new Vector2(_upperLegSize.X, 0f) + new Vector2(_lowerLegSize.X / 2f, 0f)); _rightLower.BodyType = BodyType.Dynamic; //Create joints JointFactory.CreateRevoluteJoint(world, _circle, _leftUpper, new Vector2(_upperLegSize.X / 2f, 0f)); _leftShoulderAngleJoint = JointFactory.CreateAngleJoint(world, _circle, _leftUpper); _leftShoulderAngleJoint.MaxImpulse = 3f; JointFactory.CreateRevoluteJoint(world, _circle, _rightUpper, new Vector2(-_upperLegSize.X / 2f, 0f)); _rightShoulderAngleJoint = JointFactory.CreateAngleJoint(world, _circle, _rightUpper); _rightShoulderAngleJoint.MaxImpulse = 3f; JointFactory.CreateRevoluteJoint(world, _leftUpper, _leftLower, new Vector2(_lowerLegSize.X / 2f, 0f)); _leftKneeAngleJoint = JointFactory.CreateAngleJoint(world, _leftUpper, _leftLower); _leftKneeAngleJoint.MaxImpulse = 3f; JointFactory.CreateRevoluteJoint(world, _rightUpper, _rightLower, new Vector2(-_lowerLegSize.X / 2f, 0f)); _rightKneeAngleJoint = JointFactory.CreateAngleJoint(world, _rightUpper, _rightLower); _rightKneeAngleJoint.MaxImpulse = 3; _flexed = false; _timer = 0f; _leftShoulderAngleJoint.TargetAngle = ShoulderRelaxed; _leftKneeAngleJoint.TargetAngle = KneeRelaxed; _rightShoulderAngleJoint.TargetAngle = -ShoulderRelaxed; _rightKneeAngleJoint.TargetAngle = -KneeRelaxed; }