Vertices constructor changing values

Jul 28, 2009 at 6:01 PM

I have a weird issue that is confusing me.  I'm creating a Vertices object from an array of Vector2 objects:

vertices = new Vertices(LocalPoints);

After this assignment, vertices equals

[0] x:32 y:32

[1] x:32 y:-32

[2] x:-32 y:-32

[3] x:-32 y:32

and LocalPoints equals

[0] x:0 y:0

[1] x:0 y:-64

[2] x:64 y:-64

[3] x:64 y:0


The Vertices constructor doesn't do anything except Add each point passed in to its own list.  So why are the values changing?

Jul 28, 2009 at 9:34 PM

Here was the culprit:

PhysicsGeom = GeomFactory.Instance.CreatePolygonGeom(physSim, PhysicsBody, vertices, 0);
Inside the GeomFactory, it translates all the points by the centroid, and since vertices is a class and not a struct, the reference to vertices being passed in was being modifed by a Translate() call, modifying the points in it even outside the factory.

Jul 28, 2009 at 11:29 PM

Vertices should be centered around (0,0) - then the engine will automatically translate the vertices to be around the centroid. And yes, it changes the vertices in the vertices list (not value type as you wrote). This is normal and expected behavior. If you need the polygons that have not changes, you should save the content to an array (or copy to another vertices list) before giving it to the GeomFactory.

Jul 29, 2009 at 4:00 PM

The Geom is created from a set of vertices based around 0,0... would this cause a problem with the vertices creating the body being based around a different point, or do the body and geom automatically sync up to the same position?

BTW, Thanks genbox, your constant and quick help has been invaluable. 

Jul 29, 2009 at 11:54 PM

The engine uses (0,0) as the center of all geometries, therefore you also need to give the engine a set of vertices that are centered around (0,0). Once change that made it into tho, was that the real center (centroid) is used instead of (0,0). It just makes more sense to rotate a geometry around the center of mass instead of (0,0).