From 26beb9a0773b59b5423465b70315a77486d100a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kevin=20S=C3=B6derberg?= Date: Mon, 12 Feb 2018 10:41:33 +0100 Subject: [PATCH] Added basic camera class Camera class to handle all updates etc of all the cameras that are used. --- core/src/com/mygdx/game/Camera.java | 57 ++++++++++++++++++++++++++ core/src/com/mygdx/game/MyGdxGame.java | 31 +++++++++----- 2 files changed, 78 insertions(+), 10 deletions(-) create mode 100644 core/src/com/mygdx/game/Camera.java diff --git a/core/src/com/mygdx/game/Camera.java b/core/src/com/mygdx/game/Camera.java new file mode 100644 index 0000000..1cb076a --- /dev/null +++ b/core/src/com/mygdx/game/Camera.java @@ -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; + } + +} diff --git a/core/src/com/mygdx/game/MyGdxGame.java b/core/src/com/mygdx/game/MyGdxGame.java index 7454132..a516080 100644 --- a/core/src/com/mygdx/game/MyGdxGame.java +++ b/core/src/com/mygdx/game/MyGdxGame.java @@ -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; @@ -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()); } @@ -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); @@ -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); @@ -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