3rd Person Camera View - java

I have been trying to make a 3rd person camera in libgdx for the past couple of days and can't seem to figure out how to do it. I have tried the rotateAround function in PerspectiveCamera, but when I move the camera to be just behind the model its suppose to follow, the rotation gets messed up. I am at a loss at what to try now. I want the camera to be set back and just above the model and to follow it. If someone could point me in the correct direction, I would greatly appreciate it.

In your render method of your game you want to update the camera to follow the player at a distance and you also want to make sure that the camera is looking at the right position either at your character or just ahead if you want to get an over the shoulder view.
Depending on the scale of your models you may have to play around with these values.
In the render loop you want something like this:
note that in this example player is a vector3 and cam is a Perspective camera
This will make the camera look at the character. You may want to modify the values to make it look ahead (Change the x and z for that).
cam.lookAt(player.x, 0, player.z);
Here we set the location of the camera so we can see that it will always be floating behind and above the character
cam.position.set(player.x, 10f, player.y-20f)
This updates the camera to apply all of your transformations
cam.update();
About rotation i'm not too sure, i've not tried it. Heres an article that should help.

Related

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.

Math behind a 3rd person camera

I have been trying for quite some time now and I cant seem to find anything about 3rd person cameras. I just want to make a simple 3rd person camera but it gets difficult because I'm in 3d. So far I can do the camera on 1 plane but after that its messed up. I'm using libgdx with ModelInstances if anyone knows libgdx.
I don't need you to hand me the code for it, I just need help on how to write this out with the information I have. I know the velocity in x y and z, the rotation in x y z of the object, and its position in x y z. I can make the camera look at the object so all I have to do is position it behind and above it no matter what the orientation is for the object. Any links or explanations are greatly appreciated :)
Solution:
Xoppa posted in the comments a link to his chaseCam that extends perspective cam and It is amazing. Since it wasn't an answer I could mark it as accepted but it did what I needed. Heres the link chaseCamera.java All credit to Xoppa for the answer, just making it easier to find.
I would do on each frame after updating the player's position:
camera.up.set(0,1,0); //Not sure if this is necessary.
//Making sure up is always up after
//last frame's lookAt() call.
camera.postion.set(player.position)
.add(-UNITS_BACK, UNITS_UP, 0)
.rotate(Vector3.UP, player.angle);
camera.lookAt(player.position);
camera.update();
where player.angle is the number of degrees counter-clockwise the player is facing from the X-axis.
That's a simple starting point. You'll probably want to smooth out the movement by limiting the speed at which the camera can move or giving it a second order interpolated movement to this target position and direction.

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.

Get Rotation (Tilt) Angle In Landscape View - Android Java

I've scrounged the web in search of a good example or someone trying to attempt the same thing, but so far I haven't had much luck.
I am trying to get my Devices rotation angle (If you can imagine) starting at 180° (Flat).
As the user tilts the device left and right, the angle adjusts accordingly.
And as it rotates I want to simply update a TextView.
I'm just lost as to what this evolves, some people suggest a gyroscope, accelerometer, even a magnometer, or a combination.
I've gotten a few different sensors to work, but none of the examples I've followed accomplish what I want to achieve.
I don't want anything to do with 3D, just a plan rotation.
Can anybody forward me in the right director, or provide some information?
Thank you very much!
You implement OrientationEventListener this will give you the device rotation. Portrait is 0 degree.

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