libGDX Coordinate System - java

I'm trying to properly configure my Camera and Sprites in libGDX to show up in a 2D coordinate system properly with the origin at the bottom left hand corner.
I set up my Camera like this:
cameraWidth = Gdx.graphics.getWidth();
cameraHeight = Gdx.graphics.getHeight();
camera = new OrthographicCamera(1, cameraHeight/cameraWidth);
And I set up my Sprites like this:
sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
sprite.setScale(scale);
sprite.setPosition(startX,startY);
My problem is with sprite.setSize(x,y). If I set all the sprites to have a size of (1, texture aspect ratio), then everything draws with the right display ratio (not smushed or stretched), but nothing draws in the correct place. For example, if I draw something at (0,0), it will draw with its bottom left corner off the left side of the screen and up a number of pixels.
I've noticed by changing around the ratio I can get things to draw in different places - namely if I set it to (1, display aspect ratio) things look pretty close to drawing in the right place - they just draw from their center, not their bottom left corner, as LibGDX specifies. The only problem is that the images all appear as smushed or stretched, which is no good.
This seems like a simple problem and I just want to know how to set this up so I can have a sensible coordinate system that draws things in the right place and in the right aspect ratio. Thanks.

Once you change your viewport to match the screen's aspect ratio then (0, 0) will no longer be at the bottom left of the screen unless the screen is square. If the screen is wider than it is high then the visible portion of the x axis will still go from 0.0 to 1.0, but 0.0 on the y axis will now be somewhere off the bottom of the screen.
If you adjust the camera so that (0, 0) is at the bottom left of the screen, and remember that the visible y axis will only go up to grapicsHeight / graphicsWidth then that should solve your coordinate problem.
I would recommend setting the camera to point to the middle of the screen rather than the bottom left. There's an example here that does exactly that, drawing a 2:1 rectangle which is always in the centre of the screen, always with a 2:1 ratio no matter how much you resize it.

