Box2d - update body center problem with polygons from SVG

hi all,

I have multiple polys, each of them connected to two anchor points (spheres as sensors) with two distance joints. When I use aabbq, update a body and send in my current mouse position and its framevelocity (analog to the helppatch) the polys jump away from my cursor.

It feels like they do this with increasing distance, the further I move away from the center.
The funny thing is that updating spheres and boxes work totally normal within the same world - even between my polygons.

Any box2d veterans around? :)

  1. “It feels like they do this with increasing distance, the further I move away from the center.”
    Makes sense because you update the body center to mouse pos. It will jump for sure.
  2. "The funny thing is that updating spheres and boxes work totally normal within the same world "
    Makes no sense because see No. 1 ;)
    Anyway, workaround is getting the pos of the body on mousclick and then just add the mouse movement framedifference when dragging.
    Also try raycast instead of aabb. aabb does not work good with rotated bodies when i remember right.


yes it is weird. have a look at this video. circles and boxes behave normally, but polys jump. Could this be related to the fact that I spawn my polys from svg data? (At position 0/0 but with the edge positions from the svg canvas)
And please elaborate on how raycasting might help for dragging objects with the mouse. Using aabbq seems the way to go, because where could I cast my rays from? Rotation is minimal btw.

Yes the centers of your polygons seem to be quite off, sometimes not even within the poly shapes itselves. That makes them jump so heavy. (edit -> just watched the video more carefully onec more. If the green boxes visualize your body centers then it seems correct but clearly something else is going wrong. Nontheless read on, if you are lucky it does not matter <- edit) The video also shows how your circles and boxes jump too but thats quite normal regarding the way you do the grabbing/dragging (just updating body pos to mouse pos).
Anyway this misalignment might be of no problem in certain usecases like yours. As i said above don´t force the update to the mouse pos from frame to frame (like teleporting) but move them gradually with framedifference. This way you dont need to worry about the centers at all. Thats also a more natural way of moving things, isnt it? If it fits your projects concepts maybe even drop the update body altogether and just use a mouse joint to make it bit more fancy and playful to grab and move things around.
What aabb does is checking a bounding box around your bodies. Thats not a problem with a straight box but not accurate with circles, polygons and rotated boxes. It will snap even when you click outside the shapes. See that pic, you can click in the aabb area and make it snap but still be outside of your box. But thats just about precision and is not related to your jumping. Raycast on the other hand will only snap when you really hit the shape.

Point taken with raycast vs. aabbq. You suggest casting from mousepos to mousepos, correct?

The problem with the misaligned centers remains…

Interestingly “getBodyDetails” and “getPolygonDetails” give me different centers. Maybe this causes the problem? the body centers of my polygons are 0/0 (± slight movement) and the poly centers are the actual positions in box 2d world.

Yes from mousepos to mousepos, works well.

The center pin on the polygon node outputs the centroid which will likely be different from the origin of your initial polygon points. In other words, if your initial points center (0,0) does not happen to be also the centroid of these points, there will a be a difference between body position and polygon center.
Anyway if the local coordinates of your polygon points are always around the origin of 0,0 (not offsetted in any direction) there shouldnt be that much difference to justify these bigger jumps.

Yep, seems like the problem is my way of generating the objects from SVG.

For example a poly has folloging vertices: (-5/2), (-7/-3), (-2/0)
I spawn the body on (0/0), but using the vertex data for the polygon has it appear at the correct position.

The physics simulation works correctly, but the body center is always relative to (0/0). Using updateBody updates this body center and causes the jump because of the difference to my mouseposition.


ok I got it working now. solution was:

saving the poly centers after spawn
subtracting the poly centers from their vertex coordinates
spawning the polygon(Bodies) on the original polygon positions
now the body centers ~match with the body centers

even better with the raycast!

Thank you!

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.