Skip to content

Commit

Permalink
Added basic camera class
Browse files Browse the repository at this point in the history
Camera class to handle all updates etc of all the cameras that are used.
  • Loading branch information
Kevin Söderberg committed Feb 12, 2018
1 parent e2a7e53 commit 26beb9a
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 10 deletions.
57 changes: 57 additions & 0 deletions core/src/com/mygdx/game/Camera.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.mygdx.game;

import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Matrix4;

public class Camera {

private final float WORLD_TO_RENDER = 96f;
private final float RENDER_TO_WORLD = 1/96f;

private OrthographicCamera worldCamera, lightCamera;
private Matrix4 cameraBox2D;

public Camera(float w, float h) {
worldCamera = new OrthographicCamera(w, h);
lightCamera = new OrthographicCamera(w*RENDER_TO_WORLD, h*RENDER_TO_WORLD);

worldCamera.update();
lightCamera.update();
}

//Character (x,y) and width & height
public void update(float posX, float posY, float width, float height) {

worldCamera.position.set((posX*WORLD_TO_RENDER)+
(width/2), (posY*WORLD_TO_RENDER)+(height/2), 0);

lightCamera.position.set(posX, posY, 0);

worldCamera.update();
lightCamera.update();

cameraBox2D = worldCamera.combined.cpy();
cameraBox2D.scl(WORLD_TO_RENDER);
}

public OrthographicCamera getWorld() {
return worldCamera;
}

public OrthographicCamera getLight() {
return lightCamera;
}

public Matrix4 getDebug() {
return cameraBox2D;
}

public float WORLD_TO_RENDER() {
return WORLD_TO_RENDER;
}

public float RENDER_TO_WORLD() {
return RENDER_TO_WORLD;
}

}
31 changes: 21 additions & 10 deletions core/src/com/mygdx/game/MyGdxGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public class MyGdxGame extends ApplicationAdapter {
private Matrix4 cameraBox2D;
private Box2DDebugRenderer debugRender;
public OrthographicCamera worldCamera, lightCamera;
private Camera cam;


private Sound testSound;
private Music testMusic;
Expand Down Expand Up @@ -101,17 +103,18 @@ private void update() {
worldCamera.update();

//Change light position to follow player to give sight
//pl.setPosition((loli.getBoxX()+loli.getWidth()),(loli.getBoxY()+loli.getHeight()*1.5f));
pl.setPosition((loli.getBoxX()+loli.getWidth()),(loli.getBoxY()+loli.getHeight()*1.5f));
//pl.setPosition((loli.getBoxX()*WORLD_TO_RENDER)+(loli.getWidth()/2), (loli.getBoxY()*WORLD_TO_RENDER) +(loli.getHeight()/2));
pl.setPosition(loli.getBoxX()*WORLD_TO_RENDER, loli.getBoxY()*WORLD_TO_RENDER);
//pl.setPosition(loli.getBoxX()*WORLD_TO_RENDER, loli.getBoxY()*WORLD_TO_RENDER);
pl.update();

System.out.println(loli.getBoxX()*WORLD_TO_RENDER);
//System.out.println(loli.getBoxX()*WORLD_TO_RENDER);
//lightCamera.position.set((loli.getBoxX()*WORLD_TO_RENDER)+(loli.getWidth()/2), (loli.getBoxY()*WORLD_TO_RENDER)+(loli.getHeight()/2), 0);
lightCamera.position.set(loli.getBoxX()*WORLD_TO_RENDER, loli.getBoxY()*WORLD_TO_RENDER, 0);
lightCamera.position.set(loli.getBoxX(), loli.getBoxY(), 0);
//lightCamera.position.set(loli.getBoxX()*WORLD_TO_RENDER, loli.getBoxY()*WORLD_TO_RENDER, 0);
lightCamera.update();
loli.update();
cam.update(loli.getBoxX(),loli.getBoxY(),loli.getWidth(),loli.getHeight());

}

Expand All @@ -131,6 +134,9 @@ public void create() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();

cam = new Camera(w,h);


batch = new SpriteBatch();
font = new BitmapFont();
loli = new Character(200,400, world, 123*RENDER_TO_WORLD, 192*RENDER_TO_WORLD);
Expand Down Expand Up @@ -189,10 +195,11 @@ public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

update();
cameraBox2D = worldCamera.combined.cpy();
cameraBox2D.scl(WORLD_TO_RENDER);
//cameraBox2D = worldCamera.combined.cpy();
//cameraBox2D.scl(WORLD_TO_RENDER);

batch.setProjectionMatrix(worldCamera.combined);
//batch.setProjectionMatrix(worldCamera.combined);
batch.setProjectionMatrix(cam.getWorld().combined);

batch.begin();
batch.draw(backImage,0,0);
Expand All @@ -213,11 +220,15 @@ public void render() {

batch.end();

rayhandler.setCombinedMatrix(lightCamera);
//rayhandler.setCombinedMatrix(lightCamera);
rayhandler.setCombinedMatrix(cam.getLight());
rayhandler.updateAndRender();

if(showDebug)
debugRender.render(world, cameraBox2D);
if(showDebug) {
//debugRender.render(world, cameraBox2D);
debugRender.render(world, cam.getDebug());
}

}

@Override
Expand Down

0 comments on commit 26beb9a

Please sign in to comment.