2D Moving Camera (LWJGL) - java

I'm currently developing a 2D RPG with LWJGL, and am still in the engine stage of development. I've got a lot of the tech I want created, but one of my big problems is fixing the camera on the player. All the solutions I've seen involve moving the world and keeping the player still, which can work, but it seems apparent that this can cause some calculation issues if not closely monitored. Normally, I'd write a system where I wouldn't have to worry about it, but I refuse, because I eventually intend on adding multiplayer capability, where a moving world would be unplayable.
Is there a way to affix the camera to an object or point that can move WITHOUT using translate to move the world around? Also, I'd like to avoid Slick if possible. That would require me to rework much of my game engine as it currently stands.

Whenever you are going to project the 3d viewport onto a 2d screen you need to move everything according to the point of view of the observer (the so called camera or view).
I guess you can't escape from this. What you usually do is having a Camera object which holds position and rotation that is used to build the view matrix which is passed to the vertices of your scene through a uniform to the shaders. Passing transformation matrices to shaders is the normality so you shouldn't feel burdened by it. You can always premultiply it with the perspective matrix.
You must move the whole world to match the position of your camera just because you need to transform everything in your scene as it is seen from that point of view, otherwise how could you then project it on your screen? There is no "move the camera, keep the world still" concept.

Move the world visually, it's how every other RPG does it. Don't move the actual world's location though.
Draw everything but the ui normally, than translate it all according to the players position (i.e. glTranslate2f(-player.x,-player.y)). This is all done in the render method. On networked multiplayer, the viewport is done to that specific player (i.e. Bob's screen is translated based off Bob's position, Jane's is translated based off Jane's position). Should you instead want single-screen multiplayer, you will probably have to use mutliple framebuffers (one per player), and use them as viewports.

Related

Fog in openGL dependent on camera position

I am trying to make a simple game using OpenGL. I have already modeled 3D scene. Now I want to add some fog to make it look like in horror. I have used this code below
gl.glEnable(GL2.GL_FOG);
gl.glFogi(GL2.GL_FOG_MODE,GL2.GL_LINEAR);
gl.glFogi(GL2.GL_FOG_START,(int)1);
gl.glFogi(GL2.GL_FOG_END,(int)5);
However that just creates a static fog with some random coordinates. How to make It dependent on the camera movement? So the player will only see clearly on the set distance?
You can find the definition of glFog (and all the other OpenGL commands, even the classic ones) at www.opengl.org. Fog is always calculated according to the distance from the eye / camera / viewpoint coordinates, so you don't need to do anything special.
Be warned that glFog is part of the old classic OpenGL API and may not work reliably on modern 3D systems.

LibGDX make the world move and the player not

I'm working on a simple game with libGDX and want to make the main character to be fixed in the center of the screen and the world move when I press a button. I was wondering how to do... I was thinking to add a physicsBody to the world that contains other bodies and apply impulses to it when the button is pressed, is this possible in libGDX? And if it is, can i apply other impulses or forces to the bodies contained in the world's physicsBody? I think this way would be the best for me if it is possible, because i have to work a lot with physics, but if you have other ideas tell me please
There's no need to think about applying forces to all the non-character objects, that's just going to get messy very quickly.
The simple solution is to move your camera so that it always looks at your character. So your game loop may look something like:-
Process input
Update physics, character and other entity positions.
Move camera to point at character's new position.
Render
This way, you can update your game world without having to think about the camera at all. Then, when it comes to rendering, you can position your camera and render your graphics without needing to know anything about the game physics. It keeps the physics and rendering relatively independent, and makes it much easier to change things in the future.
For example, you may later decide that you want the camera to follow your character for the most part, but then follow a baddy whilst it is their turn. This is now easy to do, you just specify the character / entity to look at in your game logic, and then position the camera to look at whatever target that is, before you render.

Multiple cameras in libgdx ( probably similar in other frameworks )

