public override void Initialize() { if (_world != null && Texture != null) { var data = new uint[Texture.Width * Texture.Height]; Texture.GetData(data); var verts = PolygonTools.CreatePolygon(data, Texture.Width, true); var scale = ConvertUnits.ToSimUnits(new Vector2(1, 1)); verts.Scale(ref scale); var list = BayazitDecomposer.ConvexPartition(verts); var compound = BodyFactory.CreateCompoundPolygon(_world, list, 1); compound.BodyType = BodyType.Dynamic; Body = compound; Body.Restitution = 1; SetStartPosition(); var joint = new FixedPrismaticJoint(Body, Body.Position, new Vector2(1, 0)); joint.LimitEnabled = true; joint.LowerLimit = -ConvertUnits.ToSimUnits((_screenBounds.Width - Texture.Width) / 2f); joint.UpperLimit = ConvertUnits.ToSimUnits((_screenBounds.Width - Texture.Width) / 2f); joint.Enabled = true; joint.MaxMotorForce = 70f; _world.AddJoint(joint); } base.Initialize(); }
public static FixedPrismaticJoint CreateFixedPrismaticJoint(World world, Body body, Vector2 worldAnchor, Vector2 axis) { FixedPrismaticJoint joint = new FixedPrismaticJoint(body, worldAnchor, axis); world.AddJoint(joint); return joint; }
public Paddle(Texture2D texture, Vector2 position, World world, DebugViewXNA debugViewXNA, ContentManager content, GraphicsDeviceManager graphics) : base(world, debugViewXNA, content, graphics) { this.mTexture = texture; this.mWidth = ConvertUnits.ToSimUnits(this.mTexture.Width); this.mHeight = ConvertUnits.ToSimUnits(this.mTexture.Height); this.mOrigin = new Vector2(this.mWidth / 2f, this.mHeight / 2f); this.mSpriteEffects = SpriteEffects.None; this.mPosition = ConvertUnits.ToSimUnits(position); this.mBody = BodyFactory.CreateRectangle(this.mWorld, this.mWidth, this.mHeight, 1f, this.mPosition); this.mBody.BodyType = BodyType.Dynamic; this.mJointWorldAnchor = this.mPosition; this.mFixedPrismJoint = new FixedPrismaticJoint(this.mBody, this.mJointWorldAnchor, this.mJointAxis); this.mFixedPrismJoint.LimitEnabled = true; this.mFixedPrismJoint.LowerLimit = this.mJointLowerLimit; this.mFixedPrismJoint.UpperLimit = this.mJointUpperLimit; this.mFixedPrismJoint.Enabled = true; this.mWorld.AddJoint(this.mFixedPrismJoint); this.mBody.OnCollision += new OnCollisionEventHandler(OnCollision); this.mBody.OnSeparation += new OnSeparationEventHandler(OnSeparation); this.mBody.FixtureList[0].AfterCollision += new AfterCollisionEventHandler(OnAfterCollision); this.mParticleRenderer = new SpriteBatchRenderer { GraphicsDeviceService = this.mGraphics }; this.mParticleEffect = new ParticleEffect(); this.mParticleRenderer.LoadContent(this.mContent); this.mParticleEffect = this.mContent.Load<ParticleEffect>(@"Effects\basicSparks"); this.mParticleEffect.LoadContent(this.mContent); this.mParticleEffect.Initialise(); }
private PrismaticTest() { Body ground; { ground = BodyFactory.CreateBody(World); PolygonShape shape3 = new PolygonShape(0); shape3.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.CreateFixture(shape3); } PolygonShape shape = new PolygonShape(5); shape.SetAsBox(2.0f, 0.5f); Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(0.0f, 10.0f); body.CreateFixture(shape); _fixedJoint = new FixedPrismaticJoint(body, body.Position, new Vector2(0.5f, 1.0f)); _fixedJoint.MotorSpeed = 5.0f; _fixedJoint.MaxMotorForce = 1000.0f; _fixedJoint.MotorEnabled = true; _fixedJoint.LowerLimit = -10.0f; _fixedJoint.UpperLimit = 20.0f; _fixedJoint.LimitEnabled = true; World.AddJoint(_fixedJoint); PolygonShape shape2 = new PolygonShape(5); shape2.SetAsBox(2.0f, 0.5f); Body body2 = BodyFactory.CreateBody(World); body2.BodyType = BodyType.Dynamic; body2.Position = new Vector2(10.0f, 10.0f); body2.CreateFixture(shape2); _joint = new PrismaticJoint(ground, body2, ground.GetLocalPoint(body2.Position), Vector2.Zero, new Vector2(0.5f, 1.0f)); _joint.MotorSpeed = 5.0f; _joint.MaxMotorForce = 1000.0f; _joint.MotorEnabled = true; _joint.LowerLimit = -10.0f; _joint.UpperLimit = 20.0f; _joint.LimitEnabled = true; World.AddJoint(_joint); }
private GearsTest() { { // First circle CircleShape circle1 = new CircleShape(1.0f, 5); Body body1 = BodyFactory.CreateBody(World); body1.BodyType = BodyType.Dynamic; body1.Position = new Vector2(-3.0f, 12.0f); body1.CreateFixture(circle1); // Second circle CircleShape circle2 = new CircleShape(2.0f, 5); Body body2 = BodyFactory.CreateBody(World); body2.BodyType = BodyType.Dynamic; body2.Position = new Vector2(0.0f, 12.0f); body2.CreateFixture(circle2); // Rectangle Vertices box = PolygonTools.CreateRectangle(0.5f, 5.0f); PolygonShape polygonBox = new PolygonShape(box, 5); Body body3 = BodyFactory.CreateBody(World); body3.BodyType = BodyType.Dynamic; body3.Position = new Vector2(2.5f, 12.0f); body3.CreateFixture(polygonBox); // Fix first circle _joint1 = new FixedRevoluteJoint(body1, Vector2.Zero, body1.Position); World.AddJoint(_joint1); // Fix second circle _joint2 = new FixedRevoluteJoint(body2, Vector2.Zero, body2.Position); World.AddJoint(_joint2); // Fix rectangle _joint3 = new FixedPrismaticJoint(body3, body3.Position, new Vector2(0.0f, 1.0f)); _joint3.LowerLimit = -5.0f; _joint3.UpperLimit = 5.0f; _joint3.LimitEnabled = true; World.AddJoint(_joint3); // Attach first and second circle together with a gear joint _joint4 = new GearJoint(_joint1, _joint2, circle2.Radius / circle1.Radius); World.AddJoint(_joint4); // Attach second and rectangle together with a gear joint _joint5 = new GearJoint(_joint2, _joint3, -1.0f / circle2.Radius); World.AddJoint(_joint5); } }
private BodyTypesTest() { //Ground BodyFactory.CreateEdge(World, new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); // Define attachment { _attachment = BodyFactory.CreateBody(World); _attachment.BodyType = BodyType.Dynamic; _attachment.Position = new Vector2(0.0f, 3.0f); Vertices box = PolygonTools.CreateRectangle(0.5f, 2.0f); PolygonShape shape = new PolygonShape(box, 2); _attachment.CreateFixture(shape); } // Define platform { _platform = BodyFactory.CreateBody(World); _platform.BodyType = BodyType.Dynamic; _platform.Position = new Vector2(0.0f, 5.0f); Vertices box = PolygonTools.CreateRectangle(4.0f, 0.5f); PolygonShape shape = new PolygonShape(box, 2); Fixture fixture = _platform.CreateFixture(shape); fixture.Friction = 0.6f; RevoluteJoint rjd = new RevoluteJoint(_attachment, _platform, _attachment.GetLocalPoint(_platform.Position), Vector2.Zero); rjd.MaxMotorTorque = 50.0f; rjd.MotorEnabled = true; World.AddJoint(rjd); FixedPrismaticJoint pjd = new FixedPrismaticJoint(_platform, new Vector2(0.0f, 5.0f), new Vector2(1.0f, 0.0f)); pjd.MaxMotorForce = 1000.0f; pjd.MotorEnabled = true; pjd.LowerLimit = -10.0f; pjd.UpperLimit = 10.0f; pjd.LimitEnabled = true; World.AddJoint(pjd); _speed = 3.0f; } // Create a payload { Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(0.0f, 8.0f); Vertices box = PolygonTools.CreateRectangle(0.75f, 0.75f); PolygonShape shape = new PolygonShape(box, 2); Fixture fixture = body.CreateFixture(shape); fixture.Friction = 0.6f; } }
/// <summary> /// Requires two existing revolute or prismatic joints (any combination will work). /// The provided joints must attach a dynamic body to a static body. /// </summary> /// <param name="jointA">The first joint.</param> /// <param name="jointB">The second joint.</param> /// <param name="ratio">The ratio.</param> public GearJoint(Joint jointA, Joint jointB, float ratio) : base(jointA.BodyA, jointA.BodyB) { JointType = JointType.Gear; JointA = jointA; JointB = jointB; Ratio = ratio; JointType type1 = jointA.JointType; JointType type2 = jointB.JointType; // Make sure its the right kind of joint Debug.Assert(type1 == JointType.Revolute || type1 == JointType.Prismatic || type1 == JointType.FixedRevolute || type1 == JointType.FixedPrismatic); Debug.Assert(type2 == JointType.Revolute || type2 == JointType.Prismatic || type2 == JointType.FixedRevolute || type2 == JointType.FixedPrismatic); // In the case of a prismatic and revolute joint, the first body must be static. if (type1 == JointType.Revolute || type1 == JointType.Prismatic) { Debug.Assert(jointA.BodyA.Type == BodyType.Static); } if (type2 == JointType.Revolute || type2 == JointType.Prismatic) { Debug.Assert(jointB.BodyA.Type == BodyType.Static); } float coordinate1 = 0.0f, coordinate2 = 0.0f; switch (type1) { case JointType.Revolute: BodyA = jointA.BodyB; _revolute1 = (RevoluteJoint)jointA; LocalAnchor1 = _revolute1.LocalAnchorB; coordinate1 = _revolute1.JointAngle; break; case JointType.Prismatic: BodyA = jointA.BodyB; _prismatic1 = (PrismaticJoint)jointA; LocalAnchor1 = _prismatic1.LocalAnchorB; coordinate1 = _prismatic1.JointTranslation; break; case JointType.FixedRevolute: BodyA = jointA.BodyA; _fixedRevolute1 = (FixedRevoluteJoint)jointA; LocalAnchor1 = _fixedRevolute1.LocalAnchorA; coordinate1 = _fixedRevolute1.JointAngle; break; case JointType.FixedPrismatic: BodyA = jointA.BodyA; _fixedPrismatic1 = (FixedPrismaticJoint)jointA; LocalAnchor1 = _fixedPrismatic1.LocalAnchorA; coordinate1 = _fixedPrismatic1.JointTranslation; break; } switch (type2) { case JointType.Revolute: BodyB = jointB.BodyB; _revolute2 = (RevoluteJoint)jointB; LocalAnchor2 = _revolute2.LocalAnchorB; coordinate2 = _revolute2.JointAngle; break; case JointType.Prismatic: BodyB = jointB.BodyB; _prismatic2 = (PrismaticJoint)jointB; LocalAnchor2 = _prismatic2.LocalAnchorB; coordinate2 = _prismatic2.JointTranslation; break; case JointType.FixedRevolute: BodyB = jointB.BodyA; _fixedRevolute2 = (FixedRevoluteJoint)jointB; LocalAnchor2 = _fixedRevolute2.LocalAnchorA; coordinate2 = _fixedRevolute2.JointAngle; break; case JointType.FixedPrismatic: BodyB = jointB.BodyA; _fixedPrismatic2 = (FixedPrismaticJoint)jointB; LocalAnchor2 = _fixedPrismatic2.LocalAnchorA; coordinate2 = _fixedPrismatic2.JointTranslation; break; } _ant = coordinate1 + Ratio * coordinate2; }
/// <summary> /// Requires two existing revolute or prismatic joints (any combination will work). /// The provided joints must attach a dynamic body to a static body. /// </summary> /// <param name="jointA">The first joint.</param> /// <param name="jointB">The second joint.</param> /// <param name="ratio">The ratio.</param> public GearJoint(Joint jointA, Joint jointB, float ratio) : base(jointA.BodyA, jointA.BodyB) { JointType = JointType.Gear; JointA = jointA; JointB = jointB; Ratio = ratio; JointType type1 = jointA.JointType; JointType type2 = jointB.JointType; // Make sure its the right kind of joint Debug.Assert(type1 == JointType.Revolute || type1 == JointType.Prismatic || type1 == JointType.FixedRevolute || type1 == JointType.FixedPrismatic); Debug.Assert(type2 == JointType.Revolute || type2 == JointType.Prismatic || type2 == JointType.FixedRevolute || type2 == JointType.FixedPrismatic); // In the case of a prismatic and revolute joint, the first body must be static. if (type1 == JointType.Revolute || type1 == JointType.Prismatic) Debug.Assert(jointA.BodyA.BodyType == BodyType.Static); if (type2 == JointType.Revolute || type2 == JointType.Prismatic) Debug.Assert(jointB.BodyA.BodyType == BodyType.Static); float coordinate1 = 0.0f, coordinate2 = 0.0f; switch (type1) { case JointType.Revolute: BodyA = jointA.BodyB; _revolute1 = (RevoluteJoint)jointA; LocalAnchor1 = _revolute1.LocalAnchorB; coordinate1 = _revolute1.JointAngle; break; case JointType.Prismatic: BodyA = jointA.BodyB; _prismatic1 = (PrismaticJoint)jointA; LocalAnchor1 = _prismatic1.LocalAnchorB; coordinate1 = _prismatic1.JointTranslation; break; case JointType.FixedRevolute: BodyA = jointA.BodyA; _fixedRevolute1 = (FixedRevoluteJoint)jointA; LocalAnchor1 = _fixedRevolute1.LocalAnchorA; coordinate1 = _fixedRevolute1.JointAngle; break; case JointType.FixedPrismatic: BodyA = jointA.BodyA; _fixedPrismatic1 = (FixedPrismaticJoint)jointA; LocalAnchor1 = _fixedPrismatic1.LocalAnchorA; coordinate1 = _fixedPrismatic1.JointTranslation; break; } switch (type2) { case JointType.Revolute: BodyB = jointB.BodyB; _revolute2 = (RevoluteJoint)jointB; LocalAnchor2 = _revolute2.LocalAnchorB; coordinate2 = _revolute2.JointAngle; break; case JointType.Prismatic: BodyB = jointB.BodyB; _prismatic2 = (PrismaticJoint)jointB; LocalAnchor2 = _prismatic2.LocalAnchorB; coordinate2 = _prismatic2.JointTranslation; break; case JointType.FixedRevolute: BodyB = jointB.BodyA; _fixedRevolute2 = (FixedRevoluteJoint)jointB; LocalAnchor2 = _fixedRevolute2.LocalAnchorA; coordinate2 = _fixedRevolute2.JointAngle; break; case JointType.FixedPrismatic: BodyB = jointB.BodyA; _fixedPrismatic2 = (FixedPrismaticJoint)jointB; LocalAnchor2 = _fixedPrismatic2.LocalAnchorA; coordinate2 = _fixedPrismatic2.JointTranslation; break; } _ant = coordinate1 + Ratio * coordinate2; }
private SliderCrankTest() { Body ground; { ground = BodyFactory.CreateBody(World); EdgeShape shape = new EdgeShape(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.CreateFixture(shape); } { Body prevBody = ground; // Define crank. { PolygonShape shape = new PolygonShape(2); shape.SetAsBox(0.5f, 2.0f); Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(0.0f, 7.0f); body.CreateFixture(shape); Vector2 anchor = new Vector2(0.0f, -2.0f); _joint1 = new RevoluteJoint(prevBody, body, prevBody.GetLocalPoint(body.GetWorldPoint(anchor)), anchor); _joint1.MotorSpeed = 1.0f * Settings.Pi; _joint1.MaxMotorTorque = 10000.0f; _joint1.MotorEnabled = true; World.AddJoint(_joint1); prevBody = body; } // Define follower. { PolygonShape shape = new PolygonShape(2); shape.SetAsBox(0.5f, 4.0f); Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(0.0f, 13.0f); body.CreateFixture(shape); Vector2 anchor = new Vector2(0.0f, -4.0f); RevoluteJoint rjd3 = new RevoluteJoint(prevBody, body, prevBody.GetLocalPoint(body.GetWorldPoint(anchor)), anchor); rjd3.MotorEnabled = false; World.AddJoint(rjd3); prevBody = body; } // Define piston { PolygonShape shape = new PolygonShape(2); shape.SetAsBox(1.5f, 1.5f); Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(0.0f, 17.0f); body.CreateFixture(shape); Vector2 anchor = Vector2.Zero; RevoluteJoint rjd2 = new RevoluteJoint(prevBody, body, prevBody.GetLocalPoint(body.GetWorldPoint(anchor)), anchor); World.AddJoint(rjd2); _joint2 = new FixedPrismaticJoint(body, new Vector2(0.0f, 17.0f), new Vector2(0.0f, 1.0f)); _joint2.MaxMotorForce = 1000.0f; _joint2.MotorEnabled = true; World.AddJoint(_joint2); } // Create a payload { PolygonShape shape = new PolygonShape(2); shape.SetAsBox(1.5f, 1.5f); Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(0.0f, 23.0f); body.CreateFixture(shape); } } }
/// <summary> /// Call during the method in which you create the body for the object. /// Make sure it's used afterwards otherwise you'll have issues. /// </summary> /// <param name="world"></param> protected virtual void SetUpJoint(World world) { #if EDITOR #else this._currentMovingDirection = false; Vector2 axis = Vector2.Zero; if (_movementDirection == Direction.Horizontal) { if (_endPosition.X < _position.X) { axis = new Vector2(-1, 0); } else { axis = new Vector2(1, 0); } } else { if (_endPosition.Y < _position.Y) { axis = new Vector2(0, -1); } else { axis = new Vector2(0, 1); } } this._prismaticJoint = JointFactory.CreateFixedPrismaticJoint(world, this.Body, ConvertUnits.ToSimUnits(this.Position), axis); if (_movementDirection == Direction.Horizontal) { this._prismaticJoint.UpperLimit = Math.Abs(ConvertUnits.ToSimUnits(_endPosition.X - Position.X)); } else { this._prismaticJoint.UpperLimit = Math.Abs(ConvertUnits.ToSimUnits(_endPosition.Y - Position.Y)); } this._prismaticJoint.LowerLimit = 0.0f; this._prismaticJoint.LimitEnabled = true; this._prismaticJoint.MotorEnabled = true; this._prismaticJoint.MaxMotorForce = float.MaxValue; if (_startsMoving) { this._isMoving = true; this._prismaticJoint.MotorSpeed = _motorSpeed; } #endif }
void HandleJoint(float delta, FixedPrismaticJoint joint, float i ) { #if !EDITOR if ((joint.JointTranslation >= joint.UpperLimit && !this.MovingToStart) || (joint.JointTranslation <= joint.LowerLimit && this.MovingToStart)) { if (joint.MotorSpeed != 0) { joint.MotorSpeed = 0.0f; } } else { if (_motorSpeed < 0) { i *= -1.0f; } if (joint.MotorSpeed != this._motorSpeed) { joint.MotorSpeed = this._motorSpeed; } } #endif }
public static Joint CopyJoint(Joint joint, Body bodyA, Body bodyB, World world) { Joint newJoint = null; switch (joint.JointType) { case JointType.Angle: newJoint = JointFactory.CreateAngleJoint(world, bodyA, bodyB); break; case JointType.Distance: newJoint = new DistanceJoint(bodyA, bodyB, bodyA.WorldCenter, bodyB.WorldCenter); break; case JointType.FixedAngle: newJoint = new FixedAngleJoint(bodyA); break; case JointType.FixedDistance: newJoint = new FixedDistanceJoint(bodyA, bodyA.WorldCenter, Vector2.Zero); break; case JointType.FixedFriction: newJoint = new FixedFrictionJoint(bodyA, bodyA.WorldCenter); break; case JointType.FixedPrismatic: var fpJoint = joint as FixedPrismaticJoint; var fpAxis = fpJoint.LocalXAxis1; newJoint = new FixedPrismaticJoint(bodyA, bodyA.WorldCenter, fpAxis); break; case JointType.FixedRevolute: newJoint = new FixedRevoluteJoint(bodyA, bodyA.WorldCenter, Vector2.Zero); break; case JointType.Friction: newJoint = new FrictionJoint(bodyA, bodyB, bodyA.WorldCenter, bodyB.WorldCenter); break; case JointType.Line: var lineJoint = joint as LineJoint; var axis = lineJoint.LocalXAxis; newJoint = new LineJoint(bodyA, bodyB, bodyA.WorldCenter, axis); break; case JointType.Prismatic: var pJoint = joint as PrismaticJoint; var pAxis = pJoint.LocalXAxis1; newJoint = new PrismaticJoint(bodyA, bodyB, bodyA.WorldCenter, bodyB.WorldCenter, pAxis); ((PrismaticJoint)newJoint).LimitEnabled = pJoint.LimitEnabled; ((PrismaticJoint)newJoint).MotorEnabled = pJoint.MotorEnabled; ((PrismaticJoint)newJoint).MaxMotorForce = pJoint.MaxMotorForce; ((PrismaticJoint)newJoint).MotorSpeed = pJoint.MotorSpeed; ((PrismaticJoint)newJoint).LowerLimit = pJoint.LowerLimit; ((PrismaticJoint)newJoint).UpperLimit = pJoint.UpperLimit; ((PrismaticJoint)newJoint).ReferenceAngle = pJoint.ReferenceAngle; ((PrismaticJoint)newJoint).LocalXAxis1 = pJoint.LocalXAxis1; break; case JointType.Pulley: var pulleyJoint = joint as PulleyJoint; var ratio = pulleyJoint.Ratio; newJoint = new PulleyJoint(bodyA, bodyB, Vector2.Zero, Vector2.Zero, bodyA.WorldCenter, bodyB.WorldCenter, ratio); break; case JointType.Revolute: newJoint = new RevoluteJoint(bodyA, bodyB, bodyA.WorldCenter, bodyB.WorldCenter); break; case JointType.Slider: var sliderJoint = joint as SliderJoint; var minLength = sliderJoint.MinLength; var maxLength = sliderJoint.MaxLength; newJoint = new SliderJoint(bodyA, bodyB, bodyA.WorldCenter, bodyB.WorldCenter, minLength, maxLength); break; case JointType.Weld: newJoint = new WeldJoint(bodyA, bodyB, bodyA.WorldCenter, bodyB.WorldCenter); break; } var data = new FarseerJointUserData(); data.BodyAName = ((FarseerJointUserData) joint.UserData).BodyAName; data.BodyBName = ((FarseerJointUserData) joint.UserData).BodyBName; joint.UserData = data; return newJoint; }