Read vertices of physics body shape

I’m experimenting with 2D shadows (little demo video here: bit.ly/1FKHPSL)

 

Therefore it would be nice to use the vertex positions of a physics body shape?

Is there a way to read the vertex positions?

(I know that box2D does have methods to do this?)

 

René

no, but YOU have access to them (you must have, when you added the body) so you don’t (shouldn’t) need box2d to retrieve them, just simply store them somewhere for later use, fe:

– at some point your code “knew” what “theShape” was

physics.addBody(aDisplayObject,“dynamic”,{shape=theShape})

– so store a copy on the object for later retrieval

aDisplayObject.theShape = theShape

hth

This is correct since I’m doing it exactly like this right now.

But now if I rotate/scale my object I have to rotate/scale my shape as well, with vector matrices and so on, which I want to avoid.

But if I could read the vertex positions … everything would be much easier… 

then you’re probably already doing best currently possible, otherwise it’d be a feature request (more likely a couple)…  expose body’s getVertices() AND expose body’s GetTransform() and at least one vector method to multiply each point by that transform matrix – since even box2d doesn’t store the transformed verts

At least I found a forum post on how to get the vertices in box2d here: http://box2d.org/forum/viewtopic.php?f=3&t=1933

So it should be possible to just read the vertices position of the body shape. But as long as these infos are not passed to Corona I think I have to bite the bullet.

Thank you hth

just note that GetWorldPoint() is still transforming on-the-fly, so no different than if you could access body.transform and call a native matrix*vector for each point.  (tradeoff:  a simpler API for this specific use, but less flexible overall)

fwiw, me personally: if I could only ask for one or the other, i’d rather have access to body.transform.  the matrix xform is simple enough that the overhead of calling a native method and packaging up a lua table return might be nearly a wash for performance anyway (unless you could do it for entire shape at once, instead of point-at-a-time).  but building the transform from scratch is “costly” in lua (due to the sin/cos and implied multiple compositions for translate, scale, rotate), so i’d rather get a prebuilt matrix, then lua can handle m11*x+m21*y, m12*x+m22*y

phew, I have to investigate some more math studies to understand you last sentences :slight_smile: thank you!

no, but YOU have access to them (you must have, when you added the body) so you don’t (shouldn’t) need box2d to retrieve them, just simply store them somewhere for later use, fe:

– at some point your code “knew” what “theShape” was

physics.addBody(aDisplayObject,“dynamic”,{shape=theShape})

– so store a copy on the object for later retrieval

aDisplayObject.theShape = theShape

hth

This is correct since I’m doing it exactly like this right now.

But now if I rotate/scale my object I have to rotate/scale my shape as well, with vector matrices and so on, which I want to avoid.

But if I could read the vertex positions … everything would be much easier… 

then you’re probably already doing best currently possible, otherwise it’d be a feature request (more likely a couple)…  expose body’s getVertices() AND expose body’s GetTransform() and at least one vector method to multiply each point by that transform matrix – since even box2d doesn’t store the transformed verts

At least I found a forum post on how to get the vertices in box2d here: http://box2d.org/forum/viewtopic.php?f=3&t=1933

So it should be possible to just read the vertices position of the body shape. But as long as these infos are not passed to Corona I think I have to bite the bullet.

Thank you hth

just note that GetWorldPoint() is still transforming on-the-fly, so no different than if you could access body.transform and call a native matrix*vector for each point.  (tradeoff:  a simpler API for this specific use, but less flexible overall)

fwiw, me personally: if I could only ask for one or the other, i’d rather have access to body.transform.  the matrix xform is simple enough that the overhead of calling a native method and packaging up a lua table return might be nearly a wash for performance anyway (unless you could do it for entire shape at once, instead of point-at-a-time).  but building the transform from scratch is “costly” in lua (due to the sin/cos and implied multiple compositions for translate, scale, rotate), so i’d rather get a prebuilt matrix, then lua can handle m11*x+m21*y, m12*x+m22*y

phew, I have to investigate some more math studies to understand you last sentences :slight_smile: thank you!