rayHandler.render() causes black screen in box2DLightsVersion - java

If I comment rayHandler.render() out, I see this screen ..
However when I uncomment it, the screen goes black. Any ideas?
public class GameScreen implements Screen {
private ColorCatch game;
private OrthographicCamera camera;
private World world;
private RayHandler rayHandler;
private Body body;
private ShapeRenderer shapeRenderer = new ShapeRenderer();
public GameScreen (final ColorCatch gam) {
this.game = gam;
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
world = new World(new Vector2(0.0f, -98.0f), true);
rayHandler = new RayHandler(world);
new PointLight(rayHandler, 1000, new Color(1,1,1,1), 5.0f, Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);
CircleShape shape = new CircleShape();
shape.setRadius(3.0f);
FixtureDef fd = new FixtureDef();
fd.shape = shape;
BodyDef bd = new BodyDef();
bd.position.set(new Vector2(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2));
bd.type = BodyType.DynamicBody;
body = world.createBody(bd);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
shapeRenderer.setProjectionMatrix(camera.combined);
world.step(Gdx.graphics.getDeltaTime(), 8, 1);
Vector2 pos = body.getPosition();
shapeRenderer.begin(ShapeType.Filled);
shapeRenderer.setColor(Color.RED);
shapeRenderer.identity();
shapeRenderer.translate(pos.x, pos.y, 0);
shapeRenderer.circle(0.0f, 0.0f, 66.0f);
shapeRenderer.end();
shapeRenderer.begin(ShapeType.Filled);
shapeRenderer.setColor(0, 1, 0, 1);
shapeRenderer.identity();
shapeRenderer.rect(5, 5, 10, 10);
shapeRenderer.circle(25, 25, 10);
shapeRenderer.end();
rayHandler.setCombinedMatrix(camera);
rayHandler.update();
//rayHandler.render(); //FIXME this line causes blank screen
}
#Override
public void dispose() {
game.getBatch().dispose();
bg.dispose();
}
#Override
public void show() {
// TODO Auto-generated method stub
}
#Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public void hide() {
// TODO Auto-generated method stub
}
}

Turns out the radius of my PointLight was too small, so not visable.
Heres what worked ..
new PointLight(rayHandler, 1000, new Color(1, 1, 1, 1), 50.0f, Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);

Related

LibGDX How to put the game in the table? *** FIXED ***

