Reputation: 1387
Basically the title. I've got the camera set up to have the bottom left corner at the origin of the cartesian plane. All my box2d numbers scale this way and everything looks good, 30 m width, 20m height.
For some reason, the input seems to still be in pixels rather than meters which makes sense, but the Y values start at the top of the screen and go down.
Anybody know why that is? Is that just the way android or openGL do it?
Anyways to fix it, do I just use simple ratios to get the input into the same scale the other components use, and then take y input values as maxYvalue-y? Or is there a different, better option?
Upvotes: 1
Views: 432
Reputation: 73
If you render your game via a camera, you should use camera.unproject
to convert a (screenX, screenY)
screen position to (cameraX, camerY)
camera's position. Then with (cameraX, camerY)
you can easily convert to your Box2D position (or may be not need, because (cameraX, camerY)
is Box2D position in some cases). Example code:
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
Vector3 v3 = new Vector3(screenX, screenY, 0);
camera.unproject(v3);
// now v3.x and v3.y is the camera's position of your touch
return true;
}
Note: I do not advice to create a new Vector3
each times you receive a touch input, this is just for quick example. You should create your own temporary Vector3
in global scope or class scope.
Upvotes: 4
Reputation: 7635
Most graphics programs set the 0,0 coord as the upper left of the screen. Your description is pretty much the best way to fix it. What I've done before is write explicit functions to convert from each coord system. like BoxToPx and PxToBox. This way you can change how these functions work at a later date and your whole program will just work.
Upvotes: -1