useful snippet

I thought someone might find the code below useful,

I use these functions to convert box2d world coordinates to screen coordinates, and screen coords to box2d coords.
To get the the scale factors, divide the resolution width/height by double the physics world width/height, i.e. 2.5 scale is for 1280 x 960.

I haven't tried this with widescreen as I don't have a widescreen monitor, but I doubt it would work without tweaking.

If anyone has any ideas for improving these functions, let me know.


#define widthScaleFactor 2.5
#define heightScaleFactor 2.5

#define widthPhysicsWorld 256.0
#define heightPhysicsWorld 192.0

b2Vec2 Math::GetScreenCoordinates( b2Vec2 pVec )
{
b2Vec2 sVec;

sVec.x = (widthPhysicsWorld + pVec.x) * widthScaleFactor;
sVec.y = (heightPhysicsWorld - pVec.y) * heightScaleFactor;

return( sVec );
}


b2Vec2 Math::GetPhysicCoordinates( b2Vec2 sVec )
{
b2Vec2 pVec;

pVec.x = (sVec.x / widthScaleFactor) - widthPhysicsWorld;
pVec.y = heightPhysicsWorld - (sVec.y / heightScaleFactor);

return( pVec );
}

Original thread: https://box2d.org/forum/viewtopic.php?t=3953