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.
Related
im trying do develop a Zelda like game. So far i am using bitmaps and everything runs smooth. At this point the camera of the hero is fixed, meaning, that he can be anywhere on the screen.
The problem with that is scaling. Supporting every device and keeping every in perfect sized rects doesnt seem to be that easy :D
To prevent that i need a moving camera. Than i can scale everything to be equally sized on every device. The hero would than be in the middle of the screen for the first step.
The working solution for that is
xCam += hero.moveX;
yCam += hero.moveY;
canvas.translate(xCam,yCam);
drawRoom();
canvas.restore();
drawHero();
I do it like this, because i dont wand to rearrange every tile in the game. I guess that could be too much processing on some devices. As i said, this works just fine. the hero is in the middle of the screen, and the whole room is moving.
But the problem is collision detection.
Here a quick example:
wall.rect.intersects(hero.rect);
Assuming the wall was originally on (0/0) and the hero is on (screenWitdh/2 / screenHeight/2) they should collide on some point.
The problem is, that the x and y of the wall.rect never change. They are (0/0) at any point of the canvas translation, so they can never collide.
I know, that I can work with canvas.getClipBounds() and then use the coordinates of the returned rect to change every tile, but as I mentioned above, I am trying to avoid that plus, the returned rect only works with int values, and not float.
Do you guys know any solution for that problem, or has anyone ever fixed something like this?
Looking forward to your answers!
You can separate your model logic and view logic. Suppose your development dimension for the window is WxH. In this case if your sprite in the model is 100x100 and placed at 0,0, it will cover area from 0,0 to 100, 100. Let's add next sprite (same 100x100 dimension) at 105,0 (basically slightly to the right of the first one), which covers area from 105,0 to 205,100. It is obvious that in the model they are not colliding. Now, as for view if your target device happens to be WxH you just draw the model as it is. If your device has a screen with w = 2*W, h = 2*H, so twice as big in each direction. You just multiply the x and y by w / W and h / H respectively. Therefore we get 2x for x and y, which on screen becomes 1st object - from 0,0 to 200, 200, 2nd object - from 210,0 to 410, 200. As can be seen they are still not colliding. To sum up, separate your game logic from your drawing (rendering) logic.
I think you should have variables holding the player's position on the "map". So you can use this to determine the collision with the non changing wall. It should look something like (depensing on the rest of your code):
canvas.translate(-hero.rect.centerX(), -.rect.centerY());
drawRoom();
canvas.restore();
drawHero();
Generally you should do the calculations in map coordinates, not on screen. For rendering just use the (negative) player position for translation.
So i have a created a background which is 1920 x 1080 in size.
And i have a window that is 1280 x 720 (will not always be this size)
Now when i render the sprite using this code:
#Override
public void render(float delta)
{
Gdx.gl20.glClearColor(0.2F, 0.5F, 1F, 1F);
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
sb.begin();
sb.draw(Assets.splash_spr_bg, 0, 0);
sb.end();
}
The sprite appears in the window but not the whole sprite, just like 1/2 of it.
Now here's my question:
·How can i make the sprite fill the whole screen no matter what size the screen is?
Thanks for any help! :)
I recommend using Viewports for this: https://github.com/libgdx/libgdx/wiki/Viewports
Its a great way to handel screen sizes.
To make it fill the whole screen u probably have to try something like:
Picture1.setX(stage.getWidth() / 2 - Picture1.getWidth() / 2);
Picture1.setY(stage.gethight() - Picture1.getHeight() /)
In this example i use a stage, but it will work without just remember that u get the screen width and height instead or set them to 0,0 and place it under in the corner. Also notice that i cut the width in half, this is because it will otherwise always render outside your screen. Then u could try and use something like:
Picture1.setSize(stage.getWidth() / 2, stage.getHeight()):
the other way u could do it is by setting your viewport zoomed in on the picture (so the same size) that way it will also give you a full screen view even tho i dislike that idea, but thats a personal preference. Also this later one wont make your background look all stretched and pixelated because it isn't being resized, but if u take a picture thats big enough you also wont notice it in the first option.
Just mess around a little with it, its pretty easy.
Example of how to implement Viewport: http://pastebin.com/3SrWKcEg
When I try to move an opengl quad outside the size of the main window, it doesn't render.
Say if I have a window 600x800. Then I resize it to be full screen. Once I move the quad(player) outside the defined window properties, it disappears.
I think this is essential because I'm working on a top down RPG and he will be moving around on the x and y plane. Also if that isn't enough, how would I make a camera that keeps the player in the center...
So far I have
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 845, 0, 520, -1, 1);
glMatrixMode(GL_MODELVIEW);
I havent been able to test it due to the first problem
On the part to keep the camera in the center. Just translate everything -x, -y relative to the camera/player's position.
glTranslatef(-cameraX, -cameraY, 0);
So when you move the player change the players position and translate everything the opposite way.
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.
In the two attached pictures, the desktop screenshot of libgdx functions as expected. The screenshot from my Galaxy Nexus is unfortunately not as expected. I am attempting to create a simple motion blur or trail effect.
Rendering as I expected on my desktop.
Not rendering as I expected on my Galaxy nexus.
The circle textures are drawn in a for loop during rendering and the effect is achieved with a pixmap using the RGBA of 0, 0, 0, 0.1f that is drawn before the circles.
screenClearSprite creation
Pixmap screenClearPixmap = new Pixmap(256, 256, Format.RGBA8888);
screenClearPixmap.setColor(Color.rgba8888(0, 0, 0, 0.1f));
screenClearPixmap.fillRectangle(0, 0, 256, 256);
screenClearTexture = new Texture(screenClearPixmap);
screenClearSprite = new Sprite(screenClearTexture);
screenClearSprite.setSize(screenWidth, screenHeight);
screenClearPixmap.dispose();
Render
batch.begin();
font.draw(batch, "fps:" + Gdx.graphics.getFramesPerSecond(), 0, 20);
screenClearSprite.draw(batch);
for (int i = 0; i < circleBodies.size(); i++) {
tempPos = circleBodies.get(i).getPosition();
batch.draw(circleTexture, (tempPos.x * SCALE) + screenWidthHalf
- circleSizeHalf, (tempPos.y * SCALE) + screenHeightHalf
- circleSizeHalf);
}
batch.end();
So, what did I do wrong? Perhaps there is a better way to get the 'motion blur' effect of movement?
Here is a different approach, where you clear your screen each time with solid color and no alpha.
This means that you will have to modify your code some. The good thing about this, is that the way you are doing it has some flaws: It will blur everything in motion, not just the balls. And can quickly produce ugly results/artefacts unless you are careful.
Do the same as you are doing now, but instead of drawing the balls to the batch, draw them onto a texture/bitmap/whatever. Then each frame add an alpha-blended image over the balls-image, and then draw the balls in their current position on top of that. Then add that image to your screen. Very much like you are doing now, except you draw to something else and keep it. This way you don't have to rely on the viewport you are drawing onto, and can keep everything separated.
This method is similar to drawing to an accumulation buffer.
Instead of doing it the way you are doing, you can keep track of the n latest positions of each ball. And then draw all of them each frame, with different alpha. This is very easy to implement. Can result in many drawing calls if you have many balls or a large n, but if it's not too much it shouldn't limit your fps and gives nice control.
Perhaps there is a better way to get the 'motion blur' effect of
movement?
in order to make motion blur in my game i use another approch "The particle effect" it works realy fine with me and i didn't have Android/Desktop problems or with different android devices
all you have to do is to use "Particle Effect Editor" of Libgdx and make your effect then load it in your project finally draw it at the same position you draw your object (and alos draw your object)
Tips to make the right effect file with Paticle Editor :
set (use) the same image of the object that you want to blur it motion in the particle effect
try to limit the count : the max number of particle allowed
Disable the "velocity" and "Angle"
parameter
Particle effect help to do motion effect
Hope this will help someone !