本文整理汇总了C#中FarseerPhysics.Collision.Shapes.PolygonShape.Set方法的典型用法代码示例。如果您正苦于以下问题:C# PolygonShape.Set方法的具体用法?C# PolygonShape.Set怎么用?C# PolygonShape.Set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类FarseerPhysics.Collision.Shapes.PolygonShape
的用法示例。
在下文中一共展示了PolygonShape.Set方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConfinedTest
private ConfinedTest()
{
{
Body ground = BodyFactory.CreateBody(World);
// Floor
Vertices edge = PolygonTools.CreateEdge(new Vector2(-10.0f, 0.0f), new Vector2(10.0f, 0.0f));
PolygonShape shape = new PolygonShape(edge, 0);
ground.CreateFixture(shape);
// Left wall
shape.Set(PolygonTools.CreateEdge(new Vector2(-10.0f, 0.0f), new Vector2(-10.0f, 20.0f)));
ground.CreateFixture(shape);
// Right wall
shape.Set(PolygonTools.CreateEdge(new Vector2(10.0f, 0.0f), new Vector2(10.0f, 20.0f)));
ground.CreateFixture(shape);
// Roof
shape.Set(PolygonTools.CreateEdge(new Vector2(-10.0f, 20.0f), new Vector2(10.0f, 20.0f)));
ground.CreateFixture(shape);
}
const float radius = 0.5f;
CircleShape shape2 = new CircleShape(radius, 1);
shape2.Position = Vector2.Zero;
for (int j = 0; j < ColumnCount; ++j)
{
for (int i = 0; i < RowCount; ++i)
{
Body body = BodyFactory.CreateBody(World);
body.BodyType = BodyType.Dynamic;
body.Position = new Vector2(-10.0f + (2.1f*j + 1.0f + 0.01f*i)*radius,
(2.0f*i + 1.0f)*radius);
Fixture fixture = body.CreateFixture(shape2);
fixture.Friction = 0.1f;
}
}
World.Gravity = Vector2.Zero;
}
示例2: CircleBenchmarkTest
private CircleBenchmarkTest()
{
Body ground = BodyFactory.CreateBody(World);
// Floor
Vertices edge = PolygonTools.CreateEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));
PolygonShape ashape = new PolygonShape(edge, 0);
ground.CreateFixture(ashape);
// Left wall
ashape.Set(PolygonTools.CreateEdge(new Vector2(-40.0f, 0.0f), new Vector2(-40.0f, 45.0f)));
ground.CreateFixture(ashape);
// Right wall
ashape.Set(PolygonTools.CreateEdge(new Vector2(40.0f, 0.0f), new Vector2(40.0f, 45.0f)));
ground.CreateFixture(ashape);
// Roof
ashape.Set(PolygonTools.CreateEdge(new Vector2(-40.0f, 45.0f), new Vector2(40.0f, 45.0f)));
ground.CreateFixture(ashape);
CircleShape shape = new CircleShape(1.0f, 1);
for (int i = 0; i < XCount; i++)
{
for (int j = 0; j < YCount; ++j)
{
Body body = BodyFactory.CreateBody(World);
body.BodyType = BodyType.Dynamic;
body.Position = new Vector2(-38f + 2.1f*i, 2.0f + 2.0f*j);
body.CreateFixture(shape);
}
}
}
示例3: EdgeShapeBenchmark
private EdgeShapeBenchmark()
{
// Ground body
{
Body ground = BodyFactory.CreateBody(World);
float x1 = -20.0f;
float y1 = 2.0f * (float)Math.Cos(x1 / 10.0f * (float)Math.PI);
for (int i = 0; i < 80; ++i)
{
float x2 = x1 + 0.5f;
float y2 = 2.0f * (float)Math.Cos(x2 / 10.0f * (float)Math.PI);
EdgeShape shape = new EdgeShape(new Vector2(x1, y1), new Vector2(x2, y2));
ground.CreateFixture(shape);
x1 = x2;
y1 = y2;
}
}
const float w = 1.0f;
const float t = 2.0f;
float b = w / (2.0f + (float)Math.Sqrt(t));
float s = (float)Math.Sqrt(t) * 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));
_polyShape = new PolygonShape(20);
_polyShape.Set(vertices);
}
示例4: Deserialize
public void Deserialize(World world, Stream stream)
{
world.Clear();
XMLFragmentElement root = XMLFragmentParser.LoadFromStream(stream);
if (root.Name.ToLower() != "world")
throw new Exception();
foreach (XMLFragmentElement main in root.Elements)
{
if (main.Name.ToLower() == "gravity")
{
world.Gravity = ReadVector(main);
}
}
foreach (XMLFragmentElement shapeElement in root.Elements)
{
if (shapeElement.Name.ToLower() == "shapes")
{
foreach (XMLFragmentElement n in shapeElement.Elements)
{
if (n.Name.ToLower() != "shape")
throw new Exception();
ShapeType type = (ShapeType)Enum.Parse(typeof(ShapeType), n.Attributes[0].Value, true);
switch (type)
{
case ShapeType.Circle:
{
CircleShape shape = new CircleShape();
foreach (XMLFragmentElement sn in n.Elements)
{
switch (sn.Name.ToLower())
{
case "radius":
shape.Radius = float.Parse(sn.Value);
break;
case "position":
shape.Position = ReadVector(sn);
break;
default:
throw new Exception();
}
}
_shapes.Add(shape);
}
break;
case ShapeType.Polygon:
{
PolygonShape shape = new PolygonShape();
foreach (XMLFragmentElement sn in n.Elements)
{
switch (sn.Name.ToLower())
{
case "vertices":
{
List<Vector2> verts = new List<Vector2>();
foreach (XMLFragmentElement vert in sn.Elements)
verts.Add(ReadVector(vert));
shape.Set(new Vertices(verts.ToArray()));
}
break;
case "centroid":
shape.MassData.Centroid = ReadVector(sn);
break;
}
}
_shapes.Add(shape);
}
break;
case ShapeType.Edge:
{
EdgeShape shape = new EdgeShape();
foreach (XMLFragmentElement sn in n.Elements)
{
switch (sn.Name.ToLower())
{
case "hasvertex0":
shape.HasVertex0 = bool.Parse(sn.Value);
break;
case "hasvertex3":
shape.HasVertex0 = bool.Parse(sn.Value);
break;
case "vertex0":
shape.Vertex0 = ReadVector(sn);
break;
case "vertex1":
shape.Vertex1 = ReadVector(sn);
break;
case "vertex2":
shape.Vertex2 = ReadVector(sn);
//.........这里部分代码省略.........
示例5: CollisionFilteringTest
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.Set(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,
triangleBody2.GetLocalPoint(body.Position),
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.SetAsBox(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.SetAsBox(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);
//.........这里部分代码省略.........
示例6: CreateLeg
private void CreateLeg(World world, float s, Vector2 wheelAnchor, int index)
{
Vector2 p1 = new Vector2(5.4f * s, 6.1f);
Vector2 p2 = new Vector2(7.2f * s, 1.2f);
Vector2 p3 = new Vector2(4.3f * s, 1.9f);
Vector2 p4 = new Vector2(3.1f * s, -0.8f);
Vector2 p5 = new Vector2(6.0f * s, -1.5f);
Vector2 p6 = new Vector2(2.5f * s, -3.7f);
PolygonShape poly1 = new PolygonShape(1f);
PolygonShape poly2 = new PolygonShape(2f);
Vertices vertices = new Vertices(3);
if (s < 0f)
{
vertices.Add(p1);
vertices.Add(p2);
vertices.Add(p3);
poly1.Set(vertices);
vertices[0] = Vector2.Zero;
vertices[1] = p5 - p4;
vertices[2] = p6 - p4;
poly2.Set(vertices);
}
else
{
vertices.Add(p1);
vertices.Add(p3);
vertices.Add(p2);
poly1.Set(vertices);
vertices[0] = Vector2.Zero;
vertices[1] = p6 - p4;
vertices[2] = p5 - p4;
poly2.Set(vertices);
}
Body body1 = BodyFactory.CreateBody(world);
body1.BodyType = BodyType.Dynamic;
body1.Position = _position;
body1.AngularDamping = 10f;
if (s < 0f)
{
_leftLegs[index] = body1;
}
else
{
_rightLegs[index] = body1;
}
Body body2 = BodyFactory.CreateBody(world);
body2.BodyType = BodyType.Dynamic;
body2.Position = p4 + _position;
body2.AngularDamping = 10f;
if (s < 0f)
{
_leftShoulders[index] = body2;
}
else
{
_rightShoulders[index] = body2;
}
Fixture f1 = body1.CreateFixture(poly1);
f1.CollisionGroup = -1;
Fixture f2 = body2.CreateFixture(poly2);
f2.CollisionGroup = -1;
// Using a soft distanceraint can reduce some jitter.
// It also makes the structure seem a bit more fluid by
// acting like a suspension system.
DistanceJoint djd = new DistanceJoint(body1, body2, body1.GetLocalPoint(p2 + _position),
body2.GetLocalPoint(p5 + _position));
djd.DampingRatio = 0.5f;
djd.Frequency = 10f;
world.AddJoint(djd);
_walkerJoints.Add(djd);
DistanceJoint djd2 = new DistanceJoint(body1, body2, body1.GetLocalPoint(p3 + _position),
body2.GetLocalPoint(p4 + _position));
djd2.DampingRatio = 0.5f;
djd2.Frequency = 10f;
world.AddJoint(djd2);
_walkerJoints.Add(djd2);
DistanceJoint djd3 = new DistanceJoint(body1, _wheel, body1.GetLocalPoint(p3 + _position),
_wheel.GetLocalPoint(wheelAnchor + _position));
djd3.DampingRatio = 0.5f;
djd3.Frequency = 10f;
world.AddJoint(djd3);
_walkerJoints.Add(djd3);
DistanceJoint djd4 = new DistanceJoint(body2, _wheel, body2.GetLocalPoint(p6 + _position),
_wheel.GetLocalPoint(wheelAnchor + _position));
//.........这里部分代码省略.........
示例7: CollisionProcessingTest
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.Set(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.Set(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.Set(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;
}
示例8: DominosTest
private DominosTest()
{
//Ground
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);
FixedRevoluteJoint jd = new FixedRevoluteJoint(b3, b3.GetLocalPoint(anchor), anchor);
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);
FixedRevoluteJoint jd2 = new FixedRevoluteJoint(b4, b4.GetLocalPoint(anchor), anchor);
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.Set(vertices);
//.........这里部分代码省略.........
示例9: LoadOrExtendMap
//.........这里部分代码省略.........
int tilesTall = y - firstY;
AddCollisionRectangle(
tileHalfSize * new Vector2(1.0f, tilesTall)
, new Vector2(tileSize.X * (x + 0.5f), tileSize.Y * (y - (float) tilesTall / 2))
);
}
}
}
// Create final collision.
if (hasCollision) {
int tilesTall = map.Height - firstY;
AddCollisionRectangle(
tileHalfSize * new Vector2(1.0f, tilesTall)
, new Vector2(tileSize.X * (x + 0.5f), tileSize.Y * (map.Height - (float) tilesTall / 2))
);
}
}
// Traverse map and create non-solid tile shapes.
for (int x = 0; x < map.Width; ++x) {
for (int y = 0; y < map.Height; ++y) {
switch (levelCollision[x, y]) {
case CollisionType.Solid:
// Already handled.
break;
case CollisionType.SlopeUp: {
Vector2 halfsize = UnitConverter.ToSimUnits(tileHalfSize);
Vector2 center = UnitConverter.ToSimUnits(new Vector2(tileSize.X * (x + 0.5f), tileSize.Y * (y + 0.5f)));
PolygonShape poly = new PolygonShape();
poly.Set(new FarseerPhysics.Common.Vertices(new Vector2[] {
center + new Vector2(-halfsize.X, halfsize.Y)
, center + new Vector2(halfsize.X, -halfsize.Y)
, center + new Vector2(halfsize.X, halfsize.Y)
}));
WorldBody.CreateFixture(poly);
break;
}
case CollisionType.HalfSlopeUp1: {
Vector2 halfsize = UnitConverter.ToSimUnits(tileHalfSize);
Vector2 center = UnitConverter.ToSimUnits(new Vector2(tileSize.X * (x + 0.5f), tileSize.Y * (y + 0.5f)));
PolygonShape poly = new PolygonShape();
poly.Set(new FarseerPhysics.Common.Vertices(new Vector2[] {
center + new Vector2(-halfsize.X, halfsize.Y)
, center + new Vector2(halfsize.X, 0.0f)
, center + new Vector2(halfsize.X, halfsize.Y)
}));
Fixture f = WorldBody.CreateFixture(poly);
break;
}
case CollisionType.HalfSlopeUp2: {
Vector2 halfsize = UnitConverter.ToSimUnits(tileHalfSize);
Vector2 center = UnitConverter.ToSimUnits(new Vector2(tileSize.X * (x + 0.5f), tileSize.Y * (y + 0.5f)));
PolygonShape poly = new PolygonShape();
poly.Set(new FarseerPhysics.Common.Vertices(new Vector2[] {
center + new Vector2(-halfsize.X, halfsize.Y)
, center + new Vector2(-halfsize.X, 0.0f)
, center + new Vector2(halfsize.X, -halfsize.Y)
, center + new Vector2(halfsize.X, halfsize.Y)
示例10: ApplyForceTest
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);
Vertices edge = PolygonTools.CreateEdge(new Vector2(-20.0f, -20.0f), new Vector2(-20.0f, 20.0f));
PolygonShape shape = new PolygonShape(edge, 0);
// Left vertical
Fixture fixture = ground.CreateFixture(shape);
fixture.Restitution = restitution;
// Right vertical
edge = PolygonTools.CreateEdge(new Vector2(20.0f, -20.0f), new Vector2(20.0f, 20.0f));
shape.Set(edge);
ground.CreateFixture(shape);
// Top horizontal
edge = PolygonTools.CreateEdge(new Vector2(-20.0f, 20.0f), new Vector2(20.0f, 20.0f));
shape.Set(edge);
ground.CreateFixture(shape);
// Bottom horizontal
edge = PolygonTools.CreateEdge(new Vector2(-20.0f, -20.0f), new Vector2(20.0f, -20.0f));
shape.Set(edge);
ground.CreateFixture(shape);
}
{
Transform xf1 = new Transform();
xf1.R.Set(0.3524f*Settings.Pi);
xf1.Position = MathUtils.Multiply(ref xf1.R, new Vector2(1.0f, 0.0f));
Vertices vertices = new Vertices(3);
vertices.Add(MathUtils.Multiply(ref xf1, new Vector2(-1.0f, 0.0f)));
vertices.Add(MathUtils.Multiply(ref xf1, new Vector2(1.0f, 0.0f)));
vertices.Add(MathUtils.Multiply(ref xf1, new Vector2(0.0f, 0.5f)));
PolygonShape poly1 = new PolygonShape(vertices, 4);
Transform xf2 = new Transform();
xf2.R.Set(-0.3524f*Settings.Pi);
xf2.Position = MathUtils.Multiply(ref xf2.R, new Vector2(-1.0f, 0.0f));
vertices[0] = MathUtils.Multiply(ref xf2, new Vector2(-1.0f, 0.0f));
vertices[1] = MathUtils.Multiply(ref xf2, new Vector2(1.0f, 0.0f));
vertices[2] = MathUtils.Multiply(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);
}
}
}
示例11: CreateShapesFromElement
public List<Shape> CreateShapesFromElement(World simulator, PhysicsSprite entity, PhysicsSprite.ShapeTypes shapeType)
{
List<Shape> shapeDefs = new List<Shape>();
switch (shapeType)
{
case PhysicsSprite.ShapeTypes.Ellipse:
xna.Vector2 worldRadius = ScreenSizeToWorldSize(new SysWin.Size(entity.Width / 2, entity.Width / 2));
CircleShape circleDef = new CircleShape(worldRadius.Y, 0.1f);
shapeDefs.Add(circleDef);
break;
case PhysicsSprite.ShapeTypes.Rectangle:
PolygonShape polygonRect = new PolygonShape(0.1f);
SysWin.Rect rect = new SysWin.Rect(0, 0, entity.Width, entity.Height);
xna.Vector2 pos = new xna.Vector2(0F, 0F);
// does the box have a rotation?
double angle = 0;
if (entity.RenderTransform is RotateTransform)
{
angle = (entity.RenderTransform as RotateTransform).Angle;
}
else
if (entity.RenderTransform is CompositeTransform)
{
angle = (entity.RenderTransform as CompositeTransform).Rotation;
}
float radAngle = (float)(Math.PI * angle / 180.0);
xna.Vector2 worldSize = ScreenSizeToWorldSize(new SysWin.Size(rect.Width / 2, rect.Height / 2));
//polygonRect.SetAsBox(worldSize.X, worldSize.Y, new xna.Vector2(0,0), radAngle);
polygonRect.SetAsBox(worldSize.X, worldSize.Y, new xna.Vector2(0, 0), 0);
shapeDefs.Add(polygonRect);
break;
default:
// Polygon via Path
Vertices vertices = new Vertices();
UIShapes.Path pathBoundary = (entity as Canvas).Children[0] as UIShapes.Path;
if (pathBoundary == null)
{
throw new Exception("The PhysicsSprite " + entity.Name + " has a Polygon ShapeType, but the first element in the PhysicsSprite must be a Path element that depicts the shape. Note you can set the Visibility of this Path to Collapsed if you do not want it shown in the Sprite.");
}
else
{
PathGeometry pathGeom = pathBoundary.Data as PathGeometry;
if (pathGeom == null)
{
throw new Exception("The PhysicsSprite " + entity.Name + " contains a Path as the first element, but that Path has no PathGeometry defined.");
}
else
{
#if WINDOWS_PHONE || SILVERLIGHT
PathConverter.StringToPathGeometryConverter cnv = new PathConverter.StringToPathGeometryConverter();
string geom = pathGeom.ToString();
if (geom != string.Empty)
{
pathGeom = cnv.Convert(geom);
}
#endif
foreach (PathFigure figure in pathGeom.Figures)
{
SysWin.Point ptStart = figure.StartPoint;
// adjust the position for origin at center
ptStart.X = (ptStart.X - ((float)entity.Width / 2));
ptStart.Y = (ptStart.Y - ((float)entity.Height / 2));
// adjust the position for Canvas Left, Top
ptStart.X = ptStart.X + Canvas.GetLeft(pathBoundary);
ptStart.Y = ptStart.Y + Canvas.GetTop(pathBoundary);
vertices.Add(ScreenSizeToWorldSize(ptStart));
foreach (PathSegment segment in figure.Segments)
{
if (segment is LineSegment)
{
LineSegment lineSegment = segment as LineSegment;
SysWin.Point ptNext = lineSegment.Point;
// adjust the position for origin at center
ptNext.X = (ptNext.X - ((float)entity.Width / 2));
ptNext.Y = (ptNext.Y - ((float)entity.Height / 2));
// adjust the position for Canvas Left, Top
ptNext.X = ptNext.X + Canvas.GetLeft(pathBoundary);
ptNext.Y = ptNext.Y + Canvas.GetTop(pathBoundary);
vertices.Add(ScreenSizeToWorldSize(ptNext));
}
else
if (segment is BezierSegment)
{
// HACK: This DOES NOT take into account a real Bezier Curve!!
BezierSegment bezSegment = segment as BezierSegment;
SysWin.Point ptNext = bezSegment.Point3;
// adjust the position for origin at center
ptNext.X = (ptNext.X + ((float)entity.Width / 2));
//.........这里部分代码省略.........