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 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); }
/// <summary> /// Compute and set all necessary settings to move to the vertex specified by <paramref name="vertexIndex"/>. /// </summary> /// <param name="vertexIndex">Vertex to move to.</param> private void SetMoveToVertex(int vertexIndex) { // compute move vector and time to get to the vertex moveVector = path.Vertices[vertexIndex] - actor.Position; leftTime = moveVector.Length() / Speed.Value; moveVector /= leftTime; // destroy prismatic joint if any if (fixedPrismaticJoint != null) { actor.Screen.World.RemoveJoint(fixedPrismaticJoint); fixedPrismaticJoint = null; } // kinematic body if (actor.Body != null && actor.Body.BodyType == FarseerPhysics.Dynamics.BodyType.Kinematic) { actor.Body.LinearVelocity = moveVector; } // dynamic body else if (actor.Body != null && actor.Body.BodyType == FarseerPhysics.Dynamics.BodyType.Dynamic) { fixedPrismaticJoint = JointFactory.CreateFixedPrismaticJoint(actor.Screen.World, actor.Body, path.Vertices[vertexIndex], Vector2.Normalize(moveVector)); fixedPrismaticJoint.MotorEnabled = true; fixedPrismaticJoint.MotorSpeed = Speed.Value; fixedPrismaticJoint.MaxMotorForce = float.MaxValue / 100f; } }
private PrismaticTest() { Body ground; { ground = BodyFactory.CreateBody(World); EdgeShape shape3 = new EdgeShape(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); }
/// <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 }
/// <inheritdoc /> /// <remarks> /// Deactive the movement to the location for the actor. /// </remarks> protected override void OnUpdateStopped() { if (fixedPrismaticJoint != null) { actor.Screen.World.RemoveJoint(fixedPrismaticJoint); fixedPrismaticJoint = null; } if (actor != null && actor.Body != null && actor.Body.BodyType == FarseerPhysics.Dynamics.BodyType.Kinematic) { actor.Body.LinearVelocity = Vector2.Zero; } }
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); } }
public override void LoadContent() { base.LoadContent(); World.Gravity = new Vector2(0f, 10f); //HasCursor = false; //Bounds Vertices bounds = new Vertices(); bounds.Add(new Vector2(-10, 13)); bounds.Add(new Vector2(-10, -13)); bounds.Add(new Vector2(10, -13)); bounds.Add(new Vector2(10, 13)); Body body = BodyFactory.CreateLoopShape(World, bounds); //Launcher FixtureFactory.AttachEdge(new Vector2(9, -3), new Vector2(9f, 12), body); FixtureFactory.AttachEdge(new Vector2(9, 12), new Vector2(9.5f, 12), body); FixtureFactory.AttachEdge(new Vector2(9.5f, 12), new Vector2(9.5f, -3.6f), body); //Jumper _jumper = BodyFactory.CreateRectangle(World, 0.43f, 0.2f, 5, new Vector2(9.25f, 10)); _jumper.BodyType = BodyType.Dynamic; Vector2 axis = new Vector2(0.0f, -1.0f); FixedPrismaticJoint jumperJoint = JointFactory.CreateFixedPrismaticJoint(World, _jumper, _jumper.Position, axis); jumperJoint.MotorSpeed = 50.0f; jumperJoint.MaxMotorForce = 100.0f; jumperJoint.MotorEnabled = true; jumperJoint.LowerLimit = -2.0f; jumperJoint.UpperLimit = -0.1f; jumperJoint.LimitEnabled = true; //Top arc FixtureFactory.AttachLineArc(MathHelper.Pi, 50, 9.5f, new Vector2(0, -3), MathHelper.Pi, false, body); // Circle character _ball = BodyFactory.CreateBody(World, new Vector2(9.25f, 9.0f)); _ball.BodyType = BodyType.Dynamic; _ball.IsBullet = true; _ball.CreateFixture(new CircleShape(0.2f, 1.0f)); }
/// <summary> /// Compute and set all necessary settings to move to the location. /// </summary> protected virtual void InitMovement() { if (actor != null && actor.Body != null) { // kinematic body if (actor.Body.BodyType == FarseerPhysics.Dynamics.BodyType.Kinematic) { actor.Body.LinearVelocity = moveVector; } // dynamic body else if (actor.Body.BodyType == FarseerPhysics.Dynamics.BodyType.Dynamic) { fixedPrismaticJoint = JointFactory.CreateFixedPrismaticJoint(actor.Screen.World, actor.Body, EndLocation.Value, Vector2.Normalize(moveVector)); fixedPrismaticJoint.MotorEnabled = true; fixedPrismaticJoint.MotorSpeed = speed; fixedPrismaticJoint.MaxMotorForce = float.MaxValue / 100f; } } }
internal override void UpdateJoint() { base.UpdateJoint(); if (this.joint == null) { return; } FixedPrismaticJoint j = this.joint as FixedPrismaticJoint; j.LocalAnchorA = PhysicsConvert.ToPhysicalUnit(this.worldAnchor); j.LocalAnchorB = Vector2.Zero; j.ReferenceAngle = this.refAngle; j.LocalXAxis1 = this.moveAxis; j.LimitEnabled = this.limitEnabled; j.LowerLimit = PhysicsConvert.ToPhysicalUnit(this.lowerLimit); j.UpperLimit = PhysicsConvert.ToPhysicalUnit(this.upperLimit); j.MotorEnabled = this.motorEnabled; j.MotorSpeed = PhysicsConvert.ToPhysicalUnit(this.motorSpeed) / Time.SPFMult; j.MaxMotorForce = PhysicsConvert.ToPhysicalUnit(this.maxMotorForce) / Time.SPFMult; }
protected override void SetupPhysics(World world) { float textureWidth = ConvertUnits.ToSimUnits(this._texture.Width); float textureHeight = ConvertUnits.ToSimUnits(this._texture.Height); Vector2 axis = SpinAssist.ModifyVectorByOrientation(new Vector2(0, -1), _orientation); Body body; #region Shaft for (int i = 0; i < _shaftPieces; i++) { body = new Body(world); body.Position = ConvertUnits.ToSimUnits(this._position); body.Rotation = SpinAssist.RotationByOrientation(this._orientation); if (i == 0) { Fixture fixture = FixtureFactory.AttachRectangle(textureWidth, textureHeight, 1.0f, Vector2.Zero, body); body.BodyType = BodyType.Static; } else { Fixture fixture = FixtureFactory.AttachRectangle(textureWidth - ((textureWidth * 0.04f) * i), textureHeight, 1.0f, Vector2.Zero, body); body.BodyType = BodyType.Dynamic; FixedPrismaticJoint _joint = JointFactory.CreateFixedPrismaticJoint(world, body, ConvertUnits.ToSimUnits(this._position), axis); _joint.MotorEnabled = true; _joint.MaxMotorForce = float.MaxValue; _joint.LimitEnabled = true; _joint.LowerLimit = 0; _joint.UpperLimit = (textureHeight * i); _joints.Add(_joint); } body.Friction = 3.0f; body.CollisionCategories = Category.Cat2; // Ignore collision with Statics and other pistons. body.CollidesWith = Category.All & ~Category.Cat2 & ~Category.Cat20; body.UserData = _materialType; _shaftBodies.Add(body); } #endregion #region Endpiece TexVertOutput input = SpinAssist.TexToVert(world, _crusherTexture, ConvertUnits.ToSimUnits(10), true); _crusherTextureOrigin = -ConvertUnits.ToSimUnits(input.Origin); this.Body = input.Body; this.Body.Position = ConvertUnits.ToSimUnits(this._position); this.Body.Rotation = this._rotation; this.Body.Friction = 3.0f; this.Body.Restitution = 0.0f; this.Body.BodyType = BodyType.Dynamic; this.Body.Mass = 100.0f; this.Body.CollisionCategories = Category.Cat2; // Ignore collision with Statics and other pistons. this.Body.CollidesWith = Category.All & ~Category.Cat2 & ~Category.Cat20; this.Body.UserData = _materialType; this._prismaticJoint = JointFactory.CreateFixedPrismaticJoint(world, this.Body, ConvertUnits.ToSimUnits(this._position + new Vector2(0, 40)), axis); this._prismaticJoint.UpperLimit = (textureHeight * (_shaftPieces)); this._prismaticJoint.LowerLimit = (textureHeight * 0.5f); this._prismaticJoint.LimitEnabled = true; this._prismaticJoint.MotorEnabled = true; this._prismaticJoint.MaxMotorForce = float.MaxValue; #endregion if (_isLethal) { float endHeight = ConvertUnits.ToSimUnits(_crusherTexture.Height); Fixture fix = FixtureFactory.AttachRectangle( ConvertUnits.ToSimUnits(_crusherTexture.Width - 20), endHeight * 0.38f, 1.0f, new Vector2(0, -endHeight * 0.4f), Body); fix.IsSensor = true; fix.IgnoreCollisionWith(this.Body.FixtureList[0]); fix.Body.OnCollision += TouchedLethal; fix.Body.OnSeparation += LeftLethal; } }
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); } } }
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; } }