I'm having issues with the Camera class in libGDX, I just can't visually move it even though its position changes.
This is how I setup the camera:
camera = new OrthographicCamera(frustumWidth, frustumHeight);
This is how I change its position:
world.onUpdate(deltaTime, camera);
renderer.render(world);
camera.position.set(MathUtils.random(0, 800), MathUtils.random(0, 480), 0);
//camera.position.set(
// world.dynamicObjects.get(GameWorld.MainPgID).pos.x * GameWorld.frustumToWorldRatio,
//world.dynamicObjects.get(GameWorld.MainPgID).pos.y * GameWorld.frustumToWorldRatio, 0);
I submit the changes at the beginning of the .render function:
camera.update();
batcher.setProjectionMatrix(camera.combined);
batcher.begin();
polygonBatcher.begin();
As I've said the position does change, and yet of the many combinations I've tried none works.
Perhaps I misunderstood how the Camera works and I need to move the objects and not the camera? Seems to be stupid to me, after all it's called camera for a reason.
You have to apply the combined projection matrix on every batch/renderer you want it to affect.
But besides of that it seems to be fine. Try to debug it step by step.
Setup a simple project with only new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); and some lines (ShapeRenderer) or sprites (SpriteBatch), set the projection matrix to camera.combined, change camera.position and update() the camera. That should do it.
Then when you change camera.position all the renderer with the camera's frustum should offset accordingly without changing the offsets of the sprites/geometry.
btw. the viewport is not 0 - 800, but rather -400 - 400 for x and respectively for y;
Related
I'am trying to make libgdx game, adn i've got 2 problems now:
1. When im using camera.setToOrtho(false, GAME_WIDTH, GAME_HEIGHT); my textrure of player is distored, one eye is bigger than another.
2. Im using this camera and viweport:
camera= new OrthographicCamera(GAME_WIDTH,GAME_HEIGHT);
camera.setToOrtho(false, GAME_WIDTH, GAME_HEIGHT);
viewport = new StretchViewport(GAME_WIDTH,GAME_HEIGHT,camera);
and when i do :
touchPos.set(Gdx.input.getX(1),Gdx.input.getY(1),0);
game.camera.unproject(touchPos);
System.out.println(touchPos.x+" "+ touchPos.y);
I get 0 0 in right top corner but my character witch cords 0 0 is drawing in left bottom corner.
When i use:
game.camera = new OrthographicCamera(820,480);
game.viewport = new FillViewport(820,480,game.camera);
i must use game.batch.setProjectionMatrix(game.camera.combined);
when i use this code i've got corectly working unproject method and coordinate system but i've got distored texture.
When i use:
camera = new PerspectiveCamera();
viewport = new StretchViewport(900, 470, camera);
i've got distored texture and bad coords system. and i can 't use :game.batch.setProjectionMatrix(game.camera.combined);
It is distorted because you are using a fixed width and height for the camera, so it simply stretches to fit whatever size window/screen you're putting it on.
Incidentally, StretchViewport does the same thing (distorts the scene to fit the dimensions you gave it to the window). But you just created a viewport without actually calling update() on it, so it's not doing anything.
You most likely want to use ExtendViewport or FillViewport to avoid stretching, but other options are available. Read about them here.
Your viewport won't properly respond to screen rotations (or desktop window resize) unless you put viewport.update(width,height) into the resize method. And you have to call update on it at least once regardless, using the actual screen dimensions. This is most convenient by simply putting viewport.update(width,height) into the resize method.
I'm making an Android game using LibGDX. I want to do something hard to explain but I'll try nonetheless.
Look at this camera view (it's from 3ds max):
I can get same exact view in LibGDX without any problems:
perspCam = new PerspectiveCamera(40, screenHeight, screenWidth * h / w);
perspCam.position.set(1, 6, 18f);
perspCam.lookAt(1, 3, -1f);
perspCam.update();
Now the tricky part. What I actually want to see in-game is part inside the red rectangle but filling the whole screen.
I have managed (using GlScissors) to make it look like this:
But I have no idea how to make it fit the whole screen.
Gdx.gl20.glEnable(Gdx.gl20.GL_SCISSOR_TEST);
Gdx.gl20.glScissor(0, 0, 450, 550);
modelBatch.render(instance);
modelBatch.flush();
Gdx.gl20.glDisable(Gdx.gl20.GL_SCISSOR_TEST);
modelBatch.end();
In stead of using glScissor, you should change camera position.
Make it nearer and shift it sideways too. The exact coordinates can be calculated properly.
Hope this helps.
I am having problems with my coordinates system. what I wanted to achieve is the the starting point of (0,0) is at the middle of the screen not in the lower bottom of the screen. I am encountering a problem on which involves the coordinates of the camera which my map appear smaller and my sprite bigger on screen and messes up the collision.
here is my code: click here for the code
Use:
cam = new OrthographicCamera(10*(w/h), 10);
without using:
cam.setToOrtho(false, 10*(w/h), 10);
cam.update();
afterwards.
I am making a 3d game with LWJGL. In this game, whenever I press an arrow key, I want the screen to rotate in that direction, regardless of the current orientation. I am struggling to implement this in code. Using three glRotatef functions based off of a rotation vector3f, does not accomplish this. Rotating up and down work because glRotatef(rotation.x, 1, 0, 0) is called first, but left and right only work when your not looking up or down. If you are, you rotate around a universal y axis, and camera spins. I saw that another implementation could use gluLookAt(), but I imagine I would encounter the same problem.
EDIT
I thought I solved my issue by changing the order by which glRotatef()'s where called depending on the direction I want to rotate. I thought this would work because in my game, I will only be rotating one axis at a time. It worked somewhat but in some orientations it doesn't.
if(updown){
glRotatef(rotation.x, 1, 0, 0);
glRotatef(rotation.y, 0, 1, 0);
} else if(leftright){
glRotatef(rotation.y, 0, 1, 0);
glRotatef(rotation.x, 1, 0, 0);
}
glTranslatef(position.x, position.y, position.z);
gluLookAt would probably get you there much quicker, but you would need to manually rotate the eye coordinate about the origin coordinate. With gluLookAt you also need to calculate an up vector if you plan to rotate around all 3 axes.
I have a hunch you just need to add a glTranslate before you do your glRotate so that the camera has something to orbit around.
If you show some code you might be able to get more help.
I am over this issue and have moved on. Because I am only going to rotate one axis 90 degrees at a time, I went through all 64 possible orientations and applied the necesary transforations manualy.
I want to render a particle effect in 3D using the Z coordinate. I've tried to implement a own ParticleEffect using Decals instead of Sprites without success.
Is there any other way to render a ParticleEffect using the Z coordinate? Maybe by manipulating the transformation Matrix of the SpriteBatch?
Update:
working code
// update projection each frame since my camera is moving
spriteBatch.setProjectionMatrix(camera3d.projection);
for (ParticleEffect effect : effects){
spriteBatch.setTransformMatrix(camera3d.view);
spriteBatch.getTransformMatrix().translate(x,y,z); // different for each effect
spriteBatch.getTransformMatrix().scale(0.1f,0.1f,0.1f); //optional
spriteBatch.begin();
effect.draw(spriteBatch, delta);
spriteBatch.end();
spriteBatch.getTransformMatrix().idt();
}
If your 3D effect is a parallax effect, meaning your particles face the camera perpendicularily, you can indeed set the transformation matrix of the SpriteBatch
batch.getTransformMatrix().idt().translate(0, 0, z);
batch.begin();
... do your rendering here
batch.end();
// reset the matrix, so you can use the batch for other stuff
batch.idt();
For a perspective effect you'll also have to use perspective projection. The easiest way to cope with this requirement is to use a PerspectiveCamera instead of an OrthographicCamera.