I’m playing around with Box2d (more or less unrelated to this post) and creating a custom polygon body for use with the physics engine. I’m loading my shape data from a file that contains all the points, and creating a ofPolyline from those points. I can use this ofPolyline to create the Box2d polygon with polyline.getVertexes(). This is great, but the shape is always created at the same position and size (not surprising, that’s the vertex data I’m using).
I’m looking for a way to scale, rotate and translate the ofPolyline shape before creating the Box2d polygon with it. I noticed the ofPath has some methods for scaling and rotating, but ofPolyline does not. I can do the scaling and rotating with an ofPath version of the same shape data, but I don’t know how to get all the vertexes of the ofPath to create the Box2d body from (I think because ofPath has subPaths and not just one single vector of points like ofPolyline?)
Generally, I’m hoping someone can explain the differences between ofPath and ofPolyline, why you would choose one over the other, and if there is anyway to scale, rotate and translate an ofPolyline shape. Maybe I just need to write the methods to loop through all the ofPolyline points and do those myself? (translate seems easy enough, but i’m not sure about scale or rotate)
There’s some good definitions of each in the docs section (eg. http://openframeworks.cc/documentation/graphics/ofPath.html). In general though, I think of ofPolyline as a single vector of points and use this as a replacement for such. ofPath (which can also be used in ofPolyline mode), is usually made up from multiple vectors of points, as well as having some convenient drawing functions (eg. draw with fill and outline), and mesh/tessellation helpers.
To get the vertices from an ofPath to pass to Box2D you call myPath.getOutline().getVertices() which would give you the first ofPolyline’s vector of points. Also, the code for scaling, rotating, and translating an ofPolyline’s individual points is actually in the ofPath.cpp methods if your interested.
If you want to scale your Box2D shapes after they have been added to your physics world, it’s quite different as your vertices have already been turned into b2PolygonShape’s and b2Vec2’s. It’s possible though.
Yeah I was able to do a lot of the scaling / rotating / translating functions I wanted with some vector math inside a loop of the ofPolyline vertices. It’s probably pretty close to what’s going on in the ofPath.cpp source. I think where I got confused was the fact that ofPath’s sub-paths are just ofPolylines. ofPath is starting to look almost more like an SVG document that can have many paths, while ofPolyline is good for representing just 1 single path or vector of points, like you said.
Thanks again for the reply. Now I need to figure out why the triangulatePoly() method on my ofxBox2dPolygon is resulting in a child like rendering of my original shape…