I've found a solution to this problem:
Set the camera to ortho (even though it's already an orthographic camera)
camera.setToOrtho(false,1,screen height / screen width);
Also, each sprite must have its position set to (x - sprite.getWidth()/2, y - sprite.getHeight()/2. I extended the Sprite class and overrode the setPosition method to account for this. Now, every time the position is set, the Sprites end up going where you "would think they'd go", with setPosition(0,0) putting it in the bottom left and setPosition(1,height/width) in the top left.
Oddly enough, this draws every sprite centered around the (x,y) point, which would make sense since width/2 and height/2 were subtracted from the position, except not subtracting the values does not make setPosition center the sprite via the bottom left corner - it's centered in a way I haven't figured out.

Related

How to start an Activity in specific coordinates

My Activity has a Canvas with both width and height equal to 6000. When I start the Activity the upper left coordinate of my screen is (0,0) so screen' center is about (30,60). What I want is start the Activity with coordinates (3000,3000) in the center of screen
any solution?
Update 1:
I used this Kotlin code:
var fondo = Lienzo(this) //fondo is the Canvas View (6000,6000)
val scrollV = ScrollView(this)
val hscrollV = HorizontalScrollView(this)
scrollV.addView(fondo)
hscrollV.addView(scrollV)
layaout1.addView(hscrollV) //layaout1 is a RelativeLayout
More code would be nice since I cannot see where you are declaring the position but if you are hard-coding the coordinates, I would suggest instead of using the same variables for width and height and just putting them in the coordinates /2 so if your variables are width and height, it should be kind of like this depending on what you are using:
.setPosition(width/2, height/2);
If this isn't useful then please provide more info or more code to see where your mistake is :)
That's not how Canvases work in Android. You don't declare how big you want it to be and it magically scales to the screen. If you're in the View's onDraw function, the Canvas passed in is the size of the View, in physical pixels. You need to scale your drawing to it. If you're drawing to an offscreen bitmap first and then blitting that to the screen, its your job to scale the bitmap via a matrix when you blit it. Also, in Android the coordinate system is 0,0 as the upper left hand corner. Unless it greatly eases your drawing I suggest you not fight it.
However you could use a matrix transformation on the Canvas to change that. First you'd want to scale the matrix by 6000/view.getHeight() in the y and 6000/view.getWidth() in the x. Then you'd want to translate it by -3000 in the x and -3000 in the y. That should scale it to the view and move origin to center.

Libgdx Coordinate System Origin(0,0), not at bottom left of screen

I am working on a program to visualize an n-body particle simulation. To do this I have created a camera which is used to project particle positions unto the screen. Then, using a spritebatch these particles are rendered in the correct positions. However, for this to work, the origin of the coordinate system must be located at the bottom left of the screen. This is often not the case and instead the origin is located about a fourth of the screen up and to the left in all cases, and this changes when resizing the screen.
How do I guarantee the coordinate system origin is always at the bottom left of the screen, instead of elevated by a few hundred pixels in either axis?
Thank you.
If you decided to use sprite you should stick with proposed workflow and draw them with:
sprite.draw(batch);
I mean, what's the purpose using sprites if you are getting textures and drawing them?
When you draw a texture to screen at position (0,0) it means that it's bottom left corner will be at screen bottom left corner. That' is sprite's "hot-spot" or "origin" is at it's left bottom corner by default.
But you can change that with setOrigin method:
https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/Sprite.html#setOrigin-float-float-
That is, origin is kinda "center" of the sprite, so i.e. if it rotates it will rotate around that spot. Also when you draw sprite at some coordinates sprites origin will be exactly at those coordinates. That way you don't have to adjust sprite drawing position.
Check on that page what methods you have for sprite class...
I fixed my problem. Instead of using:
sprite.draw(batch);
I used:
batch.setColor(color);
batch.draw(sprite.getTexture(),sprite.getX(),sprite.getY(),sprite.getWidth()*sprite.getScaleX(),sprite.getHeight()*sprite.getScaleY());
I do not know why this worked.

LibGDX: Where is the X-/Y-Coordinate of a camera?

I'm wondering how the orthographic camera in LibGDX is positioned.
Is X bottom left or center or on right(etc)? And how it is with the Y?
I know its a simple question, but I'm messing around with my cam at this moment and I need some help :D
Libgdx camera coordinates is always CENTER of your screen.
For example if your viewportWidth and viewportHeight like
(800, 480)
it's coordinates will be
(400, 240)
In LibGDX, we have lots of coordinate systems (not only LibGDX, this applies to other engines/frameworks too). Camera is also a game object like other objects and thus is positioned like other objects.
The only difference about cameras is that they don't have width and height in the same sense of other objects. They are always a zero size point and can capture a rectangle (which is called viewport) centered on this point.
In your game if you use only one camera, what you see is the viewport that the only existent camera captures. So, if a sprite is on (0, 0) and also your camera is on (0, 0), you'll see that sprite exactly on the center of your screen.
Here's an example project using orthographic camera.

Quad moves when screen is resized verticly

I have a quad drawn on the screen, positioned at the top of the screen and centred. As I resize the screen so it is not as high, the quad appears to move in the opposite direction from what it should, IE, up at the same rate it should be going down on my monitor.
It is constantly being set to be at the top of the screen and centred each loop, before rendering.
this.location[0] = (Display.getWidth()/2)-(this.size[0]/2);
this.location[1] = 0;
Then I render the quad and print out its Y location. this consistently returns 0, no matter what I resize the window to. By printing out the mouse coordinates I can confirm that the top of the screen is 0 when it is resized. Why is it then, that even though I am setting the quad to be at 0, then rendering it exactly after, it still isn't drawn at 0?
Is there some Opengl or LWJGL concept I don't understand?
Here's the Opengl Setup code.
GL11.glEnable(GL_TEXTURE_2D);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,Display.getWidth(),Display.getHeight(),0 ,1, -1);
glMatrixMode(GL_MODELVIEW);
Since nobody has answered this, and I have found a solution, inefficient as it may be, here's an answer.
I Changed the line
glOrtho(0,Display.getWidth(),Display.getHeight(), 0 ,1, -1);
to
glOrtho(0,Display.getWidth(), 0,Display.getHeight(),1, -1);
Which means instead of the top being the top, the top is now the bottom of the screen. I then had to create another variable inside my box class, called realY which is the actual y coordinate of the box/quad's left hand corner.
Then with some simple math (this.realY = (Display.getHeight() - this.location[1])-this.size[1];) I convert the input Y into the upside down version of itself, so 0 becomes the height of the screen, and vise versa
To me, this seems like a inefficient solution, and more of a workaround. If anyone has a better answer, please post it.

libGDX spiriteBatch.draw() not positioning at the right place

Here is a sample code that is not correctly functioning for me, am I doing something wrong?
textureCharacter = new Texture(Gdx.files.internal("data/character1.png"));
if (Gdx.input.isTouched()) {
spriteBatch.draw(textureCharacter, Gdx.input.getX(), Gdx.input.getY());
}
When I touch the the SpiriteBatch at the location X=5 and Y=5 (for example) it draws me the texture at X 5 but Y is the Gdx.graphics.getHeight() - 5px ??? By moving the input Y down, the texture moves up...
Gdx.input.getX() and Gdx.input.getY() are returning the values: X=5, Y=5
What I'm trying to do is just move the texture to the input positions I'm touching/moving.
Screen coordinates are not necessarily the same as model-space coordinates. What does your camera definition look like? (Since that defines the mapping of model space to the screen.) The Gdx.intput.getX() call returns a point in screen space.
The Y-axis (by default) points in opposite directions on the screen and in GL space. (grows down from the top of the screen in screen coordinates, and grows up towards the top of the screen in GL coords). You can either fix your camera to match the screen coordinates or map your touch coordinates into GL coordinates.
See the call to camera.unproject() in:
https://code.google.com/p/libgdx/source/browse/trunk/tests/gdx-tests/src/com/badlogic/gdx/tests/examples/MoveSpriteExample.java

Categories

Resources