I am using Box2D 2.0 with Flash. I have screen objects that contain multiple bodies and want to allow the user to interactively rotate the screen objects to any angle. I've been able to use body.SetXForm to rotate a screen object when it consists of a single body and the rotation center is the center of the single body. How would I rotate around a specific point several bodies contained in an object?
Rotate Multiple Bodies Around a Common Center?
General DiscussionHow are the bodies linked together? With joints? If so, what kinds of joints?
Thanks for reply. Currently for the most part the bodies aren't really connected in Box2D. I do have some jointed objects, but for the most part the objects bodies are only connected through code in Flash. So I might have an object containing a wheel body and a target body. The Flash code detects when the target has been hit and then starts the wheel spinning. So, I need to take 2 bodies that are, in some ways unrelated in the Box2D world and then rotate them about a common point.
Ok, it sounds like this is basically a linear algebra/2-d math problem then.
Since you were previously able to use SetXForm() to rotate a single body in response to user input, you should be able to do the same with multiple selected bodies. The trick is just figuring out the correct transforms for the bodies based on said input.
The most straightforward solution would probably be as follows. First, you need to select a pivot point for the group of objects. (What point to choose depends on the needs of your application - an example though would be the center of the axis-aligned bounding box containing the objects.) You'll also need to grab and store the starting transforms (position and rotation/angle) for each of the objects in the group.
With this information, you should be able to compute new transforms for the objects given any delta angle (e.g. as derived from user input). To compute the new position for a given body, rotate the vector from the pivot point to its original position by the delta angle, and then add the resulting vector to the pivot point (this can be done using matrix math, or simple trig). To compute the new rotation for the body, simply add the angle delta to the body's original angle. (I'm pretty sure this will give you the correct results.)
You can do the same thing using transforms and matrix math if you prefer. Store the starting transform for each object as a 3x3 matrix, and compute the pivot point as described previously. Create a 3x3 translation matrix based on the pivot point. and then multiply each of the 'object' transform matrices by the inverse of this matrix to yield a transform that is local relative to the pivot point.
Then, whenever the angle changes in response to user input (or whatever), create a new 3x3 matrix from this angle and the pivot point. Finally, multiply this matrix by the local transform matrices for the objects to yield the new world transform for each object.
Now, the above is all based on my understanding of what you're trying to do, which may be wrong or incomplete. If you have any questions about any of the above though, feel free to ask.
Since you were previously able to use SetXForm() to rotate a single body in response to user input, you should be able to do the same with multiple selected bodies. The trick is just figuring out the correct transforms for the bodies based on said input.
The most straightforward solution would probably be as follows. First, you need to select a pivot point for the group of objects. (What point to choose depends on the needs of your application - an example though would be the center of the axis-aligned bounding box containing the objects.) You'll also need to grab and store the starting transforms (position and rotation/angle) for each of the objects in the group.
With this information, you should be able to compute new transforms for the objects given any delta angle (e.g. as derived from user input). To compute the new position for a given body, rotate the vector from the pivot point to its original position by the delta angle, and then add the resulting vector to the pivot point (this can be done using matrix math, or simple trig). To compute the new rotation for the body, simply add the angle delta to the body's original angle. (I'm pretty sure this will give you the correct results.)
You can do the same thing using transforms and matrix math if you prefer. Store the starting transform for each object as a 3x3 matrix, and compute the pivot point as described previously. Create a 3x3 translation matrix based on the pivot point. and then multiply each of the 'object' transform matrices by the inverse of this matrix to yield a transform that is local relative to the pivot point.
Then, whenever the angle changes in response to user input (or whatever), create a new 3x3 matrix from this angle and the pivot point. Finally, multiply this matrix by the local transform matrices for the objects to yield the new world transform for each object.
Now, the above is all based on my understanding of what you're trying to do, which may be wrong or incomplete. If you have any questions about any of the above though, feel free to ask.
Cool. Thanks. That was exactly what I needed. My bodies are rotating now!
I found some formulas at http://en.wikipedia.org/wiki/Rotation_(mathematics) for the rotation. Initially, I had tried to roll my own trig/algebra solution but that didn't work. Your matrix suggestion and the example on the wiki helped me solve it.
I found some formulas at http://en.wikipedia.org/wiki/Rotation_(mathematics) for the rotation. Initially, I had tried to roll my own trig/algebra solution but that didn't work. Your matrix suggestion and the example on the wiki helped me solve it.
Original thread: https://box2d.org/forum/viewtopic.php?t=4963