I have a game screen in which I want to implement both the game itself and the control buttons. I divided this screen into two parts using TABLE.
The control buttons and the game are implemented in different classes. Everything works correctly, except for displaying the game.
How to make the game fit in the top window?
Game Screen code
public class GameScreen implements Screen {
private Main parent;
private Stage stage;
public static SnakeControl snakeControl;
private SpriteBatch batch;
private GameControl game;
public GameScreen(Main main) {
parent = main;
stage = new Stage(new ScreenViewport());
Gdx.input.setInputProcessor(stage);
snakeControl = new SnakeControl();
GameAssets.instance().loadAssets();
batch = new SpriteBatch();
game = new GameControl();
}
#Override
public void show() {
Table table = new Table();
table.setFillParent(true);
table.setDebug(true);
table.row();
table.add(game).expand().pad(10f, 5f, 10f, 5f);
table.row();
table.add(snakeControl.get_tSnakeControl()).fillX().height(GameInfo.SCREEN_HEIGHT/3.5f).pad(0f, 5f, 10f, 5f);
stage.addActor(table);
}
#Override
public void render(float delta) {
game.update(Gdx.graphics.getDeltaTime());
clearScreen();
batch.begin();
game.render(batch);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
batch.end();
}
private void clearScreen() {
Gdx.gl.glClearColor(0f, 0f, 0f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
#Override
public void resize(int width, int height) {
stage.getViewport().update(width, height, true);
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
stage.dispose();
batch.dispose();
GameAssets.instance().dispose();
}
The Game code
public class GameControl extends Actor {
private Board board;
private Snake snake;
private float timeState;
private BitmapFont font;
private GameObject food;
private boolean isGameOver;
public GameControl() {
TextureAtlas atlas = GameAssets.instance().get(GameAssets.SNAKE_PACK);
font = GameAssets.instance().get(GameAssets.PIXEL_FONT);
snake = new Snake(atlas);
board = new Board(snake, GameInfo.SCREEN_WIDTH, GameInfo.SCREEN_HEIGHT);
food = board.generateFood();
init();
}
private void init() {
GameSoundsPlayer.init();
GameSoundsPlayer.playMusic(GameAssets.MEMO_SOUND, false);
}
public void update(float delta) {
if (snake.hasLive()) {
timeState += delta;
snake.handleEvents();
if (timeState >= .09f) {
snake.moveBody();
timeState = 0;
}
if (snake.isCrash()) {
snake.reset();
snake.popLife();
GameSoundsPlayer.playSound(GameAssets.CRASH_SOUND, false);
}
if (snake.isFoodTouch(food)) {
GameSoundsPlayer.playSound(GameAssets.EAT_FOOD_SOUND, false);
Scorer.score();
snake.grow();
food = board.generateFood();
}
} else {
gameOver();
if (Gdx.input.isKeyJustPressed(Input.Keys.ANY_KEY)) start();
}
}
private void gameOver() {
isGameOver = true;
GameSoundsPlayer.stopMusic(GameAssets.MEMO_SOUND);
GameSoundsPlayer.playMusic(GameAssets.GAME_OVER_SOUND, false);
}
private void start() {
GameSoundsPlayer.playMusic(GameAssets.MEMO_SOUND, false);
GameSoundsPlayer.stopMusic(GameAssets.GAME_OVER_SOUND);
isGameOver = false;
snake.reset();
snake.restoreHealth();
food = board.generateFood();
Scorer.reset();
}
public void render(SpriteBatch batch) {
board.render(batch);
food.draw(batch);
snake.render(batch);
if (isGameOver) {
font.draw(batch, "GAME OVER", (GameInfo.SCREEN_WIDTH - 100) / 2, (GameInfo.SCREEN_HEIGHT + 100) / 2);
font.draw(batch, "Press any key to continue", (GameInfo.SCREEN_WIDTH - 250) / 2, (GameInfo.SCREEN_HEIGHT + 50) / 2);
}
font.draw(batch, "Player: ", GameInfo.SCALE * 4, GameInfo.SCREEN_HEIGHT - 10);
font.draw(batch, "Score: " + Scorer.getScore(), GameInfo.SCALE / 2, GameInfo.SCREEN_HEIGHT - 10);
font.draw(batch, "Size: " + snake.getBody().size(), GameInfo.SCALE / 2, GameInfo.SCREEN_HEIGHT - 40);
}
}
I used Viewport for this!!!
#Override
public void render(float delta) {
clearScreen();
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()/3);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
Gdx.gl.glViewport(0, Gdx.graphics.getHeight()/3, Gdx.graphics.getWidth(), (Gdx.graphics.getHeight() - Gdx.graphics.getHeight()/3));
game.update(Gdx.graphics.getDeltaTime());
batch.begin();
game.render(batch);
batch.end();
}

Libgdx/java Performance crash with box2d light

Background Info
Hey guys i am currently working on a little rpg :) and today i tried to implement some easy light ....
Main Question
When i use Point light in my Project the fps will get slower and slower ... i have got a very good pc, so it cant be my gpu or cpu ... So what did i miss?
Heres an Screenshot
You can see in the left bottom corner the fps : 6.
By the way, i disabled vsync and my gpu is an gtx 960 ... so i dont really know why i have so low fps ...
Player class :
package Mobs;
public class Player {
AnimatedSprite animatedSprite;
SpriteBatch batch;
Light licht = new Light(Color.WHITE,500);
public int state = 0;
public int netState = 1;
float speed = 2f;
public Vector2 position = new Vector2(256,256);
public Vector2 networkPosition = new Vector2(0,0);
public Player(){
}
public void update(){
state = 0;
if(Gdx.input.isKeyPressed(Keys.A)){
position.x -= Gdx.graphics.getDeltaTime() * 100f;
state = 1;
//System.out.println(currentState);
}
if(Gdx.input.isKeyPressed(Keys.D)){
position.x += Gdx.graphics.getDeltaTime() * 100f;
state = 2;
//System.out.println(currentState);
}
if(Gdx.input.isKeyPressed(Keys.W)){
position.y += Gdx.graphics.getDeltaTime() * 100f;
state = 3;
//System.out.println(currentState);
}
if(Gdx.input.isKeyPressed(Keys.S)){
position.y -= Gdx.graphics.getDeltaTime() * 100f;
state = 4;
//System.out.println(currentState);
}
}
public void setX(float x){
position.x = x;
}
public void setY(float y){
position.y = y;
}
public void draw(float f, float g, OrthographicCamera camera){
position.x = f;
position.y = g;
//System.out.println("In beforeSetState : "+currentState);
animatedSprite.setState(state);
//System.out.println("In after : "+currentState);
animatedSprite.createAnimation();
camera.position.set(f,g,0);
camera.update();
licht.drawLight(camera, f+25 , g+25);
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(animatedSprite.convertAnimationTOframes(),f,g, Gdx.graphics.getWidth()/25,Gdx.graphics.getHeight()/15);
batch.end();
}
public void doSetup(){
batch = new SpriteBatch();
animatedSprite = new AnimatedSprite();
}
public float getX(){
return position.x;
}
public float getY(){
return position.y;
}
}
And my "Light" class :
package Screen;
public class Light {
World world;
RayHandler rayHandler;
PointLight pointLight;
Body player;
Box2DDebugRenderer debugRenderer;
public Light(Color farbe, int radius) {
world = new World(new Vector2(0,0),false);
rayHandler = new RayHandler(world);
pointLight = new PointLight(rayHandler, 500 , farbe , radius, 0, 0);
pointLight.setSoftnessLength(0f);
debugRenderer = new Box2DDebugRenderer();
}
public void drawLight(OrthographicCamera playerCam, float x, float y){
world.step(1 / 60f, 8, 3);
debugRenderer.render(world, playerCam.combined);
pointLight.setPosition(x,y);
rayHandler.setCombinedMatrix(playerCam.combined);
rayHandler.updateAndRender();
}
public void removeLights(){
rayHandler.removeAll();
pointLight.remove();
}
}
Because i still got laggs, heres my MainClass :
public class LauncherScreen implements Screen{
//-----------------------------------------------------------
//-----------------idle Animation----------------------------
//-----------------------------------------------------------
Map duengon;
AnimatedSprite animationForMultiplayer;
SpriteBatch spriteBatch;
Player mySelf;
OrthographicCamera mpPlayerCam;
OrthographicCamera camera;
static Client client = new Client();
Launcher launcher = new Launcher();
int[][] map = {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};
#Override
public void render(float delta) {
// TODO Auto-generated method stub
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
launcher.update();
//------------------------------------------------------------------------
//------------------------Draws the Map-----------------------------------
//------------------------------------------------------------------------
duengon.ubdate(map, camera);
//------------------------------------------------------------------------
//------------------------Draws the Players-------------------------------
//------------------------------------------------------------------------
for(MPPlayer mpPlayer : launcher.getPlayersValue()){
animationForMultiplayer.setState(mpPlayer.state);
animationForMultiplayer.createAnimation();
camera.position.set(mpPlayer.x,mpPlayer.y,0);
spriteBatch.setProjectionMatrix(camera.combined);
spriteBatch.begin();
spriteBatch.draw(animationForMultiplayer.convertAnimationTOframes(), mpPlayer.x, mpPlayer.y,Gdx.graphics.getWidth()/25,Gdx.graphics.getHeight()/15); // #6
spriteBatch.end();
System.out.println("mpPlayer : "+mpPlayer.x+" "+mpPlayer.y);
}
mySelf.update();
mySelf.draw(launcher.getPlayerX(), launcher.getPlayerY(), camera);
camera.update();
System.out.println(Gdx.graphics.getFramesPerSecond());
System.out.println("player : "+launcher.getPlayerX()+" "+launcher.getPlayerY());
}
#Override
public void show() {
// TODO Auto-generated method stub
animationForMultiplayer = new AnimatedSprite();
spriteBatch = new SpriteBatch();
mySelf = new Player();
mySelf.doSetup();
mpPlayerCam = new OrthographicCamera(0,0);
mpPlayerCam.setToOrtho(false);
camera = new OrthographicCamera(0, 0);
camera.setToOrtho(false);
duengon = new Map();
}
#Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void hide() {
// TODO Auto-generated method stub
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public void dispose() {
// TODO Auto-generated method stub
spriteBatch.dispose();
mySelf.doDispose();
animationForMultiplayer.doDispose();
duengon.doDispose();
}
}
The .doDispose() in my mainClass are methods which disposes the resource from the classes is use
Thanks for your help and your time :)
You are not disposing anything, you might want to look into the dispose() function of LibGDX
Here is the link
https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/Disposable.html
if thats not the problem please let us know.

