According to "The Nature of Code", the coordinate system for Box2D is a standard cartesian one, whereas for Processing it's one with the origin at the top left of the window, with y-coordinate increasing downward and x-coordinate to the right.
However, testing the coordinate conversion functions in pbox2d gives a different picture:
import pbox2d.*;
import org.jbox2d.common.*;
boolean once = false;
PBox2D box2d;
void setup()
{
box2d = new PBox2D(this);
box2d.createWorld();
size(500, 500);
}
void draw()
{
if (!once)
{
Vec2[] screenPositions = new Vec2[3];
screenPositions[0] = new Vec2(250, 250); // center of screen
screenPositions[1] = new Vec2(0, 0); // upper left corner
screenPositions[2] = new Vec2(500, 500); // lower right corner
for ( int i = 0; i < 3; i++ )
{
Vec2 worldPos = box2d.coordPixelsToWorld(screenPositions[i]);
print("screenPos.x = " + screenPositions[i].x + " screenPos.y = " + screenPositions[i].y);
println();
print("worldPos.x = " + worldPos.x + " worldPos.y = " + worldPos.y);
println();
println();
}
once = true;
}
}
gives the following output in the message area:
screenPos.x = 250.0 screenPos.y = 250.0
worldPos.x = 20.0 worldPos.y = 20.0
screenPos.x = 0.0 screenPos.y = 0.0
worldPos.x = -5.0 worldPos.y = 45.0
screenPos.x = 500.0 screenPos.y = 500.0
worldPos.x = 45.0 worldPos.y = -5.0
So it seems that a screen coordinate with x = 0 corresponds to a world Position with x coordinate = -5, and a screen coordinate with y = 500 (or, generally speaking, the width of the window, as I've confirmed by testing with different sizes of the window) results in a world position with y coordinate = -5.0. The scaling factor is always 1/10.
Is there a reason why the origin of the Box2D world coordinate system doesn't correspond to the center of the screen? I don't have much familiarity with either box2d or Processing, and I jumped directly into chapter 5 of "The Nature of Code", so apologies if I'm missing something that ought to be common knowledge.
According to "The Nature of Code", the coordinate system for Box2D is a standard cartesian one, whereas for Processing it's one with the origin at the top left of the window, with y-coordinate increasing downward and x-coordinate to the right.
However, testing the coordinate conversion functions in pbox2d gives a different picture:
gives the following output in the message area:
So it seems that a screen coordinate with x = 0 corresponds to a world Position with x coordinate = -5, and a screen coordinate with y = 500 (or, generally speaking, the width of the window, as I've confirmed by testing with different sizes of the window) results in a world position with y coordinate = -5.0. The scaling factor is always 1/10.
Is there a reason why the origin of the Box2D world coordinate system doesn't correspond to the center of the screen? I don't have much familiarity with either box2d or Processing, and I jumped directly into chapter 5 of "The Nature of Code", so apologies if I'm missing something that ought to be common knowledge.