/// Initialize the proxy using the given shape. The shape /// must remain in scope while the proxy is in use. public void Set(Shape shape, int index) { switch (shape.ShapeType) { case ShapeType.Circle: { CircleShape circle = (CircleShape)shape; _vertices[0] = circle._p; _count = 1; _radius = circle._radius; } break; case ShapeType.Polygon: { PolygonShape polygon = (PolygonShape)shape; _vertices = polygon._vertices; _count = polygon._vertexCount; _radius = polygon._radius; } break; case ShapeType.Loop: { LoopShape loop = (LoopShape)shape; Debug.Assert(0 <= index && index < loop._count); _buffer[0] = loop._vertices[index]; if (index + 1 < loop._count) { _buffer[1] = loop._vertices[index + 1]; } else { _buffer[1] = loop._vertices[0]; } _vertices[0] = _buffer[0]; _vertices[1] = _buffer[1]; _count = 2; _radius = loop._radius; } break; case ShapeType.Edge: { EdgeShape edge = (EdgeShape)shape; _vertices[0] = edge._vertex1; _vertices[1] = edge._vertex2; _count = 2; _radius = edge._radius; } break; default: Debug.Assert(false); break; } }
/// Implement Shape. public override Shape Clone() { var loop = new LoopShape(); loop._count = _count; loop._radius = _radius; loop._vertices = (Vector2[])_vertices.Clone(); return loop; }
void DrawShape(Fixture fixture, Transform xf, Color color) { switch (fixture.ShapeType) { case ShapeType.Circle: { CircleShape circle = (CircleShape)fixture.GetShape(); Vector2 center = MathUtils.Multiply(ref xf, circle._p); float radius = circle._radius; Vector2 axis = xf.R.col1; DebugDraw.DrawSolidCircle(center, radius, axis, color); } break; case ShapeType.Polygon: { PolygonShape poly = (PolygonShape)fixture.GetShape(); int vertexCount = poly._vertexCount; Debug.Assert(vertexCount <= Settings.b2_maxPolygonVertices); FixedArray8 <Vector2> vertices = new FixedArray8 <Vector2>(); for (int i = 0; i < vertexCount; ++i) { vertices[i] = MathUtils.Multiply(ref xf, poly._vertices[i]); } DebugDraw.DrawSolidPolygon(ref vertices, vertexCount, color); } break; case ShapeType.Edge: { EdgeShape edge = (EdgeShape)fixture.GetShape(); Vector2 v1 = MathUtils.Multiply(ref xf, edge._vertex1); Vector2 v2 = MathUtils.Multiply(ref xf, edge._vertex2); DebugDraw.DrawSegment(v1, v2, color); } break; case ShapeType.Loop: { LoopShape loop = (LoopShape)fixture.GetShape(); int count = loop._count; Vector2 v1 = MathUtils.Multiply(ref xf, loop._vertices[count - 1]); for (int i = 0; i < count; ++i) { Vector2 v2 = MathUtils.Multiply(ref xf, loop._vertices[i]); DebugDraw.DrawSegment(v1, v2, color); v1 = v2; } } break; } }
/// <summary> Implement Shape. /// </summary> public override Shape Clone() { var loop = new LoopShape(); loop._count = _count; loop._radius = _radius; loop._vertices = (Vector2[])_vertices.Clone(); return(loop); }
public CharacterCollision() { // Ground body { BodyDef bd = new BodyDef(); Body ground = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsEdge(new Vector2(-20.0f, 0.0f), new Vector2(20.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } // Collinear edges { BodyDef bd = new BodyDef(); Body ground = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsEdge(new Vector2(-8.0f, 1.0f), new Vector2(-6.0f, 1.0f)); ground.CreateFixture(shape, 0.0f); shape.SetAsEdge(new Vector2(-6.0f, 1.0f), new Vector2(-4.0f, 1.0f)); ground.CreateFixture(shape, 0.0f); shape.SetAsEdge(new Vector2(-4.0f, 1.0f), new Vector2(-2.0f, 1.0f)); ground.CreateFixture(shape, 0.0f); } // Square tiles { BodyDef bd = new BodyDef(); Body ground = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsBox(1.0f, 1.0f, new Vector2(4.0f, 3.0f), 0.0f); ground.CreateFixture(shape, 0.0f); shape.SetAsBox(1.0f, 1.0f, new Vector2(6.0f, 3.0f), 0.0f); ground.CreateFixture(shape, 0.0f); shape.SetAsBox(1.0f, 1.0f, new Vector2(8.0f, 3.0f), 0.0f); ground.CreateFixture(shape, 0.0f); } // Square made from edges notice how the edges are shrunk to account // for the polygon radius. This makes it so the square character does // not get snagged. However, ray casts can now go through the cracks. for (int i = 0; i < 4; i++) { BodyDef bd = new BodyDef(); Body ground = _world.CreateBody(bd); ground.SetTransform(new Vector2(-2f * i, 0), 0); Vector2[] vs = new Vector2[4]; vs[0] = new Vector2(-1.0f, 3.0f); vs[1] = new Vector2(1.0f, 3.0f); vs[2] = new Vector2(1.0f, 5.0f); vs[3] = new Vector2(-1.0f, 5.0f); LoopShape shape = new LoopShape(); shape._count = 4; shape._vertices = vs; ground.CreateFixture(shape, 0.0f); //PolygonShape shape = new PolygonShape(); //float d = 2.0f * Settings.b2_polygonRadius; //shape.SetAsEdge(new Vector2(-1.0f + d, 3.0f), new Vector2(1.0f - d, 3.0f)); //ground.CreateFixture(shape, 0.0f); //shape.SetAsEdge(new Vector2(1.0f, 3.0f + d), new Vector2(1.0f, 5.0f - d)); //ground.CreateFixture(shape, 0.0f); //shape.SetAsEdge(new Vector2(1.0f - d, 5.0f), new Vector2(-1.0f + d, 5.0f)); //ground.CreateFixture(shape, 0.0f); //shape.SetAsEdge(new Vector2(-1.0f, 5.0f - d), new Vector2(-1.0f, 3.0f + d)); //ground.CreateFixture(shape, 0.0f); } // Square character { BodyDef bd = new BodyDef(); bd.position = new Vector2(-3.0f, 5.0f); bd.type = BodyType.Dynamic; bd.fixedRotation = true; bd.allowSleep = false; Body body = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.5f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; body.CreateFixture(fd); } #if false // Hexagon character { BodyDef bd = new BodyDef(); bd.position = new Vector2(-5.0f, 5.0f); bd.type = BodyType.Dynamic; bd.fixedRotation = true; bd.allowSleep = false; Body body = _world.CreateBody(bd); float angle = 0.0f; float delta = (float)Math.PI / 3.0f; Vector2[] vertices = new Vector2[6]; for (int i = 0; i < 6; ++i) { vertices[i] = new Vector2(0.5f * (float)Math.Cos(angle), 0.5f * (float)Math.Sin(angle)); angle += delta; } PolygonShape shape = new PolygonShape(); shape.Set(vertices, 6); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; body.CreateFixture(fd); } // Circle character { BodyDef bd = new BodyDef(); bd.position = new Vector2(3.0f, 5.0f); bd.type = BodyType.Dynamic; bd.fixedRotation = true; bd.allowSleep = false; Body body = _world.CreateBody(bd); CircleShape shape = new CircleShape(); shape._radius = 0.5f; FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; body.CreateFixture(fd); } #endif }