本文整理汇总了C#中Vertices.GetCentroid方法的典型用法代码示例。如果您正苦于以下问题:C# Vertices.GetCentroid方法的具体用法?C# Vertices.GetCentroid怎么用?C# Vertices.GetCentroid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Vertices
的用法示例。
在下文中一共展示了Vertices.GetCentroid方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateFixture
public List<Fixture> CreateFixture(Body body)
{
var vertices = new Vertices(Verts);
List<Vertices> vertsList = BayazitDecomposer.ConvexPartition(vertices);
List<Fixture> fixtureList = new List<Fixture>();
Vector2 mainCentroid = vertices.GetCentroid();
foreach (Vertices v in vertsList)
{
PolygonShape shape = new PolygonShape(v, Density);
Fixture fixture = new Fixture(body, shape, RotationOffset);
fixture.IsSensor = IsSensor;
fixture.CollisionGroup = CollisionGroup;
fixture.CollisionCategories = (Category)CollisionCategories;
fixture.CollidesWith = (Category)CollidesWith;
fixtureList.Add(fixture);
}
return fixtureList;
}
示例2: CreatePolygonBody
/// <summary>
/// Creates a Body. The moment of inertia of the body is calculated from the
/// set of vertices passed in to this method. The vertices should represent a polygon.
/// </summary>
/// <param name="vertices">Vertices representing some polygon</param>
/// <param name="mass">Mass of the Body</param>
/// <returns></returns>
public Body CreatePolygonBody(Vertices vertices, float mass)
{
if (vertices == null)
throw new ArgumentNullException("vertices", "Vertices must not be null");
if (mass <= 0)
throw new ArgumentOutOfRangeException("mass", "Mass must be more than 0");
Body body = new Body();
body.Mass = mass;
body.MomentOfInertia = mass * vertices.GetMomentOfInertia();
body.position = vertices.GetCentroid();
return body;
}
示例3: PolygonCollision
/// <summary>
/// Check if polygon A is going to collide with polygon B.
/// The last parameter is the *relative* velocity
/// of the polygons (i.e. velocityA - velocityB)
/// </summary>
/// <param name="polygonA">The polygon A.</param>
/// <param name="polygonB">The polygon B.</param>
/// <param name="velocity">The velocity.</param>
/// <returns></returns>
internal PolygonCollisionResult PolygonCollision(Vertices polygonA, Vertices polygonB, Vector2 velocity)
{
PolygonCollisionResult result = new PolygonCollisionResult();
result.Intersect = true;
result.WillIntersect = true;
int edgeCountA = polygonA.Count;
int edgeCountB = polygonB.Count;
float minIntervalDistance = float.PositiveInfinity;
Vector2 translationAxis = new Vector2();
Vector2 edge;
// Loop through all the edges of both polygons
for (int edgeIndex = 0; edgeIndex < edgeCountA + edgeCountB; edgeIndex++)
{
if (edgeIndex < edgeCountA)
{
edge = polygonA.GetEdge(edgeIndex);
}
else
{
edge = polygonB.GetEdge(edgeIndex - edgeCountA);
}
// ===== 1. Find if the polygons are currently intersecting =====
// Find the axis perpendicular to the current edge
Vector2 axis = new Vector2(-edge.Y, edge.X);
axis.Normalize();
// Find the projection of the polygon on the current axis
float minA = 0; float minB = 0; float maxA = 0; float maxB = 0;
ProjectPolygon(axis, polygonA, ref minA, ref maxA);
ProjectPolygon(axis, polygonB, ref minB, ref maxB);
// Check if the polygon projections are currentlty intersecting
float intervalDistance = IntervalDistance(minA, maxA, minB, maxB);
if (intervalDistance > 0)
result.Intersect = false;
// ===== 2. Now find if the polygons *will* intersect =====
// Project the velocity on the current axis
//float velocityProjection = Vector2.Dot(axis, velocity);
// Get the projection of polygon A during the movement
//if (velocityProjection < 0) {
// minA += velocityProjection;
//} else {
// maxA += velocityProjection;
//}
// Do the same test as above for the new projection
//float intervalDistance = IntervalDistance(minA, maxA, minB, maxB);
//if (intervalDistance > 0) result.WillIntersect = false;
// If the polygons are not intersecting and won't intersect, exit the loop
//if (!result.Intersect && !result.WillIntersect) break;
if (!result.Intersect) break;
// Check if the current interval distance is the minimum one. If so store
// the interval distance and the current distance.
// This will be used to calculate the minimum translation vector
intervalDistance = Math.Abs(intervalDistance);
if (intervalDistance < minIntervalDistance)
{
minIntervalDistance = intervalDistance;
translationAxis = axis;
Vector2 d = polygonA.GetCentroid() - polygonB.GetCentroid();
if (Vector2.Dot(d, translationAxis) < 0)
translationAxis = -translationAxis;
}
}
// The minimum translation vector
// can be used to push the polygons appart.
if (result.WillIntersect)
result.MinimumTranslationVector =
translationAxis * minIntervalDistance;
return result;
}
示例4: ConstructFromVertices
/// <summary>
///
/// </summary>
/// <param name="world"></param>
/// <param name="vertices">The collection in vertices in display units (pixels)</param>
/// <param name="density"></param>
private void ConstructFromVertices(World world, Vertices vertices, float density)
{
//We need to find the real center (centroid) of the vertices for 2 reasons:
//1. To translate the vertices so the polygon is centered around the centroid.
var centroid = -vertices.GetCentroid();
vertices.Translate(ref centroid);
//2. To draw the texture the correct place.
Center = -centroid;
//We simplify the vertices found in the texture.
vertices = SimplifyTools.ReduceByDistance(vertices, 4f);
//Since it is a concave polygon, we need to partition it into several smaller convex polygons
List<Vertices> list = BayazitDecomposer.ConvexPartition(vertices);
//List<Vertices> list = EarclipDecomposer.ConvexPartition(vertices);
//Now we need to scale the vertices (result is in pixels, we use meters)
Vector2 vertScale = new Vector2(ConvertUnits.SimUnitsToDisplayUnitsRatio);// new Vector2(ConvertUnits.ToSimUnits(1)) * _scale;
foreach (Vertices newVertices in list)
{
newVertices.Scale(ref vertScale);
}
Body = BodyFactory.CreateCompoundPolygon(world, list, density);
}
示例5: CreatePolygonGeom
/// <summary>
/// Creates a polygon geom.
/// </summary>
/// <param name="body">The body.</param>
/// <param name="vertices">The vertices.</param>
/// <param name="positionOffset">The offset.</param>
/// <param name="rotationOffset">The rotation offset.</param>
/// <param name="collisionGridSize">Size of the collision grid. - not used with SAT
/// Put in 0 or less to make the engine calculate a grid cell size.</param>
/// <returns></returns>
public Geom CreatePolygonGeom(Body body, Vertices vertices, Vector2 positionOffset, float rotationOffset, float collisionGridSize)
{
if (body == null)
throw new ArgumentNullException("body", "Body must not be null");
if (vertices == null)
throw new ArgumentNullException("vertices", "Vertices must not be null");
//Adjust the verts to be relative to the centroid.
Vector2 centroid = vertices.GetCentroid();
Vector2.Multiply(ref centroid, -1, out centroid);
vertices.Translate(ref centroid);
Geom geometry = new Geom(body, vertices, positionOffset, rotationOffset, collisionGridSize);
return geometry;
}
示例6: findShadowHull
public void findShadowHull(Texture2D texture)
{
//Create an array to hold the data from the texture
uint[] data = new uint[texture.Width * texture.Height];
//Transfer the texture data to the array
texture.GetData(data);
//Find the vertices that makes up the outline of the shape in the texture
textureVertices = PolygonTools.CreatePolygon(data, texture.Width, false);
//The tool return vertices as they were found in the texture.
//We need to find the real center (centroid) of the vertices for 2 reasons:
//1. To translate the vertices so the polygon is centered around the centroid.
Vector2 centroid = -textureVertices.GetCentroid();
textureVertices.Translate(ref centroid);
//2. To draw the texture the correct place.
_origin = -centroid;
//We simplify the vertices found in the texture.
textureVertices = SimplifyTools.CollinearSimplify(textureVertices, 0f);
//Since it is a concave polygon, we need to partition it into several smaller convex polygons
list = BayazitDecomposer.ConvexPartition(textureVertices);
//Adjust the scale of the object for WP7's lower resolution
//Adjust the scale of the object for WP7's lower resolution
#if WINDOWS_PHONE
_scale = 0.6f;
#else
_scale = 1f;
#endif
//scale the vertices from graphics space to sim space
Vector2 vertScale = new Vector2(ConvertUnits.ToSimUnits(1)) * _scale;
foreach (Vertices vertices in list)
{
vertices.Scale(ref vertScale);
Vector2[] verticesArray = vertices.ToArray();
var hull = ShadowHull.CreateConvex(ref verticesArray);
hulls.Add(hull);
krypton.Hulls.Add(hull);
}
}
示例7: CreatePolygonGeom
/// <summary>
///
/// </summary>
/// <param name="body"></param>
/// <param name="vertices"></param>
/// <param name="offset"></param>
/// <param name="rotationOffset"></param>
/// <param name="collisionGridCellSize">Pass in 0 or less to make Farseer calculate the grid cell size</param>
/// <returns></returns>
public Geom CreatePolygonGeom(Body body, Vertices vertices, Vector2 offset, float rotationOffset,
float collisionGridCellSize)
{
//adjust the verts to be relative to 0,0
Vector2 centroid = vertices.GetCentroid();
vertices.Translate(-centroid);
if (collisionGridCellSize <= 0)
{
collisionGridCellSize = CalculateGridCellSizeFromAABB(vertices);
}
Geom geometry = new Geom(body, vertices, offset, rotationOffset, collisionGridCellSize);
return geometry;
}
示例8: InitOuterWall
private void InitOuterWall(Vertices vertices)
{
if (vertices.Count < 3)
{
Console.WriteLine("Too few vertices " + vertices.Count);
return;
}
Vertices drawingVertices = new Vertices(vertices);
float scale = _thickness / drawingVertices.GetRadius() + 1;
Vector2 scaleVector = new Vector2(scale);
Vector2 translation = drawingVertices.GetCentroid();
drawingVertices.Translate(-translation);
drawingVertices.Scale(ref scaleVector);
drawingVertices.Translate(translation);
_wallVerts = new VertexPositionTexture[vertices.Count * 2 + 2];
for (int i = 0; i < vertices.Count; ++i)
{
int j = i * 2;
_wallVerts[j].Position.X = vertices[i].X;
_wallVerts[j].Position.Y = vertices[i].Y;
_wallVerts[j].Position.Z = 0;
_wallVerts[j].TextureCoordinate = vertices[i] / _textureScaling;
_wallVerts[j + 1].Position.X = drawingVertices[i].X;
_wallVerts[j + 1].Position.Y = drawingVertices[i].Y;
_wallVerts[j + 1].Position.Z = 0;
_wallVerts[j + 1].TextureCoordinate = drawingVertices[i] / _textureScaling;
}
int lastIndex = vertices.Count * 2;
_wallVerts[lastIndex].Position.X = vertices[0].X;
_wallVerts[lastIndex].Position.Y = vertices[0].Y;
_wallVerts[lastIndex].Position.Z = 0;
_wallVerts[lastIndex].TextureCoordinate = vertices[0] / _textureScaling;
_wallVerts[lastIndex + 1].Position.X = drawingVertices[0].X;
_wallVerts[lastIndex + 1].Position.Y = drawingVertices[0].Y;
_wallVerts[lastIndex + 1].Position.Z = 0;
_wallVerts[lastIndex + 1].TextureCoordinate = drawingVertices[0] / _textureScaling;
}
示例9: InitInnerWall
private void InitInnerWall(Vertices vertices)
{
if (vertices.Count < 3)
{
Console.WriteLine("Too few vertices " + vertices.Count);
return;
}
Vector2 center = vertices.GetCentroid();
_wallVerts = new VertexPositionTexture[vertices.Count * 2 + 2];
for (int i = 0; i < vertices.Count; ++i)
{
int j = i * 2;
_wallVerts[j].Position.X = vertices[i].X;
_wallVerts[j].Position.Y = vertices[i].Y;
_wallVerts[j].Position.Z = 0;
_wallVerts[j].TextureCoordinate = vertices[i] / _textureScaling;
_wallVerts[j + 1].Position.X = center.X;
_wallVerts[j + 1].Position.Y = center.Y;
_wallVerts[j + 1].Position.Z = 0;
_wallVerts[j + 1].TextureCoordinate = center / _textureScaling;
}
int lastIndex = vertices.Count * 2;
_wallVerts[lastIndex].Position.X = vertices[0].X;
_wallVerts[lastIndex].Position.Y = vertices[0].Y;
_wallVerts[lastIndex].Position.Z = 0;
_wallVerts[lastIndex].TextureCoordinate = vertices[0] / _textureScaling;
_wallVerts[lastIndex + 1].Position.X = center.X;
_wallVerts[lastIndex + 1].Position.Y = center.Y;
_wallVerts[lastIndex + 1].Position.Z = 0;
_wallVerts[lastIndex + 1].TextureCoordinate = center / _textureScaling;
}