本文整理汇总了C#中FarseerPhysics.Collision.Shapes.PolygonShape类的典型用法代码示例。如果您正苦于以下问题:C# PolygonShape类的具体用法?C# PolygonShape怎么用?C# PolygonShape使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PolygonShape类属于FarseerPhysics.Collision.Shapes命名空间,在下文中一共展示了PolygonShape类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PyramidTest
private PyramidTest()
{
//Create ground
FixtureFactory.CreateEdge(World, new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));
Vertices box = PolygonTools.CreateRectangle(0.5f, 0.5f);
PolygonShape shape = new PolygonShape(box, 5);
Vector2 x = new Vector2(-7.0f, 0.75f);
Vector2 deltaX = new Vector2(0.5625f, 1.25f);
Vector2 deltaY = new Vector2(1.125f, 0.0f);
for (int i = 0; i < Count; ++i)
{
Vector2 y = x;
for (int j = i; j < Count; ++j)
{
Body body = BodyFactory.CreateBody(World);
body.BodyType = BodyType.Dynamic;
body.Position = y;
body.CreateFixture(shape);
y += deltaY;
}
x += deltaX;
}
}
示例2: Pyramid
public Pyramid(World world, Vector2 position, int count, float density)
{
Vertices rect = PolygonTools.CreateRectangle(0.5f, 0.5f);
PolygonShape shape = new PolygonShape(rect, density);
Vector2 rowStart = position;
rowStart.Y -= 0.5f + count * 1.1f;
Vector2 deltaRow = new Vector2(-0.625f, 1.1f);
const float spacing = 1.25f;
// Physics
_boxes = new List<Body>();
for (int i = 0; i < count; i++)
{
Vector2 pos = rowStart;
for (int j = 0; j < i + 1; j++)
{
Body body = BodyFactory.CreateBody(world);
body.BodyType = BodyType.Dynamic;
body.Position = pos;
body.CreateFixture(shape);
_boxes.Add(body);
pos.X += spacing;
}
rowStart += deltaRow;
}
//GFX
_box = new Sprite(ContentWrapper.PolygonTexture(rect, "Square", ContentWrapper.Blue, ContentWrapper.Gold, ContentWrapper.Black, 1f));
}
示例3: CreateChain
/// <summary>
/// Creates a chain.
/// </summary>
/// <param name="world">The world.</param>
/// <param name="start">The start.</param>
/// <param name="end">The end.</param>
/// <param name="linkWidth">The width.</param>
/// <param name="linkHeight">The height.</param>
/// <param name="fixStart">if set to <c>true</c> [fix start].</param>
/// <param name="fixEnd">if set to <c>true</c> [fix end].</param>
/// <param name="numberOfLinks">The number of links.</param>
/// <param name="linkDensity">The link density.</param>
/// <returns></returns>
public static Path CreateChain(World world, Vector2 start, Vector2 end, float linkWidth, float linkHeight, bool fixStart, bool fixEnd, int numberOfLinks, float linkDensity)
{
// Chain start / end
Path path = new Path();
path.Add(start);
path.Add(end);
// A single chainlink
PolygonShape shape = new PolygonShape(PolygonTools.CreateRectangle(linkWidth, linkHeight));
// Use PathManager to create all the chainlinks based on the chainlink created before.
List<Body> chainLinks = PathManager.EvenlyDistibuteShapesAlongPath(world, path, shape, BodyType.Dynamic, numberOfLinks, linkDensity);
if (fixStart)
{
// Fix the first chainlink to the world
JointFactory.CreateFixedRevoluteJoint(world, chainLinks[0], new Vector2(0, -(linkHeight / 2)), chainLinks[0].Position);
}
if (fixEnd)
{
// Fix the last chainlink to the world
JointFactory.CreateFixedRevoluteJoint(world, chainLinks[chainLinks.Count - 1], new Vector2(0, (linkHeight / 2)), chainLinks[chainLinks.Count - 1].Position);
}
// Attach all the chainlinks together with a revolute joint
PathManager.AttachBodiesWithRevoluteJoint(world, chainLinks, new Vector2(0, -linkHeight), new Vector2(0, linkHeight),
false, false);
return (path);
}
示例4: CreateBody
public override Body CreateBody(World world)
{
var unithull = new Vertices((Vector2[])this.ResourceDictionary.GetResource(_resource.CollisionHullKey));
var scaledVertices = new List<Vector2>();
foreach (var vertex in unithull)
{
scaledVertices.Add(new Vector2(vertex.X * _resource.Width, vertex.Y * _resource.Height));
}
var vertices = new Vertices(scaledVertices);
var body = new Body(world);
body.BodyType = BodyType.Dynamic;
body.IgnoreGravity = true;
body.LinearDamping = 0.0f;
body.FixedRotation = true;
body.Mass = 0;
var location = ((ILocatable)_actor).Location;
body.Position = new Vector2(location.X, location.Y);
var shape = new PolygonShape(vertices, 1f);
var fixture = body.CreateFixture(shape, 0f);
fixture.CollisionGroup = _resource.CollisionGroup;
fixture.UserData = new CollisionData { Actor = _actor };
fixture.OnCollision += OnCollision;
_body = body;
return body;
}
示例5: CreateBody
public override Body CreateBody(World world)
{
var unitHull = (Vector2[])ResourceDictionary.GetResource("UnitHull");
var scaledVertices = new List<Vector2>();
foreach (var vertex in unitHull)
{
scaledVertices.Add(new Vector2(vertex.X * _resource.Width, vertex.Y * _resource.Height));
}
var vertices = new Vertices(scaledVertices);
var body = new Body(world);
body.BodyType = BodyType.Static;
var location = ((ILocatable)_actor).Location;
body.Position = new Vector2(location.X, location.Y);
var shape = new PolygonShape(vertices, 1f);
var fixture = body.CreateFixture(shape, 0f);
fixture.UserData = new CollisionData { Actor = _actor };
fixture.CollisionGroup = _resource.CollisionGroup;
fixture.OnCollision += OnCollision;
_body = body;
return body;
}
示例6: Tumbler
private Tumbler()
{
var ground = BodyFactory.CreateBody(World);
var body = BodyFactory.CreateBody(World);
body.BodyType = BodyType.Dynamic;
body.Position = new Vector2(0.0f, 10.0f);
PolygonShape shape = new PolygonShape(5);
shape.SetAsBox(0.5f, 10.0f, new Vector2(10.0f, 0.0f), 0.0f);
body.CreateFixture(shape);
shape.SetAsBox(0.5f, 10.0f, new Vector2(-10.0f, 0.0f), 0.0f);
body.CreateFixture(shape);
shape.SetAsBox(10.0f, 0.5f, new Vector2(0.0f, 10.0f), 0.0f);
body.CreateFixture(shape);
shape.SetAsBox(10.0f, 0.5f, new Vector2(0.0f, -10.0f), 0.0f);
body.CreateFixture(shape);
var jd = new RevoluteJoint(ground, body, new Vector2(0.0f, 10.0f), new Vector2(0.0f, 0.0f));
jd.ReferenceAngle = 0.0f;
jd.MotorSpeed = 0.05f * MathHelper.Pi;
jd.MaxMotorTorque = 1e8f;
jd.MotorEnabled = true;
World.AddJoint(jd);
}
示例7: PulleysTest
private PulleysTest()
{
//Ground
Body ground = BodyFactory.CreateBody(World);
FixtureFactory.AttachCircle(2, 0, ground, new Vector2(-10.0f, Y + B + L));
FixtureFactory.AttachCircle(2, 0, ground, new Vector2(10.0f, Y + B + L));
{
PolygonShape shape = new PolygonShape(5);
shape.Vertices = PolygonTools.CreateRectangle(A, B);
Body body1 = BodyFactory.CreateBody(World);
body1.BodyType = BodyType.Dynamic;
body1.Position = new Vector2(-10.0f, Y);
body1.CreateFixture(shape);
Body body2 = BodyFactory.CreateBody(World);
body2.BodyType = BodyType.Dynamic;
body2.Position = new Vector2(10.0f, Y);
body2.CreateFixture(shape);
Vector2 anchor1 = new Vector2(-10.0f, Y + B);
Vector2 anchor2 = new Vector2(10.0f, Y + B);
Vector2 groundAnchor1 = new Vector2(-10.0f, Y + B + L);
Vector2 groundAnchor2 = new Vector2(10.0f, Y + B + L);
_joint1 = new PulleyJoint(body1, body2, anchor1, anchor2, groundAnchor1, groundAnchor2, 1.5f, true);
World.AddJoint(_joint1);
}
}
示例8: RopeTest
private RopeTest()
{
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);
}
{
Body prevBody = ground;
PolygonShape largeShape = new PolygonShape(PolygonTools.CreateRectangle(1.5f, 1.5f), 100);
PolygonShape smallShape = new PolygonShape(PolygonTools.CreateRectangle(0.5f, 0.125f), 20);
const int N = 10;
const float y = 15;
for (int i = 0; i < N; ++i)
{
Body body = BodyFactory.CreateBody(World);
body.BodyType = BodyType.Dynamic;
body.Position = new Vector2(0.5f + 1.0f * i, y);
if (i == N - 1)
{
Fixture fixture = body.CreateFixture(largeShape);
fixture.Friction = 0.2f;
fixture.CollisionCategories = Category.Cat2;
fixture.CollidesWith = Category.All & ~Category.Cat2;
body.Position = new Vector2(1.0f * i, y);
body.AngularDamping = 0.4f;
}
else
{
Fixture fixture = body.CreateFixture(smallShape);
fixture.Friction = 0.2f;
fixture.CollisionCategories = Category.Cat1;
fixture.CollidesWith = Category.All & ~Category.Cat2;
}
Vector2 anchor = new Vector2(i, y);
RevoluteJoint jd = new RevoluteJoint(prevBody, body, prevBody.GetLocalPoint(ref anchor),
body.GetLocalPoint(ref anchor));
jd.CollideConnected = false;
World.AddJoint(jd);
prevBody = body;
}
_rj = new RopeJoint(ground, prevBody, new Vector2(0, y), Vector2.Zero);
//FPE: The two following lines are actually not needed as FPE sets the MaxLength to a default value
const float extraLength = 0.01f;
_rj.MaxLength = N - 1.0f + extraLength;
World.AddJoint(_rj);
}
}
示例9: OneSidedPlatformTest
private OneSidedPlatformTest()
{
//Ground
BodyFactory.CreateEdge(World, new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));
// Platform
{
Body body = BodyFactory.CreateBody(World);
body.Position = new Vector2(0.0f, 10.0f);
PolygonShape shape = new PolygonShape(1);
shape.SetAsBox(3.0f, 0.5f);
_platform = body.CreateFixture(shape);
_top = 10.0f + 0.5f;
}
// Actor
{
Body body = BodyFactory.CreateBody(World);
body.BodyType = BodyType.Dynamic;
body.Position = new Vector2(0.0f, 12.0f);
_radius = 0.5f;
CircleShape shape = new CircleShape(_radius, 20);
_character = body.CreateFixture(shape);
body.LinearVelocity = new Vector2(0.0f, -50.0f);
}
}
示例10: Tank
protected Tank(
ISoundManager soundManager,
World world,
Collection<IDoodad> doodads,
Team team,
Vector2 position,
float rotation,
DoodadFactory doodadFactory)
{
this.soundManager = soundManager;
this.world = world;
this.doodadFactory = doodadFactory;
this.doodads = doodads;
this.body = BodyFactory.CreateBody(world, position, this);
this.body.Rotation = rotation;
this.body.BodyType = BodyType.Dynamic;
this.Team = team;
this.Heading = rotation;
this.activeMissiles = new List<Missile>();
this.powerup = PowerUpType.None;
var shape = new PolygonShape(0);
shape.SetAsBox(15 / Constants.PixelsPerMeter, 15 / Constants.PixelsPerMeter);
var fixture = this.body.CreateFixture(shape);
fixture.CollisionCategories = this.CollisionCategory;
fixture.CollidesWith = PhysicsConstants.MissileCategory;
if (this is PlayerControlledTank)
{
fixture.CollidesWith |= PhysicsConstants.ObstacleCategory | PhysicsConstants.PitCategory |
PhysicsConstants.SensorCategory;
}
}
示例11: LoadContent
public override void LoadContent()
{
base.LoadContent();
World.Gravity = new Vector2(0, 9.82f);
_border = new Border(World, this, ScreenManager.GraphicsDevice.Viewport);
/* Bridge */
//We make a path using 2 points.
Path bridgePath = new Path();
bridgePath.Add(new Vector2(-15, 5));
bridgePath.Add(new Vector2(15, 5));
bridgePath.Closed = false;
Vertices box = PolygonTools.CreateRectangle(0.125f, 0.5f);
PolygonShape shape = new PolygonShape(box, 20);
_bridgeBodies = PathManager.EvenlyDistributeShapesAlongPath(World, bridgePath, shape,
BodyType.Dynamic, 29);
_bridgeBox =
new Sprite(ScreenManager.Assets.TextureFromShape(shape, MaterialType.Dots, Color.SandyBrown, 1f));
//Attach the first and last fixtures to the world
JointFactory.CreateFixedRevoluteJoint(World, _bridgeBodies[0], new Vector2(0f, -0.5f),
_bridgeBodies[0].Position);
JointFactory.CreateFixedRevoluteJoint(World, _bridgeBodies[_bridgeBodies.Count - 1], new Vector2(0, 0.5f),
_bridgeBodies[_bridgeBodies.Count - 1].Position);
PathManager.AttachBodiesWithRevoluteJoint(World, _bridgeBodies, new Vector2(0f, -0.5f),
new Vector2(0f, 0.5f),
false, true);
/* Soft body */
//We make a rectangular path.
Path rectanglePath = new Path();
rectanglePath.Add(new Vector2(-6, -11));
rectanglePath.Add(new Vector2(-6, 1));
rectanglePath.Add(new Vector2(6, 1));
rectanglePath.Add(new Vector2(6, -11));
rectanglePath.Closed = true;
//Creating two shapes. A circle to form the circle and a rectangle to stabilize the soft body.
List<Shape> shapes = new List<Shape>(2);
shapes.Add(new PolygonShape(PolygonTools.CreateRectangle(0.5f, 0.5f, new Vector2(-0.1f, 0f), 0f), 1f));
shapes.Add(new CircleShape(0.5f, 1f));
//We distribute the shapes in the rectangular path.
_softBodies = PathManager.EvenlyDistributeShapesAlongPath(World, rectanglePath, shapes,
BodyType.Dynamic, 30);
_softBodyBox =
new Sprite(ScreenManager.Assets.TextureFromShape(shapes[0], MaterialType.Blank, Color.Silver * 0.8f, 1f));
_softBodyBox.Origin += new Vector2(ConvertUnits.ToDisplayUnits(0.1f), 0f);
_softBodyCircle =
new Sprite(ScreenManager.Assets.TextureFromShape(shapes[1], MaterialType.Waves, Color.Silver, 1f));
//Attach the bodies together with revolute joints. The rectangular form will converge to a circular form.
PathManager.AttachBodiesWithRevoluteJoint(World, _softBodies, new Vector2(0f, -0.5f), new Vector2(0f, 0.5f),
true, true);
}
示例12: CreateBody
public override Body CreateBody(World world)
{
Int32 count = 0;
foreach (var key in _resource.TileDefinitionKeys)
{
var tileDefinition = (TerrainTileDefinitionResource)ResourceDictionary.GetResource(key);
if (!String.IsNullOrEmpty(tileDefinition.CollisionHullKey))
{
var vertices = new Vertices((Vector2[])this.ResourceDictionary.GetResource(tileDefinition.CollisionHullKey));
var body = new Body(world);
_bodies.Add(body);
body.BodyType = BodyType.Static;
var location = ((ILocatable)_actor).Location;
body.Position = new Vector2(((count % _resource.Columns) * _resource.TileWidth + location.X) / Settings.MetersToPixels, ((Int32)(count / _resource.Columns) * _resource.TileHeight + location.Y) / Settings.MetersToPixels);
var shape = new PolygonShape(vertices, 0f);
var fixture = new Fixture(body, shape, 1.0f);
fixture.UserData = new CollisionData { Actor = _actor, Data = tileDefinition.CollisionData };
}
count++;
}
return null;
}
示例13: PulleysTest
private PulleysTest()
{
//Ground
FixtureFactory.CreateEdge(World, new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));
{
const float a = 2.0f;
const float b = 4.0f;
const float y = 16.0f;
const float l = 12.0f;
PolygonShape shape = new PolygonShape(5);
shape.SetAsBox(a, b);
Body body1 = BodyFactory.CreateBody(World);
body1.BodyType = BodyType.Dynamic;
body1.Position = new Vector2(-10.0f, y);
body1.CreateFixture(shape);
Body body2 = BodyFactory.CreateBody(World);
body2.BodyType = BodyType.Dynamic;
body2.Position = new Vector2(10.0f, y);
body2.CreateFixture(shape);
Vector2 anchor1 = new Vector2(-10.0f, y + b);
Vector2 anchor2 = new Vector2(10.0f, y + b);
Vector2 groundAnchor1 = new Vector2(-10.0f, y + b + l);
Vector2 groundAnchor2 = new Vector2(10.0f, y + b + l);
_joint1 = new PulleyJoint(body1, body2, groundAnchor1, groundAnchor2, body1.GetLocalPoint(anchor1),
body2.GetLocalPoint(anchor2), 2.0f);
World.AddJoint(_joint1);
}
}
示例14: SensorTest
private SensorTest()
{
{
Body ground = BodyFactory.CreateBody(World);
{
Vertices edge = PolygonTools.CreateEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));
PolygonShape shape = new PolygonShape(edge, 1);
ground.CreateFixture(shape);
}
{
CircleShape shape = new CircleShape(5.0f, 1);
shape.Position = new Vector2(0.0f, 10.0f);
_sensor = ground.CreateFixture(shape);
_sensor.IsSensor = true;
}
}
{
CircleShape shape = new CircleShape(1.0f, 1);
for (int i = 0; i < Count; ++i)
{
_touching[i] = false;
_bodies[i] = BodyFactory.CreateBody(World);
_bodies[i].BodyType = BodyType.Dynamic;
_bodies[i].Position = new Vector2(-10.0f + 3.0f*i, 20.0f);
_bodies[i].UserData = i;
_bodies[i].CreateFixture(shape);
}
}
}
示例15: RedAlarmLamp
public RedAlarmLamp(Rectangle r, Map m)
: base(m)
{
Body BodyDec = new Body(m.PhysicalWorld);
BodyDec.BodyType = BodyType.Static;
PolygonShape S = new PolygonShape(1f);
S.SetAsBox(r.Width / 2, r.Height / 2);
Fixture = BodyDec.CreateFixture(S);
Fixture.Restitution = 1f;
Fixture.Friction = 10f;
Position = r;
SetName("RedAlarmLamp");
AlertLight = new SpotLight()
{
IsEnabled = true,
Color = new Vector4(0.9f, .1f, .1f, 1f),
Power = .6f,
LightDecay = 600,
Position = new Vector3(r.X, r.Y, 20),
SpotAngle = 1.5f,
SpotDecayExponent = 3,
Direction = new Vector3(0.244402379f, 0.969673932f, 0)
};
LightingEngine LE = (LightingEngine)Renderer.GetRenderEffect("LightingEngine");
LE.AddLight(AlertLight);
}