0
\$\begingroup\$

I am using Cocos2d-x 3.5. I created a simple game with edge bodies and one circular body to test box2d. Physics wasn't working well, so i thought i should convert pixels to meters and use that meters for example on aplying impulses.

I don't know how to do it. I can't find any converting function. Also there is no any PTM_RATIO variable. I tried to create it myself, but i don't really know how to use it properly. Can you help me please to understand that? Thank you.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Models/geometry are created with arbitrary units; if those units happen to already represent meters, no scaling is needed, only translation is needed.

model-space coordinate (no units) * World = world-space coordinate (meters) 

View-space units are the same as World-space. The View matrix translates and orients the world such that the new origin is the camera's location and the Z-axis points in the same direction as camera-forward. Coordinates are now relative to the camera; i.e. a coordinate that is "to the left" of the camera will have a negative X-value.

world-space coordinate (meters) * View = view-space coordinate (meters) 

Projection-space units are intermediate; they represent a percentage of the active rendertarget/viewport. Projection-space is also defined with the origin at the camera's location, moving the camera to the center of the rendertarget/viewport. Coordinates are relative to the center of the screen and are in the range of -1 to 1; i.e. coordinates along the left-most edge of the rendertarget have X-values of -1 and coordinates along the bottom-most edge have Y-values of -1.

view-space coordinate (meters) * Projection = projection-space coordinate (unitless) 

To convert projection-space coordinates into pixels, you convert them from the -1:1 range to the 0:TextureDimension range. Texture-space defines the origin, (0,0), at the top-left corner and (1,1) at the lower-left corner.

textureSpaceCoordinate = projectionSpaceCoordinate //-1 to 1 textureSpaceCoordinate += 1.0f; // 0 to 2 textureSpaceCoordinate /= 2.0f; // 0 to 1 //A Y-value of 1 corresponds to the top of projection-space //and the bottom of texture-space; need to invert it textureSpaceCoordinate.Y = 1.0f - textureSpaceCoordinate.Y; //If the texture is 800 pixels wide, X-values will range from 0 to 799 pixelCoordinate.X = textureSpaceCoordinate.X * (TextureWidth - 1); //If the texture is 600 pixels tall, Y-values will range from 0 to 599 pixelCoordinate.Y = textureSpaceCoordinate.Y * (TextureHeight - 1); 

If you start the above process at Projection and use the view-space coordinate (1, 1), the resulting pixelCoordinate approximates the "PTM_RATIO"(s).

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.