I have been trying to solve this problem for two days and I have given up trying to find an existing solution.
I have started learning libgdx and finished a couple of tutorials. And now I have tried to use all that I have learned and create a simple side scrolling game. Now, I know that there are libgdx examples of this, but I haven't found a one that incorporates Box2d with scene2d and actors as well as tiled maps.
My main problem is with the cameras.
You need a camera for the Stage (which as far as I know is used for the projection matrix of the SpriteBatch passed to the method draw() at actors, if this is wrong please correct me) and you need a camera for the TileMapRender for calling the render() method. Also, in some of the tutorials there is a OrthographicCamera in the GameScreen, which is used where needed.
I have tried to pass a OrthographicCamera object to methods, I have tried to use the camera from the Stage and the camera from the TileMapRenderer everywhere.
Ex.
OrthographicCamera ocam = new OrthographicCamera(FRUSTUM_WIDTH, FRUSTUM_HEIGHT);
stage.setCamera(ocam); // In the other cases i replace ocam with stage.getCamera() or the one i use for the tileMap Render
tileMapRenderer.render(ocam);
stage.getSpriteBatch().setProjectionMatrix(ocam.combined); // I am not sure if this is needed
I have also tried to use different cameras everywhere.
After trying all of this I haven't noted what happens exactly when but I will list what happens :
There is nothing on the screen ( Probably the camera is away from the stuff that is drawn )
I can see the tiled map and the contours from the debugRenderer (I use debugRender too but I don't think that it interferes with the cameras), but the sprite of the actor is not visible ( probably off screen )
I can see everything that I should but when I try to move the Actor and the Camera, which is supposed to follow him, the sprite goes faster than the body ( the green debug square ).
So my main questions are :
I don't understand what happens when you have multiple cameras. "Through" which one do you actually see on the montior?
Should I use multiple cameras and how ?
Also, I thought that I should mention that I am using OpenGL ES 2.0.
I am sorry for the long question, but I thought that I should describe in detail, since it's a bit complicated for me.
You actually see through all of them at the same time. They might look at a completely different world though, but all of them render their point of view to the screen.
You can use several cameras, or just one. If you use only one you need to make sure that you update the projection matrix correctly, between drawing the TiledMap, your Stage with Actors and maybe for the optional Box2DDebugRenderer.
I'd use an extra Camera for the Box2DDebugRenderer, because you can easily throw it away later. I assume you use a conversion factor to convert meters to pixels and the other way around. Having a 1:1 ratio wouldnt be very good. I always used something between 1m=16px and 1m=128px.
So you initialize it this way, and use that one for your debugging renderer:
OrthographicCamera physicsDebugCam = new OrthographicCamera(Gdx.graphics.getWidth() / Constants.PIXEL_PER_METER, Gdx.graphics.getHeight() / Constants.PIXEL_PER_METER);
For your TiledMapRenderer you may use an extra camera as well, but that one will work in screen-coordinates only, so no conversion:
OrthographicCamera tiledMapCam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
The TiledMap will always be rendered at (0, 0). So you need to use the camera to move around on the map. It will probably follow a body, so you can update it via:
tiledMapCam.position.set(body.getPosition().x * Constants.PIXELS_PER_METER, body.getPosition().y * Constants.PIXELS_PER_METER)
Or in case it follows an Actor:
tiledMapCam.position.set(actor.getX(), actor.getY())
I actually haven't used scene2d together with Box2D yet, because I didn't need to interact very much with my game objects. You need to implement a custom PhysicsActor here, which extends Actor and builds the bridge from scene2d to Box2D by having a body as a property. It will have to set the Actors position, rotation etc based on the Body at every update-step. But here you have several options. You may re-use the tiledMapCam and work in screen-coordinates. In this case you need to always remember to multiply with Constants.PIXELS_PER_METER when you update your actor. Or you will use another cam with the same viewport like the physicsDebugCam. In this case no conversion is needed, but I'm not sure if this might interfere with some scene2d-specific things.
For a ParallaxBackground you may use another camera as well, for UI you can use another stage and another camera again... or reuse others by resetting them correctly. It's your choice but I think several cameras do not influence performance much. Less resetting and conversions might even improve it.
After everything is setup, you just need to render everything, using the correct cameras and render every "layer"/"view" on top of each other. First a ParallaxBackground, then your Tiledmap, then your Entity-Stage, then your Box2DDebugging view, then your UI-stage.
In general remember to call spriteBatch.setProjectionMatrix(cam.combined); and using cam.update() after you changed anything of your camera.

Third Person Perspective Camera with Opengl

I want to implement a TPP Camera for my project, but something is not working + i don't know if i use the right concept.
Should I rotate the whole scene model-view matrices except my main model, which will be centered on the screen or rotate the lookAt camera?
Other thing is how to make the model move in given direction after rotating? (I think moving the whole scene makes it easier?) + how to add collision detection to it?
Collision detection is nothing to do with openGL you use you game state variables to work that out you can manage it in the same loop as the game where you do user input and display.
You should use a LookAtMatrix for the third person camera you will have the eye component behind the player and the at somewhere infront. Persective can be implemented by using a perspective matrix.
So the matrix multiplication will look like.
PerspectiveMatrix * LookAtMatrix * worldSpacePosition
Here is a good answer from gamedev explaining a lookatmatrix, most OpenGL / Computer Graphics books will also cover this.
Are you working with the new or old pipeline model?

JOGL - open GL - keeping part of the world stationary while moving the rest

I'm kind of new to JOGL, and I am working on a game. I have a rocket, stars in the background, and planets. I want the rocket to remain stationary at the bottom of the screen, the GO button to be pressed, and then planets start coming down for the rocket to avoid.
I keep ending up with hitting go, the planets coming down (via translation), and the rocket also going down. I know Why this is happened with the camera moving, so the whole world is moving. I've been trying to use the pushMatrix and popMatrix, but haven't had any luck.
i.e.
if (goButtonPressed)
{
//gl.glTranslatef(0.0f,0.3f,0f); // this line just keeps the whole thing still
drawRocket(gl); // I was trying to 'undo' the translation
gl.glPushMatrix();
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glTranslatef(0.0f, -0.3f, 0f);
gl.glPopMatrix();
}
Is what I am trying to do possible in opengl? Am I making it too difficult?
Instead of relying on the projection matrix to hold the state of your rockets' positions, how about storing this information in your own code and then passing absolute coordinates to JOGL? Then updating the position of an object can be done with a more obvious bit of code like rocket.y += dy;.
As a small aside, the projection matrix is not intended to be used for spatial transformations. Use GL_MODELVIEW instead to avoid subtle problems down the road.
Not exactly an answer to your question, but you should look at JMonkeyEngine if you want to make a game with opengl in Java.
http://jmonkeyengine.com/
The thing you are trying to do is certainly possible with opengl and fairly easy to do with JMonkeyEngine. They have plenty of tutorials and such on their site.

Categories

Resources