Libgdx/ Java cameras for multiple players?

I am trying to make a little multiplayer rpg game.
It all worked fine, until I implemented cameras for each player.
Now I got the problem, that if one player joines, he can't walk alone. It seems that he is stuck on the Client players cam. I have created a camera for each of them. Did I miss something?
Here's my "Main" class
public class LauncherScreen implements Screen{
//-----------------------------------------------------------
//-----------------idle Animation----------------------------
//-----------------------------------------------------------
Texture texture;
AnimatedSprite animationForMultiplayer;
SpriteBatch spriteBatch;
Player mySelf;
OrthographicCamera playerCam;
OrthographicCamera mpPlayerCam;
static Client client = new Client();
Launcher launcher = new Launcher();
#Override
public void render(float delta) {
// TODO Auto-generated method stub
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
launcher.update();
for(MPPlayer mpPlayer : launcher.getPlayersValue()){
//System.out.println("mpPlayerXandY : "+mpPlayer.state);
animationForMultiplayer.setState(mpPlayer.state);
animationForMultiplayer.createAnimation();
mpPlayerCam.update();
spriteBatch.setProjectionMatrix(mpPlayerCam.combined);
spriteBatch.begin();
spriteBatch.draw(animationForMultiplayer.convertAnimationTOframes(), mpPlayer.x, mpPlayer.y,Gdx.graphics.getWidth()/25,Gdx.graphics.getHeight()/15); // #6
spriteBatch.end();
mpPlayerCam.position.set(mpPlayer.x,mpPlayer.y,0);
System.out.println("mpPlayer : "+mpPlayer.x+" "+mpPlayer.y);
}
mySelf.update();
mySelf.draw(launcher.getPlayerX(), launcher.getPlayerY(), playerCam);
//System.out.println(Gdx.graphics.getFramesPerSecond());
System.out.println("player : "+launcher.getPlayerX()+" "+launcher.getPlayerY());
}
#Override
public void pause() {
// TODO Auto-generated method stub
//super.pause();
}
#Override
public void resume() {
// TODO Auto-generated method stub
//super.resume();
}
#Override
public void dispose() {
// TODO Auto-generated method stub
//super.dispose();
}
#Override
public void show() {
// TODO Auto-generated method stub
texture = new Texture(Gdx.files.internal("EnemyAnimations/BugIdleStand.png"));
animationForMultiplayer = new AnimatedSprite();
spriteBatch = new SpriteBatch();
mySelf = new Player();
mySelf.doSetup();
playerCam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
playerCam.setToOrtho(false);
playerCam.position.set(mySelf.getX(), mySelf.getY(), 0);
mpPlayerCam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
mpPlayerCam.setToOrtho(false);
mpPlayerCam.position.set(0, 0, 0);
}
#Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void hide() {
// TODO Auto-generated method stub
}
}
And here's the "Main" player update
public void draw(float f, float g, OrthographicCamera camera){
position.x = f;
position.y = g;
//System.out.println("In beforeSetState : "+currentState);
animatedSprite.setState(state);
//System.out.println("In after : "+currentState);
animatedSprite.createAnimation();
camera.position.set(f,g,0);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(animatedSprite.convertAnimationTOframes(),f,g, Gdx.graphics.getWidth()/25,Gdx.graphics.getHeight()/15); // #17
batch.end();
//batch.setProjectionMatrix(null);
//System.out.println(currentState);
}
Found a solution for it, didnt knew that it was so easy ^^ heres my updated code
public class LauncherScreen implements Screen{
//-----------------------------------------------------------
//-----------------idle Animation----------------------------
//-----------------------------------------------------------
Texture texture;
AnimatedSprite animationForMultiplayer;
SpriteBatch spriteBatch;
Player mySelf;
OrthographicCamera playerCam;
OrthographicCamera mpPlayerCam;
OrthographicCamera camera;
ShapeRenderer shapeRenderer;
static Client client = new Client();
Launcher launcher = new Launcher();
int[][] map = {{1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}};
#Override
public void render(float delta) {
// TODO Auto-generated method stub
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
launcher.update();
for(int i = 0; i < map.length; i++){
for(int j = 0; j < map[0].length; j++){
if(map[i][j] == 1){
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin(ShapeType.Line);
shapeRenderer.setColor(0, 0, 0, 0);
shapeRenderer.rect(i*50, j*50, 50, 50);
shapeRenderer.end();
}
}
}
for(MPPlayer mpPlayer : launcher.getPlayersValue()){
//System.out.println("mpPlayerXandY : "+mpPlayer.state);
animationForMultiplayer.setState(mpPlayer.state);
animationForMultiplayer.createAnimation();
//mpPlayerCam.update();
//spriteBatch.setProjectionMatrix(mpPlayerCam.combined);
camera.position.set(mpPlayer.x,mpPlayer.y,0);
spriteBatch.setProjectionMatrix(camera.combined);
spriteBatch.begin();
spriteBatch.draw(animationForMultiplayer.convertAnimationTOframes(), mpPlayer.x, mpPlayer.y,Gdx.graphics.getWidth()/25,Gdx.graphics.getHeight()/15); // #6
spriteBatch.end();
//mpPlayerCam.position.set(mpPlayer.x,mpPlayer.y,0);
System.out.println("mpPlayer : "+mpPlayer.x+" "+mpPlayer.y);
}
mySelf.update();
mySelf.draw(launcher.getPlayerX(), launcher.getPlayerY(), camera);
//System.out.println(Gdx.graphics.getFramesPerSecond());
camera.update();
System.out.println("player : "+launcher.getPlayerX()+" "+launcher.getPlayerY());
}
#Override
public void pause() {
// TODO Auto-generated method stub
//super.pause();
}
#Override
public void resume() {
// TODO Auto-generated method stub
//super.resume();
}
#Override
public void dispose() {
// TODO Auto-generated method stub
//super.dispose();
}
#Override
public void show() {
// TODO Auto-generated method stub
texture = new Texture(Gdx.files.internal("EnemyAnimations/BugIdleStand.png"));
animationForMultiplayer = new AnimatedSprite();
spriteBatch = new SpriteBatch();
mySelf = new Player();
mySelf.doSetup();
playerCam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
playerCam.setToOrtho(false);
playerCam.position.set(mySelf.getX(), mySelf.getY(), 0);
mpPlayerCam = new OrthographicCamera(0,0);
mpPlayerCam.setToOrtho(false);
camera = new OrthographicCamera(0, 0);
camera.setToOrtho(false);
shapeRenderer = new ShapeRenderer();
}
#Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void hide() {
// TODO Auto-generated method stub
}
}

