private BridgeTest() { Body ground; { ground = new Body(World); EdgeShape shape = new EdgeShape(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.CreateFixture(shape); } { Vertices box = PolygonTools.CreateRectangle(0.5f, 0.125f); PolygonShape shape = new PolygonShape(box, 20); Body prevBody = ground; for (int i = 0; i < Count; ++i) { Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(-14.5f + 1.0f * i, 5.0f); Fixture fixture = body.CreateFixture(shape); fixture.Friction = 0.2f; Vector2 anchor = new Vector2(-15f + 1.0f * i, 5.0f); RevoluteJoint jd = new RevoluteJoint(prevBody, body, anchor, true); World.AddJoint(jd); prevBody = body; } Vector2 anchor2 = new Vector2(-15.0f + 1.0f * Count, 5.0f); RevoluteJoint jd2 = new RevoluteJoint(ground, prevBody, anchor2, true); World.AddJoint(jd2); } Vertices vertices = new Vertices(3); vertices.Add(new Vector2(-0.5f, 0.0f)); vertices.Add(new Vector2(0.5f, 0.0f)); vertices.Add(new Vector2(0.0f, 1.5f)); for (int i = 0; i < 2; ++i) { PolygonShape shape = new PolygonShape(vertices, 1); Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(-8.0f + 8.0f * i, 12.0f); body.CreateFixture(shape); } for (int i = 0; i < 3; ++i) { CircleShape shape = new CircleShape(0.5f, 1); Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(-6.0f + 6.0f * i, 10.0f); body.CreateFixture(shape); } }
private CompoundShapes() { //Ground BodyFactory.CreateEdge(World, new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); { CircleShape circle1 = new CircleShape(0.5f, 2); circle1.Position = new Vector2(-0.5f, 0.5f); CircleShape circle2 = new CircleShape(0.5f, 2); circle2.Position = new Vector2(0.5f, 0.5f); for (int i = 0; i < 10; ++i) { float x = Rand.RandomFloat(-0.1f, 0.1f); Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(x + 5.0f, 1.05f + 2.5f * i); body.Rotation = Rand.RandomFloat(-Settings.Pi, Settings.Pi); body.CreateFixture(circle1); body.CreateFixture(circle2); } } { Vertices box = PolygonTools.CreateRectangle(0.25f, 0.5f); PolygonShape polygon1 = new PolygonShape(box, 2); box = PolygonTools.CreateRectangle(0.25f, 0.5f, new Vector2(0.0f, -0.5f), 0.5f * Settings.Pi); PolygonShape polygon2 = new PolygonShape(box, 2); for (int i = 0; i < 10; ++i) { float x = Rand.RandomFloat(-0.1f, 0.1f); Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(x - 5.0f, 1.05f + 2.5f * i); body.Rotation = Rand.RandomFloat(-Settings.Pi, Settings.Pi); body.CreateFixture(polygon1); body.CreateFixture(polygon2); } } { Transform xf1 = new Transform(); xf1.q.Set(0.3524f * Settings.Pi); xf1.p = MathUtils.Mul(ref xf1.q, new Vector2(1.0f, 0.0f)); Vertices vertices = new Vertices(3); vertices.Add(MathUtils.Mul(ref xf1, new Vector2(-1.0f, 0.0f))); vertices.Add(MathUtils.Mul(ref xf1, new Vector2(1.0f, 0.0f))); vertices.Add(MathUtils.Mul(ref xf1, new Vector2(0.0f, 0.5f))); PolygonShape triangle1 = new PolygonShape(vertices, 2); Transform xf2 = new Transform(); xf2.q.Set(-0.3524f * Settings.Pi); xf2.p = MathUtils.Mul(ref xf2.q, new Vector2(-1.0f, 0.0f)); vertices[0] = MathUtils.Mul(ref xf2, new Vector2(-1.0f, 0.0f)); vertices[1] = MathUtils.Mul(ref xf2, new Vector2(1.0f, 0.0f)); vertices[2] = MathUtils.Mul(ref xf2, new Vector2(0.0f, 0.5f)); PolygonShape triangle2 = new PolygonShape(vertices, 2); for (int i = 0; i < 10; ++i) { float x = Rand.RandomFloat(-0.1f, 0.1f); Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(x, 2.05f + 2.5f * i); body.CreateFixture(triangle1); body.CreateFixture(triangle2); } } { Vertices box = PolygonTools.CreateRectangle(1.5f, 0.15f); PolygonShape bottom = new PolygonShape(box, 4); box = PolygonTools.CreateRectangle(0.15f, 2.7f, new Vector2(-1.45f, 2.35f), 0.2f); PolygonShape left = new PolygonShape(box, 4); box = PolygonTools.CreateRectangle(0.15f, 2.7f, new Vector2(1.45f, 2.35f), -0.2f); PolygonShape right = new PolygonShape(box, 4); Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(0.0f, 2.0f); body.CreateFixture(bottom); body.CreateFixture(left); body.CreateFixture(right); } }
private void AddRectangle(int width, int height) { Vertices verts = PolygonTools.CreateRectangle(width, height); _polygons.Add(verts); }
/// <summary> /// Build vertices to represent an oriented box. /// </summary> /// <param name="halfWidth">The half-width..</param> /// <param name="halfHeight">The half-height.</param> /// <param name="center">The center of the box in local coordinates.</param> /// <param name="angle">The rotation of the box in local coordinates.</param> public void SetAsBox(float halfWidth, float halfHeight, FVector2 center, float angle) { Set(PolygonTools.CreateRectangle(halfWidth, halfHeight, center, angle)); }
private CollisionFilteringTest() { //Ground BodyFactory.CreateEdge(World, new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); { // Small triangle Vertices vertices = new Vertices(3); vertices.Add(new Vector2(-1.0f, 0.0f)); vertices.Add(new Vector2(1.0f, 0.0f)); vertices.Add(new Vector2(0.0f, 2.0f)); PolygonShape polygon = new PolygonShape(vertices, 1); Body triangleBody = BodyFactory.CreateBody(World); triangleBody.BodyType = BodyType.Dynamic; triangleBody.Position = new Vector2(-5.0f, 2.0f); Fixture triangleFixture = triangleBody.CreateFixture(polygon); triangleFixture.CollisionGroup = SmallGroup; triangleFixture.CollisionCategories = TriangleCategory; triangleFixture.CollidesWith = TriangleMask; // Large triangle (recycle definitions) vertices[0] *= 2.0f; vertices[1] *= 2.0f; vertices[2] *= 2.0f; polygon.Vertices = vertices; Body triangleBody2 = BodyFactory.CreateBody(World); triangleBody2.BodyType = BodyType.Dynamic; triangleBody2.Position = new Vector2(-5.0f, 6.0f); triangleBody2.FixedRotation = true; // look at me! Fixture triangleFixture2 = triangleBody2.CreateFixture(polygon); triangleFixture2.CollisionGroup = LargeGroup; triangleFixture2.CollisionCategories = TriangleCategory; triangleFixture2.CollidesWith = TriangleMask; { Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(-5.0f, 10.0f); Vertices box = PolygonTools.CreateRectangle(0.5f, 1.0f); PolygonShape p = new PolygonShape(box, 1); body.CreateFixture(p); PrismaticJoint jd = new PrismaticJoint(triangleBody2, body, new Vector2(0, 4), Vector2.Zero, new Vector2(0.0f, 1.0f)); jd.LimitEnabled = true; jd.LowerLimit = -1.0f; jd.UpperLimit = 1.0f; World.AddJoint(jd); } // Small box polygon.Vertices = PolygonTools.CreateRectangle(1.0f, 0.5f); Body boxBody = BodyFactory.CreateBody(World); boxBody.BodyType = BodyType.Dynamic; boxBody.Position = new Vector2(0.0f, 2.0f); Fixture boxFixture = boxBody.CreateFixture(polygon); boxFixture.Restitution = 0.1f; boxFixture.CollisionGroup = SmallGroup; boxFixture.CollisionCategories = BoxCategory; boxFixture.CollidesWith = BoxMask; // Large box (recycle definitions) polygon.Vertices = PolygonTools.CreateRectangle(2, 1); Body boxBody2 = BodyFactory.CreateBody(World); boxBody2.BodyType = BodyType.Dynamic; boxBody2.Position = new Vector2(0.0f, 6.0f); Fixture boxFixture2 = boxBody2.CreateFixture(polygon); boxFixture2.CollisionGroup = LargeGroup; boxFixture2.CollisionCategories = BoxCategory; boxFixture2.CollidesWith = BoxMask; // Small circle CircleShape circle = new CircleShape(1.0f, 1); Body circleBody = BodyFactory.CreateBody(World); circleBody.BodyType = BodyType.Dynamic; circleBody.Position = new Vector2(5.0f, 2.0f); Fixture circleFixture = circleBody.CreateFixture(circle); circleFixture.CollisionGroup = SmallGroup; circleFixture.CollisionCategories = CircleCategory; circleFixture.CollidesWith = CircleMask; // Large circle circle.Radius *= 2.0f; Body circleBody2 = BodyFactory.CreateBody(World); circleBody2.BodyType = BodyType.Dynamic; circleBody2.Position = new Vector2(5.0f, 6.0f); Fixture circleFixture2 = circleBody2.CreateFixture(circle); circleFixture2.CollisionGroup = LargeGroup; circleFixture2.CollisionCategories = CircleCategory; circleFixture2.CollidesWith = CircleMask; // Large circle - Ignore with other large circle Body circleBody3 = BodyFactory.CreateBody(World); circleBody3.BodyType = BodyType.Dynamic; circleBody3.Position = new Vector2(6.0f, 9.0f); //Another large circle. This one uses IgnoreCollisionWith() logic instead of categories. Fixture circleFixture3 = circleBody3.CreateFixture(circle); circleFixture3.CollisionGroup = LargeGroup; circleFixture3.CollisionCategories = CircleCategory; circleFixture3.CollidesWith = CircleMask; circleFixture3.IgnoreCollisionWith(circleFixture2); } }
public Objects(World world, ScreenManager screenManager, Vector2 startPosition, Vector2 endPosition, int count, float radius, ObjectType type, float toothHeight = 1f) { _batch = screenManager.SpriteBatch; _bodyRadius = radius; BodyList = new List <Body>(count); for (int i = 0; i < count; i++) { switch (type) { case ObjectType.Circle: BodyList.Add(world.CreateCircle(radius, 1f)); break; case ObjectType.Rectangle: BodyList.Add(world.CreateRectangle(radius, radius, 1f)); _bodyRadius = radius / 2f; break; case ObjectType.Star: BodyList.Add(world.CreateGear(radius, 10, 0f, toothHeight, 1f)); _bodyRadius = radius * 2.7f; break; case ObjectType.Gear: BodyList.Add(world.CreateGear(radius, 10, 100f, toothHeight, 1f)); _bodyRadius = radius * 2.7f; break; } } for (int i = 0; i < BodyList.Count; i++) { Body body = BodyList[i]; body.BodyType = BodyType.Dynamic; body.Position = Vector2.Lerp(startPosition, endPosition, i / (float)(count - 1)); foreach (Fixture fixture in body.FixtureList) { fixture.Restitution = 0.7f; fixture.Friction = 0.2f; fixture.CollisionCategories = Category.All; fixture.CollidesWith = Category.All; } } //GFX AssetCreator creator = screenManager.Assets; switch (type) { case ObjectType.Circle: _object = new Sprite(creator.CircleTexture(radius, MaterialType.Dots, Color.DarkRed, 0.8f, 24f)); break; case ObjectType.Rectangle: _object = new Sprite(creator.TextureFromVertices(PolygonTools.CreateRectangle(radius / 2f, radius / 2f), MaterialType.Dots, Color.Blue, 0.8f, 24f)); break; case ObjectType.Star: _object = new Sprite(creator.TextureFromVertices(PolygonTools.CreateGear(radius, 10, 0f, toothHeight), MaterialType.Dots, Color.Yellow, 0.8f, 24f)); break; case ObjectType.Gear: _object = new Sprite(creator.TextureFromVertices(PolygonTools.CreateGear(radius, 10, 100f, toothHeight), MaterialType.Dots, Color.DarkGreen, 0.8f, 24f)); break; } }
private CharacterCollisionTest() { //Ground body Body ground = World.CreateEdge(new Vector2(-20.0f, 0.0f), new Vector2(20.0f, 0.0f)); // Collinear edges with no adjacency information. // This shows the problematic case where a box shape can hit // an internal vertex. EdgeShape shape = new EdgeShape(new Vector2(-8.0f, 1.0f), new Vector2(-6.0f, 1.0f)); ground.CreateFixture(shape); shape = new EdgeShape(new Vector2(-6.0f, 1.0f), new Vector2(-4.0f, 1.0f)); ground.CreateFixture(shape); shape = new EdgeShape(new Vector2(-4.0f, 1.0f), new Vector2(-2.0f, 1.0f)); ground.CreateFixture(shape); // Chain shape { Vertices vs = new Vertices(4); vs.Add(new Vector2(5.0f, 7.0f)); vs.Add(new Vector2(6.0f, 8.0f)); vs.Add(new Vector2(7.0f, 8.0f)); vs.Add(new Vector2(8.0f, 7.0f)); Body body = World.CreateChainShape(vs); body.Rotation = 0.25f * MathHelper.Pi; } // Square tiles. This shows that adjacency shapes may // have non-smooth collision. There is no solution // to this problem. PolygonShape tile = new PolygonShape(1); tile.Vertices = PolygonTools.CreateRectangle(1.0f, 1.0f, new Vector2(4.0f, 3.0f), 0.0f); ground.CreateFixture(tile); tile.Vertices = PolygonTools.CreateRectangle(1.0f, 1.0f, new Vector2(6.0f, 3.0f), 0.0f); ground.CreateFixture(tile); tile.Vertices = PolygonTools.CreateRectangle(1.0f, 1.0f, new Vector2(8.0f, 3.0f), 0.0f); ground.CreateFixture(tile); // Square made from an edge loop. Collision should be smooth. Vertices vertices = new Vertices(4); vertices.Add(new Vector2(-1.0f, 3.0f)); vertices.Add(new Vector2(1.0f, 3.0f)); vertices.Add(new Vector2(1.0f, 5.0f)); vertices.Add(new Vector2(-1.0f, 5.0f)); ground.CreateLoopShape(vertices); // Edge loop. Collision should be smooth. vertices = new Vertices(10); vertices.Add(new Vector2(0.0f, 0.0f)); vertices.Add(new Vector2(6.0f, 0.0f)); vertices.Add(new Vector2(6.0f, 2.0f)); vertices.Add(new Vector2(4.0f, 1.0f)); vertices.Add(new Vector2(2.0f, 2.0f)); vertices.Add(new Vector2(0.0f, 2.0f)); vertices.Add(new Vector2(-2.0f, 2.0f)); vertices.Add(new Vector2(-4.0f, 3.0f)); vertices.Add(new Vector2(-6.0f, 2.0f)); vertices.Add(new Vector2(-6.0f, 0.0f)); World.CreateLoopShape(vertices, new Vector2(-10, 4)); // Square character 1 Body squareCharacter = World.CreateRectangle(1, 1, 20, new Vector2(-3.0f, 8.0f)); squareCharacter.BodyType = BodyType.Dynamic; squareCharacter.FixedRotation = true; squareCharacter.SleepingAllowed = false; squareCharacter.OnBeginContact += CharacterOnCollision; squareCharacter.OnEndContact += CharacterOnSeparation; // Square character 2 Body squareCharacter2 = World.CreateRectangle(0.5f, 0.5f, 20, new Vector2(-5.0f, 5.0f)); squareCharacter2.BodyType = BodyType.Dynamic; squareCharacter2.FixedRotation = true; squareCharacter2.SleepingAllowed = false; // Hexagon character float angle = 0.0f; const float delta = MathHelper.Pi / 3.0f; vertices = new Vertices(6); for (int i = 0; i < 6; ++i) { vertices.Add(new Vector2(0.5f * (float)Math.Cos(angle), 0.5f * (float)Math.Sin(angle))); angle += delta; } Body hexCharacter = World.CreatePolygon(vertices, 20, new Vector2(-5.0f, 8.0f)); hexCharacter.BodyType = BodyType.Dynamic; hexCharacter.FixedRotation = true; hexCharacter.SleepingAllowed = false; // Circle character Body circleCharacter = World.CreateCircle(0.5f, 20, new Vector2(3.0f, 5.0f)); circleCharacter.BodyType = BodyType.Dynamic; circleCharacter.FixedRotation = true; circleCharacter.SleepingAllowed = false; // Circle character _character = World.CreateCircle(0.25f, 20, new Vector2(-7.0f, 6.0f)); _character.BodyType = BodyType.Dynamic; _character.SetFriction(1.0f); _character.SleepingAllowed = false; }
private ApplyForceTest() { World.Gravity = Vector2.Zero; const float restitution = 0.4f; Body ground; { ground = BodyFactory.CreateBody(World); ground.Position = new Vector2(0.0f, 20.0f); EdgeShape shape = new EdgeShape(new Vector2(-20.0f, -20.0f), new Vector2(-20.0f, 20.0f)); // Left vertical Fixture fixture = ground.CreateFixture(shape); fixture.Restitution = restitution; // Right vertical shape = new EdgeShape(new Vector2(20.0f, -20.0f), new Vector2(20.0f, 20.0f)); ground.CreateFixture(shape); // Top horizontal shape = new EdgeShape(new Vector2(-20.0f, 20.0f), new Vector2(20.0f, 20.0f)); ground.CreateFixture(shape); // Bottom horizontal shape = new EdgeShape(new Vector2(-20.0f, -20.0f), new Vector2(20.0f, -20.0f)); ground.CreateFixture(shape); } { Transform xf1 = new Transform(); xf1.q.Set(0.3524f * Settings.Pi); xf1.p = MathUtils.Mul(ref xf1.q, new Vector2(1.0f, 0.0f)); Vertices vertices = new Vertices(3); vertices.Add(MathUtils.Mul(ref xf1, new Vector2(-1.0f, 0.0f))); vertices.Add(MathUtils.Mul(ref xf1, new Vector2(1.0f, 0.0f))); vertices.Add(MathUtils.Mul(ref xf1, new Vector2(0.0f, 0.5f))); PolygonShape poly1 = new PolygonShape(vertices, 4); Transform xf2 = new Transform(); xf2.q.Set(-0.3524f * Settings.Pi); xf2.p = MathUtils.Mul(ref xf2.q, new Vector2(-1.0f, 0.0f)); vertices[0] = MathUtils.Mul(ref xf2, new Vector2(-1.0f, 0.0f)); vertices[1] = MathUtils.Mul(ref xf2, new Vector2(1.0f, 0.0f)); vertices[2] = MathUtils.Mul(ref xf2, new Vector2(0.0f, 0.5f)); PolygonShape poly2 = new PolygonShape(vertices, 2); _body = BodyFactory.CreateBody(World); _body.BodyType = BodyType.Dynamic; _body.Position = new Vector2(0.0f, 2.0f); _body.Rotation = Settings.Pi; _body.AngularDamping = 5.0f; _body.LinearDamping = 0.8f; _body.SleepingAllowed = true; _body.CreateFixture(poly1); _body.CreateFixture(poly2); } { Vertices box = PolygonTools.CreateRectangle(0.5f, 0.5f); PolygonShape shape = new PolygonShape(box, 1); for (int i = 0; i < 10; ++i) { Body body = BodyFactory.CreateBody(World); body.Position = new Vector2(0.0f, 5.0f + 1.54f * i); body.BodyType = BodyType.Dynamic; Fixture fixture = body.CreateFixture(shape); fixture.Friction = 0.3f; const float gravity = 10.0f; float I = body.Inertia; float mass = body.Mass; // For a circle: I = 0.5 * m * r * r ==> r = sqrt(2 * I / m) float radius = (float)Math.Sqrt(2.0 * (I / mass)); FrictionJoint jd = new FrictionJoint(ground, body, Vector2.Zero, Vector2.Zero); jd.CollideConnected = true; jd.MaxForce = mass * gravity; jd.MaxTorque = mass * radius * gravity; World.AddJoint(jd); } } }
public FSBoxBody(World world, Subtexture subtexture, float density, Vector2 position = default(Vector2), BodyType bodyType = BodyType.Static) : base(world, subtexture, PolygonTools.CreateRectangle(subtexture.sourceRect.Width / 2, subtexture.sourceRect.Height / 2), density, position, bodyType) { }
private CantileverTest() { 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); } { Vertices box = PolygonTools.CreateRectangle(0.5f, 0.125f); PolygonShape shape = new PolygonShape(box, 20); Body prevBody = ground; for (int i = 0; i < Count; ++i) { Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(-14.5f + 1.0f * i, 5.0f); body.CreateFixture(shape); Vector2 anchor = new Vector2(-15.0f + 1.0f * i, 5.0f); WeldJoint jd = new WeldJoint(prevBody, body, prevBody.GetLocalPoint(anchor), body.GetLocalPoint(anchor)); World.AddJoint(jd); prevBody = body; } } { Vertices box = PolygonTools.CreateRectangle(0.5f, 0.125f); PolygonShape shape = new PolygonShape(box, 20); Body prevBody = ground; for (int i = 0; i < Count; ++i) { Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(-14.5f + 1.0f * i, 15.0f); body.CreateFixture(shape); Vector2 anchor = new Vector2(-15.0f + 1.0f * i, 15.0f); WeldJoint jd = new WeldJoint(prevBody, body, prevBody.GetLocalPoint(anchor), body.GetLocalPoint(anchor)); World.AddJoint(jd); prevBody = body; } } { Vertices box = PolygonTools.CreateRectangle(0.5f, 0.125f); PolygonShape shape = new PolygonShape(box, 20); Body prevBody = ground; for (int i = 0; i < Count; ++i) { Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(-4.5f + 1.0f * i, 5.0f); body.CreateFixture(shape); if (i > 0) { Vector2 anchor = new Vector2(-5.0f + 1.0f * i, 5.0f); WeldJoint jd = new WeldJoint(prevBody, body, prevBody.GetLocalPoint(anchor), body.GetLocalPoint(anchor)); World.AddJoint(jd); } prevBody = body; } } { Vertices box = PolygonTools.CreateRectangle(0.5f, 0.125f); PolygonShape shape = new PolygonShape(box, 20); Body prevBody = ground; for (int i = 0; i < Count; ++i) { Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(5.5f + 1.0f * i, 10.0f); body.CreateFixture(shape); if (i > 0) { Vector2 anchor = new Vector2(5.0f + 1.0f * i, 10.0f); WeldJoint jd = new WeldJoint(prevBody, body, prevBody.GetLocalPoint(anchor), body.GetLocalPoint(anchor)); World.AddJoint(jd); } prevBody = body; } } //Triangels Vertices vertices = new Vertices(3); vertices.Add(new Vector2(-0.5f, 0.0f)); vertices.Add(new Vector2(0.5f, 0.0f)); vertices.Add(new Vector2(0.0f, 1.5f)); for (int i = 0; i < 2; ++i) { PolygonShape shape = new PolygonShape(vertices, 1); Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(-8.0f + 8.0f * i, 12.0f); body.CreateFixture(shape); } //Circles for (int i = 0; i < 2; ++i) { CircleShape shape = new CircleShape(0.5f, 1); Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(-6.0f + 6.0f * i, 10.0f); body.CreateFixture(shape); } }
public FSBoxBody(Sprite sprite) : base(sprite, PolygonTools.CreateRectangle(sprite.SourceRect.Width / 2, sprite.SourceRect.Height / 2)) { }
/// <summary> /// Called once to load content /// </summary> /// <param name="GraphicInfo"></param> /// <param name="factory"></param> /// <param name="contentManager"></param> protected override void LoadContent(PloobsEngine.Engine.GraphicInfo GraphicInfo, PloobsEngine.Engine.GraphicFactory factory, PloobsEngine.SceneControl.IContentManager contentManager) { ///load background texture tile = factory.GetTexture2D("Textures/tile"); ///recover the physic world reference FarseerWorld fworld = this.World.PhysicWorld as FarseerWorld; ///from vertices { ////creating objects from vertices Vertices Vertices = new Vertices(3); Vertices.Add(new Vector2(0, 0)); Vertices.Add(new Vector2(100, 0)); Vertices.Add(new Vector2(0, -100)); ///creating the IModelo (graphic representation) SpriteFarseer SpriteFarseer = new SpriteFarseer(factory, Vertices, Color.Green); ///The material (how to draw) Basic2DTextureMaterial mat = new Basic2DTextureMaterial(); ///the physic object (physic representation) FarseerObject fs = new FarseerObject(fworld, SpriteFarseer, 1, BodyType.Static); ///the iobject (that comprises all) I2DObject o = new I2DObject(fs, mat, SpriteFarseer); ///adding to the world this.World.AddObject(o); } ///Creating from factory helper Vertices verts = PolygonTools.CreateRectangle(GraphicInfo.BackBufferWidth, 100); { IModelo2D model = new SpriteFarseer(factory, verts, Color.Red); Basic2DTextureMaterial mat = new Basic2DTextureMaterial(); FarseerObject fs = new FarseerObject(fworld, model, 1, BodyType.Static); I2DObject o = new I2DObject(fs, mat, model); ///the origin of the created object will be in the center of it, this mean: if we draw it, the center of mass of it will be on the midle of the screen ///We need to translate it a bit down o.PhysicObject.Position = new Vector2(0, 250); this.World.AddObject(o); } ///Creating from factory helper verts = PolygonTools.CreateRectangle(GraphicInfo.BackBufferWidth, 100); { IModelo2D model = new SpriteFarseer(factory, verts, Color.Red); Basic2DTextureMaterial mat = new Basic2DTextureMaterial(); FarseerObject fs = new FarseerObject(fworld, model, 1, BodyType.Static); I2DObject o = new I2DObject(fs, mat, model); ///the origin of the created object will be in the center of it, this mean: if we draw it, the center of mass of it will be on the midle of the screen ///We need to translate it a bit down o.PhysicObject.Position = new Vector2(GraphicInfo.BackBufferWidth, 450); this.World.AddObject(o); } ///creating a circle =P CircleShape circle = new CircleShape(50, 1); { IModelo2D model = new SpriteFarseer(factory, circle, Color.Orange); Basic2DTextureMaterial mat = new Basic2DTextureMaterial(); FarseerObject fs = new FarseerObject(fworld, model); Tracked = new I2DObject(fs, mat, model); Tracked.PhysicObject.Position = new Vector2(0, -250); /// a middle of the screen + 250 pixels up this.World.AddObject(Tracked); } ///when space is pressed, perform the following action this.BindInput(new SimpleConcreteKeyboardInputPlayable(StateKey.PRESS, Keys.Space, (a) => { if (tracking == false) { ///enable camera tracking (this.World.Camera2D as Camera2D).TrackingBody = Tracked; (this.World.Camera2D as Camera2D).EnablePositionTracking = true; } else { ///reset the camera (recreating =P) this.World.Camera2D = new Camera2D(GraphicInfo); } tracking = !tracking; } )); ///the basic ortographic 2D camera this.World.Camera2D = new Camera2D(GraphicInfo); base.LoadContent(GraphicInfo, factory, contentManager); }
protected override void LoadContent(PloobsEngine.Engine.GraphicInfo GraphicInfo, PloobsEngine.Engine.GraphicFactory factory, PloobsEngine.SceneControl.IContentManager contentManager) { tile = factory.GetTexture2D("Textures/tile"); FarseerWorld fworld = this.World.PhysicWorld as FarseerWorld; ///border border = new Border(fworld, factory, GraphicInfo, factory.CreateTexture2DColor(1, 1, Color.Red)); ///from texture { Texture2D tex = factory.GetTexture2D("Textures//goo"); IModelo2D model = new SpriteFarseer(tex); Basic2DTextureMaterial mat = new Basic2DTextureMaterial(); FarseerObject fs = new FarseerObject(fworld, tex); I2DObject o = new I2DObject(fs, mat, model); o.OnHasMoved += new PloobsEngine.SceneControl._2DScene.OnHasMoved(o_OnHasMoved); this.World.AddObject(o); } ///from texture, scale usage sample { Texture2D tex = factory.GetTexture2D("Textures//goo"); tex = factory.GetScaledTexture(tex, new Vector2(2)); IModelo2D model = new SpriteFarseer(tex); Basic2DTextureMaterial mat = new Basic2DTextureMaterial(); FarseerObject fs = new FarseerObject(fworld, tex); I2DObject o = new I2DObject(fs, mat, model); this.World.AddObject(o); } ///rectangle Vertices verts = PolygonTools.CreateRectangle(5, 5); { IModelo2D model = new SpriteFarseer(factory, verts, Color.Orange); Basic2DTextureMaterial mat = new Basic2DTextureMaterial(); FarseerObject fs = new FarseerObject(fworld, verts); I2DObject o = new I2DObject(fs, mat, model); this.World.AddObject(o); } ///circle CircleShape circle = new CircleShape(5, 1); { IModelo2D model = new SpriteFarseer(factory, circle, Color.Orange); Basic2DTextureMaterial mat = new Basic2DTextureMaterial(); FarseerObject fs = new FarseerObject(fworld, circle); I2DObject o = new I2DObject(fs, mat, model); this.World.AddObject(o); } ///camera this.World.Camera2D = new Camera2D(GraphicInfo); DPFSParticleSystem ps = new DPFSParticleSystem("TESTE", new SpriteParticleSystem(null)); this.World.ParticleManager.AddAndInitializeParticleSystem(ps); ///add a post effect =P //this.RenderTechnic.AddPostEffect(new WigglePostEffect()); ///updateable JointUpdateable ju = new JointUpdateable(this, fworld, this.World.Camera2D); base.LoadContent(GraphicInfo, factory, contentManager); }
public Rope(Item item, XElement element) : base(item, element) { string spritePath = ToolBox.GetAttributeString(element, "sprite", ""); if (spritePath == "") { DebugConsole.ThrowError("Sprite " + spritePath + " in " + element + " not found!"); } float length = ConvertUnits.ToSimUnits(ToolBox.GetAttributeFloat(element, "length", 200.0f)); pullForce = ToolBox.GetAttributeFloat(element, "pullforce", 10.0f); projectileAnchor = Vector2.Zero; projectileAnchor.X = ToolBox.GetAttributeFloat(element, "projectileanchorx", 0.0f); projectileAnchor.Y = ToolBox.GetAttributeFloat(element, "projectileanchory", 0.0f); projectileAnchor = ConvertUnits.ToSimUnits(projectileAnchor); characterUsable = ToolBox.GetAttributeBool(element, "characterusable", false); sprite = new Sprite(spritePath, new Vector2(0.5f, 0.5f)); sectionLength = ConvertUnits.ToSimUnits(sprite.size.X); Path ropePath = new Path(); ropePath.Add(item.body.SimPosition); ropePath.Add(item.body.SimPosition + new Vector2(length, 0.0f)); ropePath.Closed = false; Vertices box = PolygonTools.CreateRectangle(sectionLength, 0.05f); PolygonShape shape = new PolygonShape(box, 5); List <Body> ropeList = PathManager.EvenlyDistributeShapesAlongPath(GameMain.World, ropePath, shape, BodyType.Dynamic, (int)(length / sectionLength)); ropeBodies = new PhysicsBody[ropeList.Count]; for (int i = 0; i < ropeBodies.Length; i++) { ropeList[i].Mass = 0.01f; ropeList[i].Enabled = false; //only collide with the map ropeList[i].CollisionCategories = Physics.CollisionItem; ropeList[i].CollidesWith = Physics.CollisionWall; //ropeBodies[i] = new PhysicsBody(ropeList[i]); } List <RevoluteJoint> joints = PathManager.AttachBodiesWithRevoluteJoint(GameMain.World, ropeList, new Vector2(-sectionLength / 2, 0.0f), new Vector2(sectionLength / 2, 0.0f), false, false); ropeJoints = new RevoluteJoint[joints.Count + 1]; //ropeJoints[0] = JointFactory.CreateRevoluteJoint(Game1.world, item.body, ropeList[0], new Vector2(0f, -0.0f)); for (int i = 0; i < joints.Count; i++) { var distanceJoint = JointFactory.CreateDistanceJoint(GameMain.World, ropeList[i], ropeList[i + 1]); distanceJoint.Length = sectionLength; distanceJoint.DampingRatio = 1.0f; ropeJoints[i] = joints[i]; } }
private DominosTest() { Body b1 = BodyFactory.CreateEdge(World, new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); { Vertices box = PolygonTools.CreateRectangle(6.0f, 0.25f); PolygonShape shape = new PolygonShape(box, 0); Body ground = BodyFactory.CreateBody(World); ground.Position = new Vector2(-1.5f, 10.0f); ground.CreateFixture(shape); } { Vertices box = PolygonTools.CreateRectangle(0.1f, 1.0f); PolygonShape shape = new PolygonShape(box, 20); for (int i = 0; i < 10; ++i) { Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(-6.0f + 1.0f * i, 11.25f); Fixture fixture = body.CreateFixture(shape); fixture.Friction = 0.1f; } } { Vertices box = PolygonTools.CreateRectangle(7.0f, 0.25f, Vector2.Zero, 0.3f); PolygonShape shape = new PolygonShape(box, 0); Body ground = BodyFactory.CreateBody(World); ground.Position = new Vector2(1.0f, 6.0f); ground.CreateFixture(shape); } Body b2; { Vertices box = PolygonTools.CreateRectangle(0.25f, 1.5f); PolygonShape shape = new PolygonShape(box, 0); b2 = BodyFactory.CreateBody(World); b2.Position = new Vector2(-7.0f, 4.0f); b2.CreateFixture(shape); } Body b3; { Vertices box = PolygonTools.CreateRectangle(6.0f, 0.125f); PolygonShape shape = new PolygonShape(box, 10); b3 = BodyFactory.CreateBody(World); b3.BodyType = BodyType.Dynamic; b3.Position = new Vector2(-0.9f, 1.0f); b3.Rotation = -0.15f; b3.CreateFixture(shape); } Vector2 anchor = new Vector2(-2.0f, 1.0f); RevoluteJoint jd = new RevoluteJoint(b1, b3, anchor, true); jd.CollideConnected = true; World.AddJoint(jd); Body b4; { Vertices box = PolygonTools.CreateRectangle(0.25f, 0.25f); PolygonShape shape = new PolygonShape(box, 10); b4 = BodyFactory.CreateBody(World); b4.BodyType = BodyType.Dynamic; b4.Position = new Vector2(-10.0f, 15.0f); b4.CreateFixture(shape); } anchor = new Vector2(-7.0f, 15.0f); RevoluteJoint jd2 = new RevoluteJoint(b2, b4, anchor, true); World.AddJoint(jd2); Body b5; { b5 = BodyFactory.CreateBody(World); b5.BodyType = BodyType.Dynamic; b5.Position = new Vector2(6.5f, 3.0f); Vertices vertices = PolygonTools.CreateRectangle(1.0f, 0.1f, new Vector2(0.0f, -0.9f), 0.0f); PolygonShape shape = new PolygonShape(vertices, 10); Fixture fix = b5.CreateFixture(shape); fix.Friction = 0.1f; vertices = PolygonTools.CreateRectangle(0.1f, 1.0f, new Vector2(-0.9f, 0.0f), 0.0f); shape.Vertices = vertices; fix = b5.CreateFixture(shape); fix.Friction = 0.1f; vertices = PolygonTools.CreateRectangle(0.1f, 1.0f, new Vector2(0.9f, 0.0f), 0.0f); shape.Vertices = vertices; fix = b5.CreateFixture(shape); fix.Friction = 0.1f; } anchor = new Vector2(6.0f, 2.0f); RevoluteJoint jd3 = new RevoluteJoint(b1, b5, anchor, true); World.AddJoint(jd3); Body b6; { Vertices box = PolygonTools.CreateRectangle(1.0f, 0.1f); PolygonShape shape = new PolygonShape(box, 30); b6 = BodyFactory.CreateBody(World); b6.BodyType = BodyType.Dynamic; b6.Position = new Vector2(6.5f, 4.1f); b6.CreateFixture(shape); } anchor = new Vector2(7.50f, 4.0f); RevoluteJoint jd4 = new RevoluteJoint(b5, b6, anchor, true); jd4.CollideConnected = true; World.AddJoint(jd4); Body b7; { Vertices box = PolygonTools.CreateRectangle(0.1f, 1.0f); PolygonShape shape = new PolygonShape(box, 10); b7 = BodyFactory.CreateBody(World); b7.BodyType = BodyType.Dynamic; b7.Position = new Vector2(7.4f, 1.0f); b7.CreateFixture(shape); } DistanceJoint djd = new DistanceJoint(b3, b7, new Vector2(6.0f, 0.0f), new Vector2(0.0f, -1.0f)); World.AddJoint(djd); { const float radius = 0.2f; CircleShape shape = new CircleShape(radius, 10); for (int i = 0; i < 4; ++i) { Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(5.9f + 2.0f * radius * i, 2.4f); Fixture fix = body.CreateFixture(shape); fix.OnCollision += BallCollision; } } }
public override void LoadContent() { base.LoadContent(); World.Gravity = new Vector2(0f, 10f); HasCursor = false; EnableCameraControl = true; _hzFront = 8.5f; _hzBack = 5.0f; _zeta = 0.85f; _maxSpeed = 50.0f; // terrain _ground = new Body(World); { Vertices terrain = new Vertices(); terrain.Add(new Vector2(-20f, -5f)); terrain.Add(new Vector2(-20f, 0f)); terrain.Add(new Vector2(20f, 0f)); terrain.Add(new Vector2(25f, -0.25f)); terrain.Add(new Vector2(30f, -1f)); terrain.Add(new Vector2(35f, -4f)); terrain.Add(new Vector2(40f, 0f)); terrain.Add(new Vector2(45f, 0f)); terrain.Add(new Vector2(50f, 1f)); terrain.Add(new Vector2(55f, 2f)); terrain.Add(new Vector2(60f, 2f)); terrain.Add(new Vector2(65f, 1.25f)); terrain.Add(new Vector2(70f, 0f)); terrain.Add(new Vector2(75f, -0.3f)); terrain.Add(new Vector2(80f, -1.5f)); terrain.Add(new Vector2(85f, -3.5f)); terrain.Add(new Vector2(90f, 0f)); terrain.Add(new Vector2(95f, 0.5f)); terrain.Add(new Vector2(100f, 1f)); terrain.Add(new Vector2(105f, 2f)); terrain.Add(new Vector2(110f, 2.5f)); terrain.Add(new Vector2(115f, 1.3f)); terrain.Add(new Vector2(120f, 0f)); terrain.Add(new Vector2(160f, 0f)); terrain.Add(new Vector2(159f, 10f)); terrain.Add(new Vector2(201f, 10f)); terrain.Add(new Vector2(200f, 0f)); terrain.Add(new Vector2(240f, 0f)); terrain.Add(new Vector2(250f, -5f)); terrain.Add(new Vector2(250f, 10f)); terrain.Add(new Vector2(270f, 10f)); terrain.Add(new Vector2(270f, 0)); terrain.Add(new Vector2(310f, 0)); terrain.Add(new Vector2(310f, -5)); for (int i = 0; i < terrain.Count - 1; ++i) { FixtureFactory.AttachEdge(terrain[i], terrain[i + 1], _ground); } _ground.Friction = 0.6f; } // teeter board { _board = new Body(World); _board.BodyType = BodyType.Dynamic; _board.Position = new Vector2(140.0f, -1.0f); PolygonShape box = new PolygonShape(1f); box.Vertices = PolygonTools.CreateRectangle(10.0f, 0.25f); _teeter = new Sprite(ContentWrapper.TextureFromShape(box, "Stripe", ContentWrapper.Gold, ContentWrapper.Black, ContentWrapper.Black, 1f)); _board.CreateFixture(box); RevoluteJoint teeterAxis = JointFactory.CreateRevoluteJoint(World, _ground, _board, Vector2.Zero); teeterAxis.LowerLimit = -8.0f * Settings.Pi / 180.0f; teeterAxis.UpperLimit = 8.0f * Settings.Pi / 180.0f; teeterAxis.LimitEnabled = true; _board.ApplyAngularImpulse(-100.0f); } // bridge { _bridgeSegments = new List <Body>(); const int segmentCount = 20; PolygonShape shape = new PolygonShape(1f); shape.Vertices = PolygonTools.CreateRectangle(1.0f, 0.125f); _bridge = new Sprite(ContentWrapper.TextureFromShape(shape, ContentWrapper.Gold, ContentWrapper.Black)); Body prevBody = _ground; for (int i = 0; i < segmentCount; ++i) { Body body = new Body(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(161f + 2f * i, 0.125f); Fixture fix = body.CreateFixture(shape); fix.Friction = 0.6f; JointFactory.CreateRevoluteJoint(World, prevBody, body, -Vector2.UnitX); prevBody = body; _bridgeSegments.Add(body); } JointFactory.CreateRevoluteJoint(World, _ground, prevBody, Vector2.UnitX); } // boxes { _boxes = new List <Body>(); PolygonShape box = new PolygonShape(1f); box.Vertices = PolygonTools.CreateRectangle(0.5f, 0.5f); _box = new Sprite(ContentWrapper.TextureFromShape(box, "Square", ContentWrapper.Sky, ContentWrapper.Sunset, ContentWrapper.Black, 1f)); Body body = new Body(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(220f, -0.5f); body.CreateFixture(box); _boxes.Add(body); body = new Body(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(220f, -1.5f); body.CreateFixture(box); _boxes.Add(body); body = new Body(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(220f, -2.5f); body.CreateFixture(box); _boxes.Add(body); } // car { Vertices vertices = new Vertices(8); vertices.Add(new Vector2(-2.5f, 0.08f)); vertices.Add(new Vector2(-2.375f, -0.46f)); vertices.Add(new Vector2(-0.58f, -0.92f)); vertices.Add(new Vector2(0.46f, -0.92f)); vertices.Add(new Vector2(2.5f, -0.17f)); vertices.Add(new Vector2(2.5f, 0.205f)); vertices.Add(new Vector2(2.3f, 0.33f)); vertices.Add(new Vector2(-2.25f, 0.35f)); PolygonShape chassis = new PolygonShape(vertices, 2f); _car = new Body(World); _car.BodyType = BodyType.Dynamic; _car.Position = new Vector2(0.0f, -1.0f); _car.CreateFixture(chassis); _wheelBack = new Body(World); _wheelBack.BodyType = BodyType.Dynamic; _wheelBack.Position = new Vector2(-1.709f, -0.78f); Fixture fix = _wheelBack.CreateFixture(new CircleShape(0.5f, 0.8f)); fix.Friction = 0.9f; _wheelFront = new Body(World); _wheelFront.BodyType = BodyType.Dynamic; _wheelFront.Position = new Vector2(1.54f, -0.8f); _wheelFront.CreateFixture(new CircleShape(0.5f, 1f)); Vector2 axis = new Vector2(0.0f, -1.2f); _springBack = new WheelJoint(_car, _wheelBack, _wheelBack.Position, axis, true); _springBack.MotorSpeed = 0.0f; _springBack.MaxMotorTorque = 20.0f; _springBack.MotorEnabled = true; _springBack.Frequency = _hzBack; _springBack.DampingRatio = _zeta; World.AddJoint(_springBack); _springFront = new WheelJoint(_car, _wheelFront, _wheelFront.Position, axis, true); _springFront.MotorSpeed = 0.0f; _springFront.MaxMotorTorque = 10.0f; _springFront.MotorEnabled = false; _springFront.Frequency = _hzFront; _springFront.DampingRatio = _zeta; World.AddJoint(_springFront); // GFX _carBody = new Sprite(ContentWrapper.GetTexture("Car"), ContentWrapper.CalculateOrigin(_car)); _wheel = new Sprite(ContentWrapper.GetTexture("Wheel")); } Camera.MinRotation = -0.05f; Camera.MaxRotation = 0.05f; Camera.TrackingBody = _car; Camera.EnableTracking = true; }
public TheoJansenWalker(World world, Vector2 position) { _position = position; _motorSpeed = 2.0f; _motorOn = true; _leftShoulders = new Body[3]; _leftLegs = new Body[3]; _rightShoulders = new Body[3]; _rightLegs = new Body[3]; Vector2 pivot = new Vector2(0f, 0.8f); // Chassis PolygonShape box = new PolygonShape(1f); box.Vertices = PolygonTools.CreateRectangle(2.5f, 1.0f); _body = new Sprite(ContentWrapper.TextureFromShape(box, _walkerColors[0], ContentWrapper.Black, 24f)); _chassis = world.CreateBody(); _chassis.BodyType = BodyType.Dynamic; _chassis.Position = pivot + _position; Fixture bodyFixture = _chassis.CreateFixture(box); bodyFixture.CollisionGroup = -1; // Wheel CircleShape circle = new CircleShape(1.6f, 1f); _engine = new Sprite(ContentWrapper.TextureFromShape(circle, "Stripe", _walkerColors[1] * 0.6f, _walkerColors[2] * 0.8f, ContentWrapper.Black, 3f, 24f)); _wheel = world.CreateBody(); _wheel.BodyType = BodyType.Dynamic; _wheel.Position = pivot + _position; Fixture wheelFixture = _wheel.CreateFixture(circle); wheelFixture.CollisionGroup = -1; // Physics _motorJoint = new RevoluteJoint(_wheel, _chassis, _chassis.Position, true); _motorJoint.CollideConnected = false; _motorJoint.MotorSpeed = _motorSpeed; _motorJoint.MaxMotorTorque = 400f; _motorJoint.MotorEnabled = _motorOn; world.Add(_motorJoint); Vector2 wheelAnchor = pivot + new Vector2(0f, -0.8f); CreateLeg(world, -1f, wheelAnchor, out _leftShoulders[0], out _leftLegs[0]); CreateLeg(world, 1f, wheelAnchor, out _rightShoulders[0], out _rightLegs[0]); _wheel.SetTransform(_wheel.Position, 120f * MathHelper.Pi / 180f); CreateLeg(world, -1f, wheelAnchor, out _leftShoulders[1], out _leftLegs[1]); CreateLeg(world, 1f, wheelAnchor, out _rightShoulders[1], out _rightLegs[1]); _wheel.SetTransform(_wheel.Position, -120f * MathHelper.Pi / 180f); CreateLeg(world, -1f, wheelAnchor, out _leftShoulders[2], out _leftLegs[2]); CreateLeg(world, 1f, wheelAnchor, out _rightShoulders[2], out _rightLegs[2]); CreateLegTextures(); }
private GearsTest() { Body ground = BodyFactory.CreateEdge(World, new Vector2(50.0f, 0.0f), new Vector2(-50.0f, 0.0f)); { CircleShape circle1 = new CircleShape(1.0f, 5f); PolygonShape box = new PolygonShape(5f); box.Vertices = PolygonTools.CreateRectangle(0.5f, 5.0f); CircleShape circle2 = new CircleShape(2.0f, 5f); Body body1 = BodyFactory.CreateBody(World, new Vector2(10.0f, 9.0f)); body1.CreateFixture(circle1); Body body2 = BodyFactory.CreateBody(World, new Vector2(10.0f, 8.0f)); body2.BodyType = BodyType.Dynamic; body2.CreateFixture(box); Body body3 = BodyFactory.CreateBody(World, new Vector2(10.0f, 6.0f)); body3.BodyType = BodyType.Dynamic; body3.CreateFixture(circle2); RevoluteJoint joint1 = new RevoluteJoint(body2, body1, body1.Position, true); World.AddJoint(joint1); RevoluteJoint joint2 = new RevoluteJoint(body2, body3, body3.Position, true); World.AddJoint(joint2); GearJoint joint4 = new GearJoint(body1, body3, joint1, joint2, circle2.Radius / circle1.Radius); World.AddJoint(joint4); } { CircleShape circle1 = new CircleShape(1.0f, 5.0f); CircleShape circle2 = new CircleShape(2.0f, 5.0f); PolygonShape box = new PolygonShape(5f); box.Vertices = PolygonTools.CreateRectangle(0.5f, 5.0f); Body body1 = BodyFactory.CreateBody(World, new Vector2(-3.0f, 12.0f)); body1.BodyType = BodyType.Dynamic; body1.CreateFixture(circle1); _joint1 = new RevoluteJoint(ground, body1, body1.Position, true); _joint1.ReferenceAngle = body1.Rotation - ground.Rotation; World.AddJoint(_joint1); Body body2 = BodyFactory.CreateBody(World, new Vector2(0.0f, 12.0f)); body2.BodyType = BodyType.Dynamic; body2.CreateFixture(circle2); _joint2 = new RevoluteJoint(ground, body2, body2.Position, true); World.AddJoint(_joint2); Body body3 = BodyFactory.CreateBody(World, new Vector2(2.5f, 12.0f)); body3.BodyType = BodyType.Dynamic; body3.CreateFixture(box); _joint3 = new PrismaticJoint(ground, body3, body3.Position, new Vector2(0.0f, 1.0f), true); _joint3.LowerLimit = -5.0f; _joint3.UpperLimit = 5.0f; _joint3.LimitEnabled = true; World.AddJoint(_joint3); _joint4 = new GearJoint(body1, body2, _joint1, _joint2, circle2.Radius / circle1.Radius); World.AddJoint(_joint4); _joint5 = new GearJoint(body2, body3, _joint2, _joint3, -1.0f / circle2.Radius); World.AddJoint(_joint5); } }
private CCDTest() { { Body body = BodyFactory.CreateBody(World); body.Position = new Vector2(0.0f, -0.2f); Vertices box = PolygonTools.CreateRectangle(10, 0.2f); PolygonShape shape = new PolygonShape(box, 0); body.CreateFixture(shape); box = PolygonTools.CreateRectangle(0.2f, 1.0f, new Vector2(0.5f, 1.2f), 0.0f); shape.Set(box); body.CreateFixture(shape); } { Vertices box = PolygonTools.CreateRectangle(2, 0.1f); PolygonShape shape = new PolygonShape(box, 1); _angularVelocity = Rand.RandomFloat(-50.0f, 50.0f); _angularVelocity = -30.669577f; Body body = BodyFactory.CreateBody(World); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(0.0f, 20.0f); Fixture fixture = body.CreateFixture(shape); fixture.Restitution = 0.0f; body.LinearVelocity = new Vector2(0.0f, -100.0f); body.AngularVelocity = _angularVelocity; } #if false { FixtureDef fd = new FixtureDef(); fd.SetAsBox(10.0f, 0.1f); fd.density = 0.0f; bd.BodyType = BodyDef.e_static; bd.position = new Vector2(0.0f, -0.2f); Body ground = _world.CreateBody(); ground.CreateFixture(fd); } { FixtureDef fd = new FixtureDef(); fd.SetAsBox(2.0f, 0.1f); fd.density = 1.0f; fd.restitution = 0.0f; BodyDef bd1; bd1.type = BodyDef.e_dynamic; bd1.bullet = true; bd1.allowSleep = false; bd1.position = new Vector2(0.0f, 20.0f); Body b1 = _world.Create(bd1); b1.CreateFixture(fd); b1.SetLinearVelocity(new Vector2(0.0f, -100.0f)); fd.SetAsBox(1.0f, 0.1f); BodyDef bd2; bd2.type = BodyDef.e_dynamic; bd2.bullet = true; bd2.allowSleep = false; bd2.position = new Vector2(0.0f, 20.2f); Body b2 = _world.Create(bd2); b2.CreateFixture(fd); b2.SetLinearVelocity(new Vector2(0.0f, -100.0f)); fd.SetAsBox(0.25f, 0.25f); fd.density = 10.0f; BodyDef bd3; bd3.type = BodyDef.e_dynamic; bd3.bullet = true; bd3.allowSleep = false; bd3.position = new Vector2(0.0f, 100.0f); Body b3 = _world.Create(bd3); b3.CreateFixture(fd); b3.SetLinearVelocity(new Vector2(0.0f, -150.0f)); } #elif false float k_restitution = 1.4f; { bd.position = new Vector2(0.0f, 20.0f); Body body = _world.CreateBody(); FixtureDef fd = new FixtureDef(); fd.density = 0.0f; fd.restitution = k_restitution; fd.SetAsBox(0.1f, 10.0f, new Vector2(-10.0f, 0.0f), 0.0f); body.CreateFixture(fd); fd.SetAsBox(0.1f, 10.0f, new Vector2(10.0f, 0.0f), 0.0f); body.CreateFixture(fd); fd.SetAsBox(0.1f, 10.0f, new Vector2(0.0f, -10.0f), 0.5f * FarseerPhysics.Settings.b2_pi); body.CreateFixture(fd); fd.SetAsBox(0.1f, 10.0f, new Vector2(0.0f, 10.0f), -0.5f * FarseerPhysics.Settings.b2_pi); body.CreateFixture(fd); } #if false { FixtureDef sd_bottom; sd_bottom.SetAsBox(1.0f, 0.1f, new Vector2(0.0f, -1.0f), 0.0f); sd_bottom.density = 4.0f; FixtureDef sd_top; sd_top.SetAsBox(1.0f, 0.1f, new Vector2(0.0f, 1.0f), 0.0f); sd_top.density = 4.0f; FixtureDef sd_left; sd_left.SetAsBox(0.1f, 1.0f, new Vector2(-1.0f, 0.0f), 0.0f); sd_left.density = 4.0f; FixtureDef sd_right; sd_right.SetAsBox(0.1f, 1.0f, new Vector2(1.0f, 0.0f), 0.0f); sd_right.density = 4.0f; bd.BodyType = BodyDef.e_dynamicBody; bd.position = new Vector2(0.0f, 15.0f); Body body = _world.CreateBody(); body.CreateFixture(&sd_bottom); body.CreateFixture(&sd_top); body.CreateFixture(&sd_left); body.CreateFixture(&sd_right); } #elif false { FixtureDef sd_bottom; sd_bottom.SetAsBox(1.5f, 0.15f); sd_bottom.density = 4.0f; FixtureDef sd_left; sd_left.SetAsBox(0.15f, 2.7f, new Vector2(-1.45f, 2.35f), 0.2f); sd_left.density = 4.0f; FixtureDef sd_right; sd_right.SetAsBox(0.15f, 2.7f, new Vector2(1.45f, 2.35f), -0.2f); sd_right.density = 4.0f; bd.position = new Vector2(0.0f, 15.0f); Body body = _world.CreateBody(); body.CreateFixture(&sd_bottom); body.CreateFixture(&sd_left); body.CreateFixture(&sd_right); } #else { bd.position = new Vector2(-5.0f, 20.0f); bd.bullet = true; Body body = _world.CreateBody(); body.SetAngularVelocity(Rand.RandomFloat(-50.0f, 50.0f)); FixtureDef fd = new FixtureDef(); fd.SetAsBox(0.1f, 4.0f); fd.density = 1.0f; fd.restitution = 0.0f; body.CreateFixture(fd); } #endif for (int i = 0; i < 0; ++i) { bd.position = new Vector2(0.0f, 15.0f + i); bd.bullet = true; Body body = _world.CreateBody(); body.SetAngularVelocity(Rand.RandomFloat(-50.0f, 50.0f)); FixtureDef fd = new FixtureDef(); fd.radius = 0.25f; fd.density = 1.0f; fd.restitution = 0.0f; body.CreateFixture(fd); } #endif }
public override void Initialize() { Settings.VelocityIterations = 2; Settings.PositionIterations = 4; //GEN GameInstance.ViewCenter = Vector2.Zero; var size = CCDirector.SharedDirector.WinSize; _worldSize = 2 * new Vector2(size.Width, 0); //GEN GameInstance.ConvertScreenToWorld(GameInstance.Window.ClientBounds.Width, 0); //Create a World using QuadTree constructor World = new World(new Vector2(0.0f, -10.0f), new AABB(-_worldSize / 2, _worldSize / 2)); //Create a World using DynamicTree constructor //World = new World(new Vector2(0.0f, -10.0f)); // //set up border // float halfWidth = _worldSize.X / 2 - 2f; float halfHeight = _worldSize.Y / 2 - 2f; Vertices borders = new Vertices(4); borders.Add(new Vector2(-halfWidth, halfHeight)); borders.Add(new Vector2(halfWidth, halfHeight)); borders.Add(new Vector2(halfWidth, -halfHeight)); borders.Add(new Vector2(-halfWidth, -halfHeight)); Body anchor = BodyFactory.CreateLoopShape(World, borders); anchor.CollisionCategories = Category.All; anchor.CollidesWith = Category.All; // //box // Vertices bigbox = PolygonTools.CreateRectangle(3f, 3f); PolygonShape bigshape = new PolygonShape(bigbox, 5); Body bigbody = BodyFactory.CreateBody(World); bigbody.BodyType = BodyType.Dynamic; bigbody.Position = Vector2.UnitX * 25; bigbody.CreateFixture(bigshape); // //populate // const int rad = 12; const float a = 0.6f; const float sep = 0.000f; Vector2 cent = Vector2.Zero; for (int y = -rad; y <= +rad; y++) { int xrad = (int)Math.Round(Math.Sqrt(rad * rad - y * y)); for (int x = -xrad; x <= +xrad; x++) { Vector2 pos = cent + new Vector2(x * (2 * a + sep), y * (2 * a + sep)); Body cBody = BodyFactory.CreateCircle(World, a, 55, pos); cBody.BodyType = BodyType.Dynamic; } } base.Initialize(); }
/// <summary> /// Build vertices to represent an axis-aligned box. /// </summary> /// <param name="halfWidth">The half-width.</param> /// <param name="halfHeight">The half-height.</param> public void SetAsBox(float halfWidth, float halfHeight) { Set(PolygonTools.CreateRectangle(halfWidth, halfHeight)); }
private SerializationTest() { Body ground = World.CreateEdge(new Vector2(-20, 0), new Vector2(20, 0)); //Friction and distance joint { Body bodyA = World.CreateBody(new Vector2(10, 25), 0, BodyType.Dynamic); bodyA.CreateCircle(1, 1.5f); Body bodyB = World.CreateBody(new Vector2(-1, 25), 0, BodyType.Dynamic); bodyB.CreateRectangle(1, 1, 1, Vector2.Zero); FrictionJoint frictionJoint = JointFactory.CreateFrictionJoint(World, bodyB, ground, Vector2.Zero); frictionJoint.CollideConnected = true; frictionJoint.MaxForce = 100; JointFactory.CreateDistanceJoint(World, bodyA, bodyB); } //Wheel joint { Vertices vertices = new Vertices(6); vertices.Add(new Vector2(-1.5f, -0.5f)); vertices.Add(new Vector2(1.5f, -0.5f)); vertices.Add(new Vector2(1.5f, 0.0f)); vertices.Add(new Vector2(0.0f, 0.9f)); vertices.Add(new Vector2(-1.15f, 0.9f)); vertices.Add(new Vector2(-1.5f, 0.2f)); Body carBody = World.CreateBody(new Vector2(0, 1), 0, BodyType.Dynamic); Fixture carFixture = carBody.CreatePolygon(vertices, 1); Body wheel1 = World.CreateBody(new Vector2(-1.0f, 0.35f), 0, BodyType.Dynamic); Fixture wheel1Fixture = wheel1.CreateCircle(0.4f, 1); wheel1Fixture.Friction = 0.9f; Body wheel2 = World.CreateBody(new Vector2(1.0f, 0.4f), 0, BodyType.Dynamic); Fixture wheel2Fixture = wheel2.CreateCircle(0.4f, 1); wheel2Fixture.Friction = 0.9f; Vector2 axis = new Vector2(0.0f, 1.0f); WheelJoint spring1 = JointFactory.CreateWheelJoint(World, carBody, wheel1, axis); spring1.MotorSpeed = 0.0f; spring1.MaxMotorTorque = 20.0f; spring1.MotorEnabled = true; spring1.Frequency = 4; spring1.DampingRatio = 0.7f; WheelJoint spring2 = JointFactory.CreateWheelJoint(World, carBody, wheel2, axis); spring2.MotorSpeed = 0.0f; spring2.MaxMotorTorque = 10.0f; spring2.MotorEnabled = false; spring2.Frequency = 4; spring2.DampingRatio = 0.7f; } //Prismatic joint { Body body = World.CreateBody(new Vector2(-10.0f, 10.0f), 0.5f * MathHelper.Pi, BodyType.Dynamic); body.CreateRectangle(2, 2, 5, Vector2.Zero); Vector2 axis = new Vector2(2.0f, 1.0f); axis.Normalize(); PrismaticJoint joint = JointFactory.CreatePrismaticJoint(World, ground, body, Vector2.Zero, axis); joint.MotorSpeed = 5.0f; joint.MaxMotorForce = 1000.0f; joint.MotorEnabled = true; joint.LowerLimit = -10.0f; joint.UpperLimit = 20.0f; joint.LimitEnabled = true; } // Pulley joint { Body body1 = World.CreateBody(new Vector2(-10.0f, 16.0f), 0, BodyType.Dynamic); body1.CreateRectangle(2, 4, 5, Vector2.Zero); Body body2 = World.CreateBody(new Vector2(10.0f, 16.0f), 0, BodyType.Dynamic); body2.CreateRectangle(2, 4, 5, Vector2.Zero); Vector2 anchor1 = new Vector2(-10.0f, 16.0f + 2.0f); Vector2 anchor2 = new Vector2(10.0f, 16.0f + 2.0f); Vector2 worldAnchor1 = new Vector2(-10.0f, 16.0f + 2.0f + 12.0f); Vector2 worldAnchor2 = new Vector2(10.0f, 16.0f + 2.0f + 12.0f); JointFactory.CreatePulleyJoint(World, body1, body2, anchor1, anchor2, worldAnchor1, worldAnchor2, 1.5f, true); } //Revolute joint { Body ball = World.CreateBody(new Vector2(5.0f, 30.0f), 0, BodyType.Dynamic); ball.CreateCircle(3.0f, 5.0f); Body polygonBody = World.CreateBody(new Vector2(10, 10), 0, BodyType.Dynamic); polygonBody.CreateRectangle(20, 0.4f, 2, Vector2.Zero); polygonBody.IsBullet = true; RevoluteJoint joint = JointFactory.CreateRevoluteJoint(World, ground, polygonBody, new Vector2(10, 0)); joint.LowerLimit = -0.25f * MathHelper.Pi; joint.UpperLimit = 0.0f * MathHelper.Pi; joint.LimitEnabled = true; } //Weld joint { PolygonShape shape = new PolygonShape(PolygonTools.CreateRectangle(0.5f, 0.125f), 20); Body prevBody = ground; for (int i = 0; i < 10; ++i) { Body body = World.CreateBody(new Vector2(-14.5f + 1.0f * i, 5.0f), 0, BodyType.Dynamic); body.CreateFixture(shape); Vector2 anchor = new Vector2(0.5f, 0); if (i == 0) { anchor = new Vector2(-15f, 5); } JointFactory.CreateWeldJoint(World, prevBody, body, anchor, new Vector2(-0.5f, 0)); prevBody = body; } } //Rope joint { World.CreateChain(new Vector2(-10, 10), new Vector2(-20, 10), 0.1f, 0.5f, 10, 0.1f, true); } //Angle joint { Body bA = World.CreateBody(new Vector2(-5, 4), (float)(Math.PI / 3), BodyType.Dynamic); Fixture fA = bA.CreateRectangle(4, 4, 1, Vector2.Zero); Body bB = World.CreateBody(new Vector2(5, 4), 0, BodyType.Dynamic); Fixture fB = bB.CreateRectangle(4, 4, 1, Vector2.Zero); AngleJoint joint = new AngleJoint(bA, bB); joint.TargetAngle = (float)Math.PI / 2; World.Add(joint); } //Motor joint { Body body = World.CreateBody(new Vector2(0, 35), 0, BodyType.Dynamic); Fixture fixture = body.CreateRectangle(4, 1, 2, Vector2.Zero); fixture.Friction = 0.6f; MotorJoint motorJoint = JointFactory.CreateMotorJoint(World, ground, body); motorJoint.MaxForce = 1000.0f; motorJoint.MaxTorque = 1000.0f; motorJoint.LinearOffset = new Vector2(0, 35); motorJoint.AngularOffset = (float)(Math.PI / 3f); } }
private CollisionProcessingTest() { //Ground BodyFactory.CreateEdge(World, new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); const float xLo = -5.0f; const float xHi = 5.0f; const float yLo = 2.0f; const float yHi = 35.0f; // Small triangle Vertices vertices = new Vertices(3); vertices.Add(new Vector2(-1.0f, 0.0f)); vertices.Add(new Vector2(1.0f, 0.0f)); vertices.Add(new Vector2(0.0f, 2.0f)); PolygonShape polygon = new PolygonShape(vertices, 1); Body body1 = BodyFactory.CreateBody(World); body1.BodyType = BodyType.Dynamic; body1.Position = new Vector2(Rand.RandomFloat(xLo, xHi), Rand.RandomFloat(yLo, yHi)); Fixture fixture = body1.CreateFixture(polygon); fixture.OnCollision += OnCollision; // Large triangle (recycle definitions) vertices[0] *= 2.0f; vertices[1] *= 2.0f; vertices[2] *= 2.0f; polygon.Vertices = vertices; Body body2 = BodyFactory.CreateBody(World); body2.BodyType = BodyType.Dynamic; body2.Position = new Vector2(Rand.RandomFloat(xLo, xHi), Rand.RandomFloat(yLo, yHi)); fixture = body2.CreateFixture(polygon); fixture.OnCollision += OnCollision; // Small box Vertices smallBox = PolygonTools.CreateRectangle(1.0f, 0.5f); polygon.Vertices = smallBox; Body body3 = BodyFactory.CreateBody(World); body3.BodyType = BodyType.Dynamic; body3.Position = new Vector2(Rand.RandomFloat(xLo, xHi), Rand.RandomFloat(yLo, yHi)); fixture = body3.CreateFixture(polygon); fixture.OnCollision += OnCollision; // Large box (recycle definitions) Vertices largeBox = PolygonTools.CreateRectangle(2.0f, 1.0f); polygon.Vertices = largeBox; Body body4 = BodyFactory.CreateBody(World); body4.BodyType = BodyType.Dynamic; body4.Position = new Vector2(Rand.RandomFloat(xLo, xHi), Rand.RandomFloat(yLo, yHi)); fixture = body4.CreateFixture(polygon); fixture.OnCollision += OnCollision; // Small circle CircleShape circle = new CircleShape(1.0f, 1); Body body5 = BodyFactory.CreateBody(World); body5.BodyType = BodyType.Dynamic; body5.Position = new Vector2(Rand.RandomFloat(xLo, xHi), Rand.RandomFloat(yLo, yHi)); fixture = body5.CreateFixture(circle); fixture.OnCollision += OnCollision; // Large circle circle.Radius *= 2.0f; Body body6 = BodyFactory.CreateBody(World); body6.BodyType = BodyType.Dynamic; body6.Position = new Vector2(Rand.RandomFloat(xLo, xHi), Rand.RandomFloat(yLo, yHi)); fixture = body6.CreateFixture(circle); fixture.OnCollision += OnCollision; }
EdgeShapesTest() { // Ground body { Body ground = BodyFactory.CreateBody(World); double x1 = -20.0f; double y1 = 2.0f * (double)Math.Cos(x1 / 10.0f * (double)Math.PI); for (int i = 0; i < 80; ++i) { double x2 = x1 + 0.5f; double y2 = 2.0f * (double)Math.Cos(x2 / 10.0f * (double)Math.PI); EdgeShape shape = new EdgeShape(new Vector2(x1, y1), new Vector2(x2, y2)); ground.CreateFixture(shape); x1 = x2; y1 = y2; } } { Vertices vertices = new Vertices(3); vertices.Add(new Vector2(-0.5f, 0.0f)); vertices.Add(new Vector2(0.5f, 0.0f)); vertices.Add(new Vector2(0.0f, 1.5f)); _polygons[0] = new PolygonShape(vertices, 20); } { Vertices vertices = new Vertices(3); vertices.Add(new Vector2(-0.1f, 0.0f)); vertices.Add(new Vector2(0.1f, 0.0f)); vertices.Add(new Vector2(0.0f, 1.5f)); _polygons[1] = new PolygonShape(vertices, 20); } { const double w = 1.0f; double b = w / (2.0f + (double)Math.Sqrt(2.0f)); double s = (double)Math.Sqrt(2.0f) * b; Vertices vertices = new Vertices(8); vertices.Add(new Vector2(0.5f * s, 0.0f)); vertices.Add(new Vector2(0.5f * w, b)); vertices.Add(new Vector2(0.5f * w, b + s)); vertices.Add(new Vector2(0.5f * s, w)); vertices.Add(new Vector2(-0.5f * s, w)); vertices.Add(new Vector2(-0.5f * w, b + s)); vertices.Add(new Vector2(-0.5f * w, b)); vertices.Add(new Vector2(-0.5f * s, 0.0f)); _polygons[2] = new PolygonShape(vertices, 20); } { _polygons[3] = new PolygonShape(20); _polygons[3].Vertices = PolygonTools.CreateRectangle(0.5f, 0.5f); } { _circle = new CircleShape(0.5f, 1); } _bodyIndex = 0; _angle = 0.0f; }
private TheoJansenTest() { _offset = new Vector2(0.0f, 8.0f); _motorSpeed = 2.0f; _motorOn = true; Vector2 pivot = new Vector2(0.0f, 0.8f); // Ground { Body ground = World.CreateEdge(new Vector2(-50.0f, 0.0f), new Vector2(50.0f, 0.0f)); ground.CreateEdge(new Vector2(-50.0f, 0.0f), new Vector2(-50.0f, 10.0f)); ground.CreateEdge(new Vector2(50.0f, 0.0f), new Vector2(50.0f, 10.0f)); } // Balls for (int i = 0; i < 40; ++i) { CircleShape shape = new CircleShape(0.25f, 1); Body body = World.CreateBody(); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(-40.0f + 2.0f * i, 0.5f); body.CreateFixture(shape); } // Chassis { PolygonShape shape = new PolygonShape(1); shape.Vertices = PolygonTools.CreateRectangle(2.5f, 1.0f); _chassis = World.CreateBody(); _chassis.BodyType = BodyType.Dynamic; _chassis.Position = pivot + _offset; Fixture fixture = _chassis.CreateFixture(shape); fixture.CollisionGroup = -1; } { CircleShape shape = new CircleShape(1.6f, 1); _wheel = World.CreateBody(); _wheel.BodyType = BodyType.Dynamic; _wheel.Position = pivot + _offset; Fixture fixture = _wheel.CreateFixture(shape); fixture.CollisionGroup = -1; } { _motorJoint = new RevoluteJoint(_wheel, _chassis, _chassis.Position, true); _motorJoint.CollideConnected = false; _motorJoint.MotorSpeed = _motorSpeed; _motorJoint.MaxMotorTorque = 400.0f; _motorJoint.MotorEnabled = _motorOn; World.Add(_motorJoint); } Vector2 wheelAnchor = pivot + new Vector2(0.0f, -0.8f); CreateLeg(-1.0f, wheelAnchor); CreateLeg(1.0f, wheelAnchor); _wheel.SetTransform(_wheel.Position, 120.0f * MathHelper.Pi / 180.0f); CreateLeg(-1.0f, wheelAnchor); CreateLeg(1.0f, wheelAnchor); _wheel.SetTransform(_wheel.Position, -120.0f * MathHelper.Pi / 180.0f); CreateLeg(-1.0f, wheelAnchor); CreateLeg(1.0f, wheelAnchor); }
PinballTest() { // Ground body Body ground; { ground = BodyFactory.CreateBody(World); Vertices vertices = new Vertices(5); vertices.Add(new Vector2(0.0f, -2.0f)); vertices.Add(new Vector2(8.0f, 6.0f)); vertices.Add(new Vector2(8.0f, 20.0f)); vertices.Add(new Vector2(-8.0f, 20.0f)); vertices.Add(new Vector2(-8.0f, 6.0f)); ChainShape chain = new ChainShape(vertices, true); ground.CreateFixture(chain); } // Flippers { Vector2 p1 = new Vector2(-2.0f, 0f); Vector2 p2 = new Vector2(2.0f, 0f); Body leftFlipper = BodyFactory.CreateBody(World, p1); leftFlipper.BodyType = BodyType.Dynamic; Body rightFlipper = BodyFactory.CreateBody(World, p2); rightFlipper.BodyType = BodyType.Dynamic; PolygonShape box = new PolygonShape(1); box.Vertices = PolygonTools.CreateRectangle(1.75f, 0.1f); leftFlipper.CreateFixture(box); rightFlipper.CreateFixture(box); _leftJoint = new RevoluteJoint(ground, leftFlipper, p1, Vector2.Zero); _leftJoint.MaxMotorTorque = 1000.0f; _leftJoint.LimitEnabled = true; _leftJoint.MotorEnabled = true; _leftJoint.MotorSpeed = 0.0f; _leftJoint.LowerLimit = -30.0f * Alt.FarseerPhysics.Settings.Pi / 180.0f; _leftJoint.UpperLimit = 5.0f * Alt.FarseerPhysics.Settings.Pi / 180.0f; World.AddJoint(_leftJoint); _rightJoint = new RevoluteJoint(ground, rightFlipper, p2, Vector2.Zero); _rightJoint.MaxMotorTorque = 1000.0f; _rightJoint.LimitEnabled = true; _rightJoint.MotorEnabled = true; _rightJoint.MotorSpeed = 0.0f; _rightJoint.LowerLimit = -5.0f * Alt.FarseerPhysics.Settings.Pi / 180.0f; _rightJoint.UpperLimit = 30.0f * Alt.FarseerPhysics.Settings.Pi / 180.0f; World.AddJoint(_rightJoint); } // Circle character { _ball = BodyFactory.CreateBody(World, new Vector2(1.0f, 15.0f)); _ball.BodyType = BodyType.Dynamic; _ball.IsBullet = true; _ball.CreateFixture(new CircleShape(0.2f, 1.0f)); } }
private CompoundShapesTest() { World.CreateEdge(new Vector2(50.0f, 0.0f), new Vector2(-50.0f, 0.0f)); { CircleShape circle1 = new CircleShape(0.5f, 2); circle1.Position = new Vector2(-0.5f, 0.5f); CircleShape circle2 = new CircleShape(0.5f, 0); circle2.Position = new Vector2(0.5f, 0.5f); for (int i = 0; i < 10; ++i) { float x = Rand.RandomFloat(-0.1f, 0.1f); Body body = World.CreateBody(); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(x + 5.0f, 1.05f + 2.5f * i); body.Rotation = Rand.RandomFloat(-MathHelper.Pi, MathHelper.Pi); body.CreateFixture(circle1); body.CreateFixture(circle2); } } { Vertices box = PolygonTools.CreateRectangle(0.25f, 0.5f); PolygonShape polygon1 = new PolygonShape(box, 2); box = PolygonTools.CreateRectangle(0.25f, 0.5f, new Vector2(0.0f, -0.5f), 0.5f * MathHelper.Pi); PolygonShape polygon2 = new PolygonShape(box, 2); for (int i = 0; i < 10; ++i) { float x = Rand.RandomFloat(-0.1f, 0.1f); Body body = World.CreateBody(); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(x - 5.0f, 1.05f + 2.5f * i); body.Rotation = Rand.RandomFloat(-MathHelper.Pi, MathHelper.Pi); body.CreateFixture(polygon1); body.CreateFixture(polygon2); } } { Transform xf1 = new Transform(new Vector2(1.0f, 0.0f), Complex.One); Transform.Multiply(ref xf1, Complex.FromAngle(0.3524f * MathHelper.Pi), out xf1); Vertices vertices = new Vertices(3); vertices.Add(Transform.Multiply(new Vector2(-1.0f, 0.0f), ref xf1)); vertices.Add(Transform.Multiply(new Vector2(1.0f, 0.0f), ref xf1)); vertices.Add(Transform.Multiply(new Vector2(0.0f, 0.5f), ref xf1)); PolygonShape triangle1 = new PolygonShape(vertices, 2); Transform xf2 = new Transform(new Vector2(-1.0f, 0.0f), Complex.One); Transform.Multiply(ref xf2, Complex.FromAngle(-0.3524f * MathHelper.Pi), out xf2); vertices[0] = Transform.Multiply(new Vector2(-1.0f, 0.0f), ref xf2); vertices[1] = Transform.Multiply(new Vector2(1.0f, 0.0f), ref xf2); vertices[2] = Transform.Multiply(new Vector2(0.0f, 0.5f), ref xf2); PolygonShape triangle2 = new PolygonShape(vertices, 2); for (int i = 0; i < 10; ++i) { float x = Rand.RandomFloat(-0.1f, 0.1f); Body body = World.CreateBody(); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(x, 2.05f + 2.5f * i); body.CreateFixture(triangle1); body.CreateFixture(triangle2); } } { Vertices box = PolygonTools.CreateRectangle(1.5f, 0.15f); PolygonShape bottom = new PolygonShape(box, 4); box = PolygonTools.CreateRectangle(0.15f, 2.7f, new Vector2(-1.45f, 2.35f), 0.2f); PolygonShape left = new PolygonShape(box, 4); box = PolygonTools.CreateRectangle(0.15f, 2.7f, new Vector2(1.45f, 2.35f), -0.2f); PolygonShape right = new PolygonShape(box, 4); Body body = World.CreateBody(); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(0.0f, 2.0f); body.CreateFixture(bottom); body.CreateFixture(left); body.CreateFixture(right); } }
public Objects(World world, Vector2 startPosition, Vector2 endPosition, int count, float radius, ObjectType type, float toothHeight = 1f) { _bodyRadius = radius; BodyList = new List <Body>(count); for (int i = 0; i < count; i++) { switch (type) { case ObjectType.Circle: BodyList.Add(world.CreateCircle(radius, 1f)); break; case ObjectType.Rectangle: BodyList.Add(world.CreateRectangle(radius, radius, 1f)); _bodyRadius = radius / 2f; break; case ObjectType.Star: BodyList.Add(world.CreateGear(radius, 10, 0f, toothHeight, 1f)); _bodyRadius = radius * 2.7f; break; case ObjectType.Gear: BodyList.Add(world.CreateGear(radius, 10, 100f, toothHeight, 1f)); _bodyRadius = radius * 2.7f; break; } } for (int i = 0; i < BodyList.Count; i++) { Body body = BodyList[i]; body.BodyType = BodyType.Dynamic; body.Position = Vector2.Lerp(startPosition, endPosition, i / (float)(count - 1)); foreach (Fixture fixture in body.FixtureList) { fixture.Restitution = 0.7f; fixture.Friction = 0.2f; } } //GFX switch (type) { case ObjectType.Circle: _object = new Sprite(ContentWrapper.CircleTexture(radius, ContentWrapper.Gold, ContentWrapper.Grey, 24f)); break; case ObjectType.Rectangle: _object = new Sprite(ContentWrapper.PolygonTexture(PolygonTools.CreateRectangle(radius / 2f, radius / 2f), ContentWrapper.Red, ContentWrapper.Grey, 24f)); break; case ObjectType.Star: _object = new Sprite(ContentWrapper.PolygonTexture(PolygonTools.CreateGear(radius, 10, 0f, toothHeight), ContentWrapper.Brown, ContentWrapper.Black, 24f)); break; case ObjectType.Gear: _object = new Sprite(ContentWrapper.PolygonTexture(PolygonTools.CreateGear(radius, 10, 100f, toothHeight), ContentWrapper.Orange, ContentWrapper.Grey, 24f)); break; } }
public FSCollisionBox(float width, float height) { _width = width; _height = height; _verts = PolygonTools.CreateRectangle(FSConvert.DisplayToSim * _width / 2, FSConvert.DisplayToSim * _height / 2); }
public override void LoadContent() { base.LoadContent(); World.Gravity = new Vector2(0f, -10f); HasCursor = false; EnableCameraControl = true; HasVirtualStick = true; // terrain _ground = World.CreateBody(); { Vertices terrain = new Vertices(); terrain.Add(new Vector2(-20f, 5f)); terrain.Add(new Vector2(-20f, 0f)); terrain.Add(new Vector2(20f, 0f)); terrain.Add(new Vector2(25f, 0.25f)); terrain.Add(new Vector2(30f, 1f)); terrain.Add(new Vector2(35f, 4f)); terrain.Add(new Vector2(40f, 0f)); terrain.Add(new Vector2(45f, 0f)); terrain.Add(new Vector2(50f, -1f)); terrain.Add(new Vector2(55f, -2f)); terrain.Add(new Vector2(60f, -2f)); terrain.Add(new Vector2(65f, -1.25f)); terrain.Add(new Vector2(70f, 0f)); terrain.Add(new Vector2(75f, 0.3f)); terrain.Add(new Vector2(80f, 1.5f)); terrain.Add(new Vector2(85f, 3.5f)); terrain.Add(new Vector2(90f, 0f)); terrain.Add(new Vector2(95f, -0.5f)); terrain.Add(new Vector2(100f, -1f)); terrain.Add(new Vector2(105f, -2f)); terrain.Add(new Vector2(110f, -2.5f)); terrain.Add(new Vector2(115f, -1.3f)); terrain.Add(new Vector2(120f, 0f)); terrain.Add(new Vector2(160f, 0f)); terrain.Add(new Vector2(159f, -10f)); terrain.Add(new Vector2(201f, -10f)); terrain.Add(new Vector2(200f, 0f)); terrain.Add(new Vector2(240f, 0f)); terrain.Add(new Vector2(250f, 5f)); terrain.Add(new Vector2(250f, -10f)); terrain.Add(new Vector2(270f, -10f)); terrain.Add(new Vector2(270f, 0)); terrain.Add(new Vector2(310f, 0)); terrain.Add(new Vector2(310f, 5)); for (int i = 0; i < terrain.Count - 1; ++i) { _ground.CreateEdge(terrain[i], terrain[i + 1]); } _ground.SetFriction(0.6f); } // teeter board { _board = World.CreateBody(); _board.BodyType = BodyType.Dynamic; _board.Position = new Vector2(140.0f, 1.0f); PolygonShape box = new PolygonShape(PolygonTools.CreateRectangle(20.0f / 2f, 0.5f / 2f), 1); _teeter = new Sprite(ScreenManager.Assets.TextureFromShape(box, MaterialType.Pavement, Color.LightGray, 1.2f)); _board.CreateFixture(box); RevoluteJoint teeterAxis = JointFactory.CreateRevoluteJoint(World, _ground, _board, Vector2.Zero); teeterAxis.LowerLimit = -8.0f * MathHelper.Pi / 180.0f; teeterAxis.UpperLimit = 8.0f * MathHelper.Pi / 180.0f; teeterAxis.LimitEnabled = true; _board.ApplyAngularImpulse(100.0f); } // bridge { _bridgeSegments = new List <Body>(); const int segmentCount = 20; PolygonShape shape = new PolygonShape(PolygonTools.CreateRectangle(1.0f, 0.125f), 1f); _bridge = new Sprite(ScreenManager.Assets.TextureFromShape(shape, MaterialType.Dots, Color.SandyBrown, 1f)); Body prevBody = _ground; for (int i = 0; i < segmentCount; ++i) { Body body = World.CreateBody(); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(161f + 2f * i, -0.125f); Fixture fix = body.CreateFixture(shape); fix.Friction = 0.6f; JointFactory.CreateRevoluteJoint(World, prevBody, body, -Vector2.UnitX); prevBody = body; _bridgeSegments.Add(body); } JointFactory.CreateRevoluteJoint(World, _ground, prevBody, Vector2.UnitX); } // boxes { _boxes = new List <Body>(); PolygonShape box = new PolygonShape(PolygonTools.CreateRectangle(0.5f, 0.5f), 1f); _box = new Sprite(ScreenManager.Assets.TextureFromShape(box, MaterialType.Squares, Color.SaddleBrown, 2f)); Body body = World.CreateBody(); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(220f, 0.5f); body.CreateFixture(box); _boxes.Add(body); body = World.CreateBody(); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(220f, 1.5f); body.CreateFixture(box); _boxes.Add(body); body = World.CreateBody(); body.BodyType = BodyType.Dynamic; body.Position = new Vector2(220f, 2.5f); body.CreateFixture(box); _boxes.Add(body); } // car { Vertices vertices = new Vertices(8); vertices.Add(new Vector2(-2.5f, -0.08f)); vertices.Add(new Vector2(-2.375f, 0.46f)); vertices.Add(new Vector2(-0.58f, 0.92f)); vertices.Add(new Vector2(0.46f, 0.92f)); vertices.Add(new Vector2(2.5f, 0.17f)); vertices.Add(new Vector2(2.5f, -0.205f)); vertices.Add(new Vector2(2.3f, -0.33f)); vertices.Add(new Vector2(-2.25f, -0.35f)); PolygonShape chassis = new PolygonShape(vertices, 2); CircleShape wheelShape = new CircleShape(0.5f, 0.8f); _car = World.CreateBody(); _car.BodyType = BodyType.Dynamic; _car.Position = new Vector2(0.0f, 1.0f); _car.CreateFixture(chassis); _wheelBack = World.CreateBody(); _wheelBack.BodyType = BodyType.Dynamic; _wheelBack.Position = new Vector2(-1.709f, 0.78f); _wheelBack.CreateFixture(wheelShape); _wheelBack.SetFriction(0.9f); wheelShape.Density = 1; _wheelFront = World.CreateBody(); _wheelFront.BodyType = BodyType.Dynamic; _wheelFront.Position = new Vector2(1.54f, 0.8f); _wheelFront.CreateFixture(wheelShape); Vector2 axis = new Vector2(0.0f, 1.2f); _springBack = new WheelJoint(_car, _wheelBack, _wheelBack.Position, axis, true); _springBack.MotorSpeed = 0.0f; _springBack.MaxMotorTorque = 20.0f; _springBack.MotorEnabled = true; _springBack.Frequency = 4.0f; _springBack.DampingRatio = 0.7f; World.Add(_springBack); _springFront = new WheelJoint(_car, _wheelFront, _wheelFront.Position, axis, true); _springFront.MotorSpeed = 0.0f; _springFront.MaxMotorTorque = 10.0f; _springFront.MotorEnabled = false; _springFront.Frequency = 4.0f; _springFront.DampingRatio = 0.7f; World.Add(_springFront); _carBody = new Sprite(ScreenManager.Content.Load <Texture2D>("Samples/car"), AssetCreator.CalculateOrigin(_car, 24f)); _wheel = new Sprite(ScreenManager.Content.Load <Texture2D>("Samples/wheel")); } Camera.MinRotation = -0.05f; Camera.MaxRotation = 0.05f; Camera.TrackingBody = _car; Camera.EnableTracking = true; }