Synchronizing with HAAF's engine.

Hi everyone. I'm new on this forum. I'm trying to syncrhonize Box2d with HAAF's engine, but I'm having problems.

- At first, I noticed that the Y coordenates decrease as you go down, totally different from HAAF.
So I inverted HAAF coordenates.
- Then I do a box 128x128 (with SetAsBox(64,64) ), it works finely. I first put the sprite with Body->GetPosition() then I rotate it with my own mathemathic formula, and it works xD.
- Next I do a ball. I use a b2CircleDef with 30 radius but here I start having weird problems:
- I did a ground, I put it on 0,0.
- I did a box of 128x128 on ground.
- I put a breakpoint on Draw thingy, I notice that GetPosition() returns y = 128.541, that's good because the height of the box is that.
- Then I do that circle of 30 radius.
- Placed on the same ground.
- With the same breakpoint, I see GetPosition() returning y = 95.0564.

¿Is that normal? I thought it should return 60, or even 30 but not 95... Any idea?
Well, box2d the C++ version doesn't really enforce any usage, you probably think the Y is the opposite way because you used the default gravity of (0, -10)? However most graphics libraries, particularly 2d will have Y decrease as you go down.

However, Box2d is tuned for objects between 0.1 and 10.0 in size, so you really need to shrink your objects and create some sort of function that scales from world to screen anyway. For example, here is a bunch of functions I used for a game in a different engine, but they should be fairly similar to what you need.

Code:
b2Vec2 Camera::getScreenCoords(const b2Vec2 * const worldCoords) const
{
   b2Vec2 screenCoords;
   screenCoords.Set(screenScale.x*(worldCoords->x-cameraView->lowerBound.x), pixelHeight - screenScale.y*(worldCoords->y-cameraView->lowerBound.y));
   return screenCoords;
}

b2Vec2 Camera::getWorldCoords(const b2Vec2 * const screenCoords) const
{
   b2Vec2 worldCoords;
   worldCoords.Set((screenCoords->x/screenScale.x) + cameraView->lowerBound.x , (pixelHeight - screenCoords->y)/screenScale.y + cameraView->lowerBound.y);
   return worldCoords;
}

void Camera::calculateScale()
{
   screenScale.x = pixelWidth/width;
   screenScale.y = pixelHeight/height;
}


Note that the actual camera position and view is stored as both a pair of vectors representing a centre and a size, and an AABB. The centre is modified when the camera moves and the size is modified when you zoom: the problem with using the AABB to do all that is floating point precision messes it up, when you move to the left or right it can slowly shrink the view. Every time the camera view changes, the AABB is recalculated. It also allows you to use b2World->Query with the AABB, which will return a list of all objects off screen so you don't need to waste time drawing offscreen objects.
Toeofdoom wrote:
However, Box2d is tuned for objects between 0.1 and 10.0 in size


So my problem is the size? It will work if I scale it?

If I want to do giant ground it won't work?
Static objects such as ground aren't so much of an issue. Having a ball that's 60 units in diameter? That won't look right.

Additionally, if your ground is a 128x128 box as well, there's no bug: it's working fine. The centre of the box is 64 above the top of the ground, which is 64 (64+64 = 128). The centre of the circle is 30 above the top of the ground (64) so it should be at 30+64 = 94. 95 is pretty close.
I'm using Haaf's game engine as well and it's working fine for me with the proper gravity definition and some scale between physic and graphic units..

My implementation is heavily inspired from mercior's work on Opal Game Framework ( see this topic : http://relishgames.com/forum/viewtopic.php?t=4850 )
Unfortunately he is using an old box2d version so I add to rewrite some of it + add my own features.
I think there is a b2DebugDraw renderer posted in the forum as well.

FYI I'm using an upward gravity vector and a physic scale of 0.05

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