开发者

Box2d - actionscript, java, andengine - body position

开发者 https://www.devze.com 2023-03-24 16:02 出处:网络
I\'ve lost in tonnes of information for box2d and it\'s ports. But I have very simple question, for which I can\'t get any informations anywhere.

I've lost in tonnes of information for box2d and it's ports. But I have very simple question, for which I can't get any informations anywhere.

How to properly calculate body position. Let say I have sprite on screen in position (10, 20). Why every tutorial for box2d computing this different ?

For example: - processing (from processing.org) using function coordPixelToWorld and coordWorldToPixel for calculate beetwen world and screen - which is a little compilcated function; - andengine have similar function for converting beetwen world and screen; - actionscript - here I don't understand why but every tutorial has getting screen coords and dividing it by scale function.

I ask this questions because all of the above have one common point: screen coords for 0,0 are in left top corner. Is box2d written different for every port ?

I will be grateful for explanation.

Update

I don't have very big troubles with box2d actionscript. My problemy is: why when I'm setting body position to (0, 0) it's displayed in left top corner of window in short words. In any other box2d ports (proc开发者_开发问答essing, jbox2d, andengine box2d extension) if I set body position to (0, 0) it's displayed in the center of window. I know rules about pixel per meters etc.


The reason for the variation is that Box2D in all of its forms, uses meters as its unit of measurement. The reason for this is that it is intended a simulation of real-world objects.

Box2D does not need to have a visual representation at all. When you create a box2D simulation, it is up to the developer what level of detail to show in the graphic render of box2D. So there will be a multiplier, representing the conversion of meters to pixels. In the case of Box2D flash, the default for this ratio is 30px:1m.

So the reason every toturial has a conversion function, is that Box2D uses meters, and computer displays use pixels.

It converts meters to pixels, and pixels to meters.


In actionscript the centre of a DisplayObject is usually top-left, but Box2D bodies' centre coincides with the barycentric centre (the actual middle of the shape, not the top-left corner). Also the Box2D units are not exactly pixels, but you can easily convert between the two.

I recommend having a look at the Box2DFlash Manual, especially the part on Units and UserData. Ideally you would want to read the whole manual to get your head around Box2D better, which will give you a lot more control.

If you're not interested in the nitty gritty then give the World Construction Kit a try. It should make it easier to setup Box2D worlds.

Box2d - actionscript, java, andengine - body position

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号