Libgdx body position and shaperenderer position not the same

i'm fairly new to libgdx in general. Basically I have the ball bouncing world and i am just experimenting with it. Before I had a fixed camera position as such:
http://prntscr.com/567hvo
After I have putted the following line in the render method so the camera keeps following the player (which is the circle) :
camera.position.set(player.getPosition().x, player.getPosition().y, 0);
The player is a Body type.
So when I do this it does follow the player, but the shape renderer acts weird now. Look at what happens:
http://prntscr.com/567i9a
Even though I am referring to the same x and y position of the player to the camera and the shape renderer, they are not in the same position.
Here is my code:
public class Game extends ApplicationAdapter {
World world = new World(new Vector2(0, -9.8f), true);
Box2DDebugRenderer debugRenderer;
OrthographicCamera camera;
static final int PPM = 100;
static float height,width;
Body player;
RayHandler handler;
PointLight l1;
ShapeRenderer shapeRenderer;
#Override
public void create() {
shapeRenderer = new ShapeRenderer();
height = Gdx.graphics.getHeight();
width = Gdx.graphics.getWidth();
//set camera
camera = new OrthographicCamera();
camera.setToOrtho(false, (width)/PPM/2, (height)/PPM/2);
camera.update();
//Ground body
BodyDef groundBodyDef =new BodyDef();
groundBodyDef.position.set(new Vector2(width/PPM/2/2, 0));
Body groundBody = world.createBody(groundBodyDef);
PolygonShape groundBox = new PolygonShape();
groundBox.setAsBox((width/PPM/2/2), 0.1f);
groundBody.createFixture(groundBox, 0.0f);
//wall left
BodyDef wallLeftDef = new BodyDef();
wallLeftDef.position.set(0, 0f);
Body wallLeftBody = world.createBody(wallLeftDef);
PolygonShape wallLeft = new PolygonShape();
wallLeft.setAsBox(width/PPM/20/2, height/PPM/2);
wallLeftBody.createFixture(wallLeft,0.0f);
//wall right
BodyDef wallRightDef = new BodyDef();
wallRightDef.position.set((width/PPM)/2, 0f);
Body wallRightBody = world.createBody(wallRightDef);
PolygonShape wallRight = new PolygonShape();
wallRight.setAsBox(width/PPM/20/2, height/PPM/2);
wallRightBody.createFixture(wallRight,0.0f);
//Dynamic Body
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DynamicBody;
bodyDef.position.set((width/PPM)/2/2, (height / PPM)/2/2);
player = world.createBody(bodyDef);
CircleShape dynamicCircle = new CircleShape();
dynamicCircle.setRadius(5f/PPM);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = dynamicCircle;
fixtureDef.density = 0.4f;
fixtureDef.friction = 0.2f;
fixtureDef.restitution = 0.6f;
player.createFixture(fixtureDef);
debugRenderer = new Box2DDebugRenderer();
//Lighting
handler = new RayHandler(world);
handler.setCombinedMatrix(camera.combined);
PointLight l1 = new PointLight(handler,5000,Color.CYAN,width/PPM/2,width/PPM/2/2/2,height/PPM/2/2);
new PointLight(handler,5000,Color.PURPLE,width/PPM/2,width/PPM/2/1.5f,height/PPM/2/2);
shapeRenderer.setProjectionMatrix(camera.combined);
}
public void update() {
if(Gdx.input.isKeyJustPressed(Keys.UP)) {
player.applyForceToCenter(0, 0.75f, true);
}
if(Gdx.input.isKeyJustPressed(Keys.RIGHT)) {
player.applyForceToCenter(0.5f, 0f, true);
}
if(Gdx.input.isKeyJustPressed(Keys.LEFT)) {
player.applyForceToCenter(-0.5f, 0f, true);
}
}
#Override
public void dispose() {
}
#Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
debugRenderer.render(world, camera.combined);
world.step(1/60f, 6, 2);
handler.updateAndRender();
shapeRenderer.begin(ShapeType.Filled);
shapeRenderer.setColor(1, 1, 1, 1);
shapeRenderer.circle(player.getPosition().x, player.getPosition().y, 5f/PPM, 100);
shapeRenderer.end();
camera.position.set(player.getPosition().x, player.getPosition().y, 0);
camera.update();
update();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
}
You are only setting the shapeRenderers projectionmatrix once so it is not updating when you render.
You should put
shapeRenderer.setProjectionMatrix(camera.combined);
to your render method right before
shapeRenderer.begin(ShapeType.Filled);

