How would I go about setting a b2Body's anchor to (0, 0) and not the center of the body? I have tried the SetAsBox() method with the four parameters, but I get unexpected results when I do. Ideas?
Set the b2Body's anchor to (0, 0)?
General DiscussionWhy not offset your sprite instead? Technically speaking, (0,0) generally is the "anchor" for your body. Usually you want position your sprite relative to the body, not the other way around. For polygon shapes, it is possible to offset your vertices when you initialize the shape (e.g. if your box is 1m x 1m, and you want the origin to be at the bottom of your box, subtracting the half the height of each vert of the box will do the trick, but you need to manually position your verts rather than using the SetAsBox() convenience method), however I think I remember reading somewhere that you generally want the origin and the center of mass for your body to be as close together as possible for a more stable simulation.
Well, my sprite is 40 pixels wide, and I set the PTM_RATIO to 40. I am trying to snap my sprite into a grid like position, and so I am moving the sprite to the nearest meter - round(). But that would make it, because my half of my sprite's width is 20, it loses 20 pixels on the side. It is like this - 20 pixels, then another 20 pixels with to the center of my sprite. So I need a way to be able to snap to position without that offset occurring.
I hope I made it clear,
Thanks
I hope I made it clear,
Thanks
I think I understand your problem you are having, I apologize in advance if I am not very good at explaining myself or if I misunderstand 🙁
If you do not want to maintain an offset to synch up the position of your sprite with the position of your physics body, then the center of your actual sprite image (let's call it your png file, though it could be a bmp or a tga or whatever depending on your setup) must correspond to the center of your physics body. So, if you have a png file that is 64x64 pixels, you must position the actual graphics in that png such that the center of the image is at the center of your canvas, at approximately (32, 32). Then, when you create a physics body that (by default) has it's origin at the center of the object, you do not need to translate one into the same space as the other. You simply set the position of your physics body in your physics engine's coordinate space (in Box2D's case it's meters), and then simply multiply that by the PTM_RATIO every frame in order to figure out where your sprite should be placed in pixel space.
I suspect that your graphics may not be centered in your png file, and that is why your sprite is not showing up where you expect. Or, if you are using some utility to create a sprite sheet out of multiple sprite files, it might be "trimming" the whitespace from your png file, introducing an implicit offset in the process. A tool that is popular among 2D iPhone developmers called Zwoptex allows you to decide whether or not you want to trim your images. I generally prefer that my images not be trimmed precisely so that I do not need to maintain an offset to synchronize my sprite with my physics bodies.
If you do not want to maintain an offset to synch up the position of your sprite with the position of your physics body, then the center of your actual sprite image (let's call it your png file, though it could be a bmp or a tga or whatever depending on your setup) must correspond to the center of your physics body. So, if you have a png file that is 64x64 pixels, you must position the actual graphics in that png such that the center of the image is at the center of your canvas, at approximately (32, 32). Then, when you create a physics body that (by default) has it's origin at the center of the object, you do not need to translate one into the same space as the other. You simply set the position of your physics body in your physics engine's coordinate space (in Box2D's case it's meters), and then simply multiply that by the PTM_RATIO every frame in order to figure out where your sprite should be placed in pixel space.
I suspect that your graphics may not be centered in your png file, and that is why your sprite is not showing up where you expect. Or, if you are using some utility to create a sprite sheet out of multiple sprite files, it might be "trimming" the whitespace from your png file, introducing an implicit offset in the process. A tool that is popular among 2D iPhone developmers called Zwoptex allows you to decide whether or not you want to trim your images. I generally prefer that my images not be trimmed precisely so that I do not need to maintain an offset to synchronize my sprite with my physics bodies.
Original thread: https://box2d.org/forum/viewtopic.php?t=4956