I've been trying to learn libgdx a little but now i'm kinda confused about world units there that i don't even know what exact question i should be asking.
Anyway, i've been reading this example game code here
and from what i understand, when there's no camera, SpriteBatch
renders everything in relation to device resolution, so pixels, but when we make a camera, set it's "size", position and then use batch.setProjectionMatrix(camera.combined)
, batch translates pixels to units and then it knows where to render , but in this game there's collision detection between Rectangle
objects, and when you're setting position of this Rectangle
with rect.set(x, y, 1, 1);
where x
and y
are world units and not pixels, how does rectangle know if it should use those x
and y
as units and not as pixels if there's nothing used like setProjectionMatrix
to let it know that now we're working in units and not in pixels, and then there's this 1, 1);
at the end, is it in units too? if so, then how does Rectangle know how big those units shoud be (Scale in game is 1 / 16 for example) renderer = new OrthogonalTiledMapRenderer(map, 1 / 16f);.
I don't even know if this question really makes sense, but that's where i'm at and i'm really lost here
EDIT: Ok, i understand how units work now, but collision still confuses me a bit, here's example, i created this
// onCreate
mapSprite = new Sprite(new Texture(Gdx.files.internal("space.png")));
planetTexture = new Texture(Gdx.files.internal("planet.png"));
shapeRenderer = new ShapeRenderer();
//Planet(X,Y,Radius)
planet = new Planet(20, 30, 7);
planet2 = new Planet(70, 50, 8);
circle = new Circle(planet.getPosition().x + planet.radius, planet.getPosition().y + planet.radius, planet.radius);
circle2 = new Circle(planet2.getPosition().x + planet2.radius, planet2.getPosition().y + planet2.radius, planet2.radius);
mapSprite.setPosition(0,0);
mapSprite.setSize(133, 100);
stateTime = 0;
camera = new OrthographicCamera();
camera.setToOrtho(false, 133, 100);
camera.position.set(133 /2, 100 /2, 0);
camera.update();
batch = new SpriteBatch();
...
// Render Method
batch.setProjectionMatrix(camera.combined);
batch.begin();
mapSprite.draw(batch);
batch.draw(planetTexture, planet.getPosition().x, planet.getPosition().y, planet.radius * 2, planet.radius * 2);
batch.draw(planetTexture, planet2.getPosition().x, planet2.getPosition().y, planet2.radius * 2, planet2.radius * 2);
batch.end();
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.setColor(Color.RED);
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.circle(circle.x, circle.y, circle.radius + 1);
shapeRenderer.end();
And everything renders fine, the ShapeRenderer
circle is where it's supposed to be after setting ShapeRenderer's projection matrix shapeRenderer.setProjectionMatrix(camera.combined)
:
But, i still don't understand how to check collision on this circle, when i do this in render method and press on the circle nothing is happening, like i don't get that log and i assume that's because the circle doesn't know the world scale (no nothing like setprojectionmatrix
) and render coordinates and where the actual circle "thinks" it is don't match. How do i check collision on that circle?
if(Gdx.input.justTouched()) {
if(circle.contains(Gdx.input.getX(), Gdx.input.getY())) {
Gdx.app.log("SCREEN", "TOUCHED AND CONTAINS");
}
}
EDIT2: I get it now, everything works, i just wasn't using camera.unproject
on touch coordinates
See Question&Answers more detail:
os