Libgdx Scrolling TiledMaps

i'm trying to make my libgdx map scroll, but i dont know the code to make map scroll, please guys help me, i would like the map to scroll like flappy bird game, this is my my code which show the map on the screen
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
position.y = position.y - 4;
if(Gdx.input.isTouched()){
position.y = position.y +10;
}
if(position.y < 0){
position.y = 0;
}
if(position.y > Gdx.graphics.getHeight() -70){
position.y = Gdx.graphics.getHeight() - 70;
}
renderer.setView(camera);
renderer.render();
//tells the computer when to start drawing textures
batch.begin();
batch.draw(Green1, position.x, position.y, 50, 50);
batch.end();
}
#Override
public void show() {
Green1 = new Texture("img/Green1.png");
batch = new SpriteBatch();
position = new Vector2(20, Gdx.graphics.getHeight());
map = new TmxMapLoader().load("maps/map1.tmx");
renderer = new OrthogonalTiledMapRenderer(map);
camera = new OrthographicCamera();
}
#Override
public void hide() {
}
#Override
public void create() {
Green1 = new Texture("img/Green.png");
batch = new SpriteBatch();
position = new Vector2(20, Gdx.graphics.getHeight());
}
#Override
public void resize(int width, int height) {
camera.viewportWidth = width;
camera.viewportHeight = height;
camera.position.set(width/2f, height/3f, 0); //by default camera position on (0,0,0)
camera.update();
}
#Override
public void render() {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
map.dispose();
renderer.dispose();
}
}
When I was learning to build 2D games I used star-assault for an example. You can find their github repository here: https://github.com/chrismorales/2d-sidescroller && https://github.com/chrismorales/2d-sidescroller/blob/master/star-assault/src/net/obviam/starassault/view/WorldRenderer.java should be useful to look at.

Categories

Resources