
Hi,
I cannot make polygons to collide with each other, i tried different shapes, different collisionGirdCell sizes, and i tried to subdivide my polygons into larger number of vertices, but they never collide with each other, sometimes they bounce ocasionally, but
thats all. When I drop circle on my static polygon it usualy collides but when I drop other polygon it always fails, could you help me, I dont know what to do :(


Coordinator
Dec 29, 2007 at 5:59 PM

How did you define your polygons vertices. Did you define them as though the are centered around 0,0 ?



it sounds like your graphical polygons are not scaled/transformed to your physics ones



Vertices are centered around 0,0,
but I'm not sure If I scaled graphical polygons to physics ones, I just draw them, like in code below:
public class RectangleEntity : IEntity
{
Point[] _polygon;
Body _body;
Geom _geom;
public RectangleEntity(PhysicsSimulator physicSimulator, Point[] poly)
{
int x, y;
_polygon = PolygonMath.CenterPolygon(poly, out x, out y);
float mass = (float)PolygonMath.PolygonArea(_polygon);
Vertices vert = new Vertices(Tools.Subdivide(Tools.Poly2Vertexes(_polygon), 8));
_body = BodyFactory.Instance.CreateBody(physicSimulator, mass, vert.CalculateMomentOfInertia());
_body.Position = new Vector2(x, y);
_geom = GeomFactory.Instance.CreatePolygonGeom(physicSimulator, _body, vert, 1);
}
#region IEntity Members
public void Draw(Graphics g)
{
g.DrawLines(Pens.Black, Tools.Vertexes2Points(_geom.WorldVertices));
foreach (Point p in Tools.Vertexes2Points(_geom.WorldVertices))
{
g.FillEllipse(Brushes.Black, p.X  2, p.Y  2, 4, 4);
}
}
#endregion
}



Hi, can you tell mi if there is something wrong with the code above?


Coordinator
Jan 2, 2008 at 10:31 AM

It's hard to tell because you are calling some of your own functions here. Could be a problem with your drawing or tools code??



// center pologon around its weight center
public static Point[] CenterPolygon(Point []polygon, out int x, out int y)
// subdivide polygon into smaler vertices, taken from phys2d engine
public static Vector2[] Subdivide(Vector2[] vertexes, float maxLength, bool loop)
// conver Point array into Vector2 array
public static Vector2[] Poly2Vertexes(Point[] poly)
// convert Vertices into Point array
public static Point[] Vertexes2Points(Vertices vertices)
I have no idea what do I do wrong, I will aprecciate if you could take a look at it, because I've got stucked.
(Source code below)
Thanks in advance
public static Point[] CenterPolygon(Point []polygon, out int x, out int y)
{
x = y = 0;
foreach (Point p in polygon)
{
x += p.X;
y += p.Y;
}
x /= polygon.Length;
y /= polygon.Length;
Point[] np = new Pointpolygon.Length;
for (int i = 0; i < polygon.Length; i++)
{
npi = new Point(polygoni.X  x, polygoni.Y
 y);
}
return np;
}
public static Vector2[] Subdivide(Vector2[] vertexes, float maxLength, bool loop)
{
if (vertexes == null) { throw new ArgumentNullException("vertexes"); }
if (vertexes.Length < 2) { throw new ArgumentOutOfRangeException("vertexes"); }
if (maxLength <= 0) { throw new ArgumentOutOfRangeException("maxLength", "must be greater then zero"); }
LinkedList<Vector2> list = new LinkedList<Vector2>(vertexes);
LinkedListNode<Vector2> node = list.First;
while (node != null)
{
Vector2 line;
if (node.Next == null)
{
if (!loop) { break; }
line = list.First.Value  node.Value;
}
else
{
line = node.Next.Value  node.Value;
}
float mag;
GetMagnitude(ref line, out mag);
if (mag > maxLength)
{
int count = (int)Math.Ceiling(mag / maxLength);
mag = 1f / count;
// Vector2.Multiply(ref line, ref mag, out line);
Vector2.Multiply(ref line, mag, out line);
for (int pos = 1; pos < count; ++pos)
{
node = list.AddAfter(node, line + node.Value);
}
}
node = node.Next;
}
Vector2[] result = new Vector2list.Count;
list.CopyTo(result, 0);
return result;
}
public static Vector2[] Poly2Vertexes(Point[] poly)
{
List<Vector2> v = new List<Vector2>();
foreach (Point p in poly)
v.Add(new Vector2(p.X, p.Y));
return v.ToArray();
}
public static Point[] Vertexes2Points(Vertices vertices)
{
List<Point> l = new List<Point>();
foreach (Vector2 v in vertices)
{
l.Add(new Point((int)v.X, (int)v.Y));
}
return l.ToArray();
}

