I have a rectangular group that holds a bunch of sprites. Sprites are moving (say up). As they reach the top, they collide with the boundary. I have set up logic that kills the sprites as they exit the group. However, the way it is now, I can see the sprite leaving the group. I want to make it so that as the sprite is crossing the boundary, it's gradually disappearing until it's out (and dead).
Here's a crude mockup of what I want to achieve.
I was playing around with cameras thinking I have to modify the viewport, but it's not working. What's the proper way of achieving this effect?
Thanks!
EDIT:
My camera was set up like this:
camera = new OrthographicCamera(width, height);
camera.setToOrtho(false, width, height);
camera.position.set(width/2, height/2, 0);
Right now it's operating from the group's parent level (layer). Width and height are Gdx.graphics.getWidth(), etc.
My update():
camera.position.x = (width / 2) - resolver.getxPixelOffset()*parallax;
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
g.drawGroup(batch);
batch.end();
I moved my camera controls to my group class. I then changed the width and height to groupWidth and groupHeight. Since the group is not the entire screen, I figured it has to be smaller. This made the group massive. I don't want to change the size (or zoom?) of the group :(
I haven't tested this myself, but I believe the proper way of doing this would be using scissors. Basically, the scissors allow you to clip a region out of your picture, and only allow things in that region to be drawn, so you would probably do something like similar to what they do in the demo here:
Rectangle scissors = new Rectangle();
Rectangle clipBounds = new Rectangle(x,y,w,h);
ScissorStack.calculateScissors(camera, spriteBatch.getTransformMatrix(), clipBounds, scissors);
ScissorStack.pushScissors(scissors);
spriteBatch.draw(...);
spriteBatch.flush();
ScissorStack.popScissors();
You may need to draw and flush your background image (if you have one) before you draw with the scissors.
Related
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.
Does it matter what height and width I use for my game? In my game I have a few polygons, they all are connected to images that I have painted, but when I have width and height as 800x480 I have to paint the images very small, this causes them to get blurry. Also, I don't really understand how this behaves on different sized phone screens.., does the images I have painted get streched, or do they stay small, even on big tablets? So my question is basically what is the optimal width and height to have on a libgdx game?
This is a part of my code, to maybe help you understand what I mean
WIDTH = Gdx.graphics.getWidth();
HEIGHT = Gdx.graphics.getHeight();
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480 );
What will happen if I change these values? What would it be best to change them to?
I was reading on a tutorial of sorts, but they didn't really talk about this part.
Do I have to do something complicated to get my painted images to look decent on all devices?
EDIT:
This is how I currently implement a cloud into my game:
public class AnotherScreen implements Screen{
Polygon cloudPoly;
Texture cloud;
#Override
public void render(float delta) {
batch.setProjectionMatrix(camera.combined);
batch.begin();
renderBackground();
batch.draw(cloud,cloudPoly.getX(),cloudPoly.getY());
batch.end();
}
#Override
public void show() {
cloud = new Texture(Gdx.files.internal("assets/textures/cloud.png"));
cloudPoly = new Polygon();
cloudPoly.setPosition(MathUtils.random(350,600),MathUtils.random(380,440));
// theses vertices are just a little square atm
float[] cloudVertices = new float[] {
0,0,
0,20,
20,20,
20,0
};
cloudPoly.setVertices(cloudVertices);
}
}
The cloud image has width 256p and height 128p, when I set 'camera.setToOrtho(false, 800, 480 );' won't this cloud always stay in proportion?
I am sorry if I am unclear, I am a new programmer and my english could be better, love you doh for staying with me!
does the images I have painted get streched, or do they stay small, even on big tablets?
One of the greatest features of libgdx is that it lets you do things exactly as you want them. So, it is really your choice. With this snippet
WIDTH = Gdx.graphics.getWidth();
HEIGHT = Gdx.graphics.getHeight();
camera = new OrthographicCamera();
camera.setToOrtho(false, WIDTH, HEIGHT);
You would have a different viewport size in every different screen size. So your images would stay small.
In the other hand, if you use predefined values for the viewport like:
WIDTH = 800;
HEIGHT = 480;
camera = new OrthographicCamera();
camera.setToOrtho(false, WIDTH, HEIGHT);
You would get the same viewport for every screen and the images would stretch to occupy the same space in each axis. This won't necessarily be the same for each axis. So a perfect square image in one screen may look like a rectangle in another screen with a different x-y ratio.
There is a third option, you can fix one side and let the other be calculated, so you keep the ratio of your images (a square stays as square). You must decide what will be the fixed side. For a game like Super Mario Bros I would recommend you to fix the height and calculate the width:
HEIGHT = 480;
WIDTH = (Gdx.graphics.getWidth()/Gdx.graphics.getHeight())*HEIGHT;
camera = new OrthographicCamera();
camera.setToOrtho(false, WIDTH, HEIGHT);
Every screen would see the same height in the game world. But some screens would see more in the width than others. If thats a problem for you (you want them to see the same ammount of the world but keep the ratio too), than adding black bars to each side is an option.
So my question is basically what is the optimal width and height to have on a libgdx game?
The optimal solution is having a different set of images for several resolutions. And choosing whats the most appropiate at runtime. But that increases the game size a lot.
Another dirty way is having only one big set and let the images shrink in lower resolutions. It looks better than the inverse. But some people think this is just too dirty :p (not me, so I recommend it).
I am using scene2d for a board game. I want the board to be zoomable and movable inside its assigned rectangular part of the screen, while the rest of the screen stays the same.
I have tried to use a separate stage for the board, but haven't found a way to initialise viewports and cameras that does this correctly.
I tried the following code in Screen.resize(int width, int height) as a test, but the boardStage becomes stretched vertically, and when zoomed in (like here), it fills the whole screen.
hudStage.setViewport(8, 12, true, 0, 12, width, height);
boardStage.setViewport(8, 8, true, 0, 10, width, width);
OrthographicCamera cam = (OrthographicCamera) boardStage.getCamera();
cam.zoom =.5f;
What is the correct way to do this?
We have implemented a similar zoom for our board game.
https://play.google.com/store/apps/details?id=net.peakgames.mobile.rummi.android
We are using a second camera which is zoomed:
A scissorStack is used to clip the second camera rendering area.
Second camera is enabled only when there is a touch/drag on the board.
we are changing the camera position as the user drags on the board.
hope this helps..
I think you want to use a scene2d ScrollPane instead of a new Stage. I'm not entirely sure how you'd do the zooming with this, but I think the scaling stuff should work.
I was trying to follow example codes to simply display a rectangle on a black background, but it didn't seem to be displaying. What I did was
private static void initGL(){
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,Display.getWidth(),0,Display.getHeight(),-1,1);
glMatrixMode(GL_MODELVIEW);
glDisable(GL_DEPTH_TEST); //2D mode
glColor3f(0.5f, 0.0f, 1.0f);
glBegin(GL_QUADS);
glVertex2f(-0.75, 0.75);
glVertex2f(-0.75, -0.75);
glVertex2f(0.75, -0.75);
glVertex2f(0.75, 0.75);
glEnd();
}
It doesn't display anything on the screen except for a black background. Does anyone know what I might have done wrong? I'm using lwjgl in eclipse.
First things first: You only have to run the whole
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,Display.getWidth(),0,Display.getHeight(),-1,1);
glMatrixMode(GL_MODELVIEW);
thing once during your program, probably shortly after you run Display.create().
Also, you're tessellating using the wrong vertices. You wrote
glVertex2f(-0.75, 0.75);
glVertex2f(-0.75, -0.75);
glVertex2f(0.75, -0.75);
glVertex2f(0.75, 0.75);
which means draw a rectangle from (-0.75, -0.75) pixels to (0.75, 0.75) pixels. This is too small to be noticed. My guess is you assumed glVertex2f deals with fractions of the display width. It does not. glVertex2f deals with actual coordinates, it just allows fractional pixels, unlike glVertex2i (this is useful believe it or not, it helps with smoother animations). Something like
glVertex2f(100F, 100F);
places a vertex at (100, 100), and is effectively equivalent to
glVertex2i(100, 100);
Also, remember that negative pixels will be rendered off the screen, because OpenGL's origin of the coordinate system, (0, 0), is in the lower left and behaves like the first quadrant from the coordinate system in math class, not like the traditional computer coordinate system with (0, 0) in the upper left.
As for the the black background, LWJGL's Display has a black background by default, so it's recommended to draw a quad with your background color that covers the entire display width and height. One quad won't really affect your performance.
glVertex2f uses same size units as your glOrtho so unless your display width and height are in units of ones, like 10 or less, you may not see anything!
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.