LIBGDX horizontal and vertical parallax background - java

Im trying to do a little game in LibGdx, right now i have a spaceship that can move with a touchpad in every directions and the camera follows it.
Im tryng to accomplish a parallax background made of stars that moves depending of where the spaceship is going.
Here it is the code, Im giving you all the class just to be sure to not mess up, for im new with this programming code.
public class TouchPadTest extends OrthographicCamera implements ApplicationListener {
public static final int WIDTH=480;
public static final int HEIGHT=800;
private OrthographicCamera camera;
private Stage stage;
private SpriteBatch batch;
private Touchpad touchpad;
private TouchpadStyle touchpadStyle;
private Skin touchpadSkin;
private Drawable touchBackground;
private Drawable touchKnob;
private Texture blockTexture;
private Sprite blockSprite;
private float blockSpeed;
public void create() {
batch = new SpriteBatch();
//Create camera
float aspectRatio = (float) Gdx.graphics.getWidth() / (float) Gdx.graphics.getHeight();
camera = new OrthographicCamera();
camera.setToOrtho(false, TouchPadTest.WIDTH, TouchPadTest.HEIGHT);
//Create a touchpad skin
touchpadSkin = new Skin();
//Set background image
touchpadSkin.add("touchBackground", new Texture("data/touchBackground.png"));
//Set knob image
touchpadSkin.add("touchKnob", new Texture("data/touchKnob.png"));
//Create TouchPad Style
touchpadStyle = new TouchpadStyle();
//Create Drawable's from TouchPad skin
touchBackground = touchpadSkin.getDrawable("touchBackground");
touchKnob = touchpadSkin.getDrawable("touchKnob");
//Apply the Drawables to the TouchPad Style
touchpadStyle.background = touchBackground;
touchpadStyle.knob = touchKnob;
//Create new TouchPad with the created style
touchpad = new Touchpad(10, touchpadStyle);
//setBounds(x,y,width,height)
touchpad.setBounds(15, 15, 200, 200);
//Create a Stage and add TouchPad
stage = new Stage(new FitViewport(Gdx.graphics.getWidth(),Gdx.graphics.getHeight()));
stage.addActor(touchpad);
Gdx.input.setInputProcessor(stage);
//Create block sprite
blockTexture = new Texture(Gdx.files.internal("data/shuttle2.png"));
blockSprite = new Sprite(blockTexture);
//Set position to centre of the screen
blockSprite.setPosition(Gdx.graphics.getWidth()/2-blockSprite.getWidth()/2, Gdx.graphics.getHeight()/2-blockSprite.getHeight()/2);
blockSpeed=5;
}
public void movePlayer(){
Vector2 v = new Vector2(touchpad.getKnobPercentX(), touchpad.getKnobPercentY());
float angle = v.angle();
if (touchpad.isTouched()){
blockSprite.setRotation(angle);
}
blockSprite.setX(blockSprite.getX() + touchpad.getKnobPercentX()*blockSpeed);
blockSprite.setY(blockSprite.getY() + touchpad.getKnobPercentY()*blockSpeed);
//Draw
camera.position.set(blockSprite.getX() + blockSprite.getWidth() / 2, blockSprite.getY() + blockSprite.getHeight() / 2, 0);
camera.update();
batch.setProjectionMatrix(camera.combined);
}
public void renderBackground() {
//---------------PARALLAX BACKGROUND---------------------//
}
public void dispose() {
}
public void render() {
Gdx.gl.glClearColor(0/255f,5/255f,15/255f,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//Move blockSprite with TouchPad
movePlayer();
batch.begin();
renderBackground();
blockSprite.draw(batch);
batch.end();
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void resize(int width, int height) {
}
}
For a better exemple, this is the kind of result that i want to achieve: https://www.youtube.com/watch?v=zA91SaOR-Io, if you can help me it will be amazing. Thank You.

This working example of a 3 layer parallax background was adapted from the LibGdx Parallax test and should give you an idea on how to implement a parallax effect. The three images used are all 1024x1024px.
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Vector3;
public class Test extends ApplicationAdapter implements InputProcessor{
private SpriteBatch batch;
private ParallaxCamera camera;
private Texture bgClose;
private Texture bgMid;
private Texture bgFar;
final Vector3 curr = new Vector3();
final Vector3 last = new Vector3(-1, -1, -1);
final Vector3 delta = new Vector3();
#Override
public void create () {
bgClose = new Texture(Gdx.files.internal("starbg-close.png"));
bgMid = new Texture(Gdx.files.internal("starbg-mid.png"));
bgFar = new Texture(Gdx.files.internal("starbg-far.png"));
camera = new ParallaxCamera(1920,1080);
batch = new SpriteBatch();
Gdx.input.setInputProcessor(this);
}
#Override
public void render () {
//clear screen
Gdx.gl.glClearColor(0f, 0f, 0f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// background layer, no parallax, centered around origin
batch.setProjectionMatrix(camera.calculateParallaxMatrix(0, 0));
batch.disableBlending();
batch.begin();
batch.draw(bgFar, -(int)(bgFar.getWidth() / 2), -(int)(bgFar.getHeight() / 2));
batch.end();
batch.enableBlending();
batch.setProjectionMatrix(camera.calculateParallaxMatrix(0.25f, 0.25f));
batch.begin();
for (int i = 0; i < 9; i++) {
batch.draw(bgMid, i * bgClose.getWidth() - 512, -512);
}
batch.end();
batch.setProjectionMatrix(camera.calculateParallaxMatrix(.5f, .5f));
batch.begin();
for (int i = 0; i < 9; i++) {
batch.draw(bgClose, i * bgClose.getWidth() - 512, -512);
}
batch.end();
}
//.. omitted empty methods ..//
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
last.set(-1, -1, -1);
return false;
}
#Override
public boolean touchDragged(int x, int y, int pointer) {
camera.unproject(curr.set(x, y, 0));
if (!(last.x == -1 && last.y == -1 && last.z == -1)) {
camera.unproject(delta.set(last.x, last.y, 0));
delta.sub(curr);
camera.position.add(delta.x, delta.y, 0);
}
last.set(x, y, 0);
return false;
}
private class ParallaxCamera extends OrthographicCamera {
Matrix4 parallaxView = new Matrix4();
Matrix4 parallaxCombined = new Matrix4();
Vector3 tmp = new Vector3();
Vector3 tmp2 = new Vector3();
public ParallaxCamera (float viewportWidth, float viewportHeight) {
super(viewportWidth, viewportHeight);
}
public Matrix4 calculateParallaxMatrix (float parallaxX, float parallaxY) {
update();
tmp.set(position);
tmp.x *= parallaxX;
tmp.y *= parallaxY;
parallaxView.setToLookAt(tmp, tmp2.set(tmp).add(direction), up);
parallaxCombined.set(projection);
Matrix4.mul(parallaxCombined.val, parallaxView.val);
return parallaxCombined;
}
}
}

Related

How to add background image in libgdx?

I want to add a background like blockbunny and I have a background image image in my assets/images folder under the android folder. I have tried to separate the image into three sections and add it as sky, clouds and mountains in Background class. For some reason, it is throwing null pointer exception because spritebatch is null in MyMainGame class. I have initialised SpriteBatch in create method of MyMainGame class.
Here is my MyMainGame.class
package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.mygdx.manager.Content;
import com.mygdx.manager.GameInputProcessor;
import com.mygdx.manager.GameStateManager;
import com.mygdx.manager.MyInput;
import javafx.scene.layout.Background;
import static com.mygdx.manager.Box2DVariables.PPM;
public class MyMainGame implements ApplicationListener {
SpriteBatch batch;
Texture img;
public static final int Width=320;
public static final int Height=240;
public static final int SCALE=2;
public static final float STEP= 1/60f; //60 frames per second
private float accumulator;
private SpriteBatch sb;
protected OrthographicCamera cam;
protected OrthographicCamera hud;
private Texture tex;
private Background[] backgrounds;
private GameStateManager gsm;
public SpriteBatch getSb() {
return sb;
}
public OrthographicCamera getCam() {
return cam;
}
public OrthographicCamera getHud() {
return hud;
}
public static Content con;
#Override
public void create () {
Gdx.input.setInputProcessor(new GameInputProcessor());
con=new Content();
con.loadTexture("images//sprite.jpg","sprite");
con.loadTexture("images//background.png","background");
gsm=new GameStateManager(this);
sb=new SpriteBatch();
cam=new OrthographicCamera(160,120);
cam.setToOrtho(false,Width/2,Height/2);
hud=new OrthographicCamera();
hud.setToOrtho(false,Width/2,Height/2);
}
#Override
public void render () {
cam.update();
hud.update();
accumulator +=Gdx.graphics.getDeltaTime();
while(accumulator>=STEP){
accumulator-=STEP;
gsm.update(STEP);
gsm.render();
MyInput.update();
}
}
public void resize(int width,int height){
sb.getProjectionMatrix().setToOrtho2D(0, 0, width, height);
}
public void dispose(){
con.disposeTexture("sprite");
con.disposeTexture("background");
}
public void pause(){
}
public void resume(){
gsm.setState(GameStateManager.PLAY);
}
}
GameState is an abstract class containing unimplemented methods
package com.mygdx.gamestate;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.mygdx.game.MyMainGame;
import com.mygdx.manager.GameStateManager;
public abstract class GameState {
protected GameStateManager gsm;
protected MyMainGame game;
protected SpriteBatch sb;
protected OrthographicCamera cam;
protected OrthographicCamera hud;
protected GameState(GameStateManager gsm){
this.gsm=gsm;
game=gsm.game();
sb=game.getSb();
cam=game.getCam();
hud=game.getHud();
init();
}
public abstract void init();
public abstract void update(float dt);
public abstract void draw();
public abstract void render();
public abstract void handleInput();
public abstract void dispose();
}
The Play class is extending GameState class
public class Play extends GameState{
// private BitmapFont font=new BitmapFont();
private World world;
private Body playerBody;
private int viewportWidth= 10 * 32 ;
int viewportHeight= 8 * 32 ;
private Box2DDebugRenderer b2d;
private OrthographicCamera B2DCAM;
private TiledMap tileMap;
private OrthogonalTiledMapRenderer orthomap;
private MyContactListener cl;
private float tileSize;
private Background[] backgrounds;
private Texture back;
//file name
private final String LEVEL_1 ="maps/tilemap1.tmx";
public Play(GameStateManager gsm) {
super(gsm);
//setup box2d
world=new World(new Vector2(0,-9.8f),true);
cl=new MyContactListener();
world.setContactListener(cl);
sb=new SpriteBatch(1000);
//cam=new OrthographicCamera();
b2d=new Box2DDebugRenderer();
//create Player
createPlayer();
//create Tiles
createTiles();
orthomap = new OrthogonalTiledMapRenderer(tileMap,1/32f);
//setup Box2D Cam
B2DCAM=new OrthographicCamera();
B2DCAM.setToOrtho(false, MyMainGame.Width/PPM,MyMainGame.Height/PPM);
cam=new OrthographicCamera();
cam.setToOrtho(false,10,7);
back = MyMainGame.con.getTexture("background");
TextureRegion sky = new TextureRegion(back, 0, 0, 320, 240);
TextureRegion clouds = new TextureRegion(back, 0, 240, 320, 240);
TextureRegion mountains = new TextureRegion(back, 0, 480, 320, 240);
backgrounds = new Background[3];
backgrounds[0] = new Background(sky,cam, 0f);
backgrounds[1] = new Background(clouds, cam, 0.1f);
backgrounds[2] = new Background(mountains, cam, 0.2f);
/* //TODO - remove me
File file = new File(LEVEL_1);
if(file.exists())
System.out.println("file exists");*/
}
#Override
public void init() {
}
#Override
public void update(float dt) {
handleInput();
world.step(dt,6,2);
MyMainGame.con.getTexture("sprite");
for (Background b : backgrounds) {
b.update(dt);
}
}
#Override
public void draw() {
}
#Override
public void render() {
//clear screen
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
//
cam.position.set(playerBody.getPosition().x * PPM +MyMainGame.Width / 4,MyMainGame.Height / 2,0);
// cam.setToOrtho(false,playerBody.getPosition().x * PPM +MyMainGame.Width / 4,MyMainGame.Height / 2);
//cam.setPosition(playerBody.getPosition().x * PPM +MyMainGame.Width / 4, MyMainGame.Height / 2);
cam.setToOrtho(false,10,7);
cam.update();
sb.setProjectionMatrix(cam.combined);
for(int i = 0; i < backgrounds.length; i++) {
backgrounds[i].render(sb);
}
orthomap.setView(cam);
orthomap.render();
b2d.render(world,B2DCAM.combined);
}
#Override
public void handleInput() {
if(MyInput.isPressed((MyInput.SPACE))){
System.out.println("space pressed");
if(cl.isPlayerOnGround())
System.out.println(cl.isPlayerOnGround());
playerBody.applyForceToCenter(0,200,true);
}
}
#Override
public void dispose() {
world.dispose();
b2d.dispose();
tileMap.dispose();
orthomap.dispose();
}
private void createPlayer(){
BodyDef bodydef=new BodyDef();
FixtureDef fixdef=new FixtureDef();
PolygonShape shape=new PolygonShape();
//create player
bodydef.position.set(160/PPM,200/PPM);
bodydef.type= BodyDef.BodyType.DynamicBody;
playerBody=world.createBody(bodydef);
shape.setAsBox(5/PPM,5/PPM);
fixdef.shape=shape;
fixdef.filter.categoryBits= BIT_PLAYER;
fixdef.filter.maskBits=Box2DVariables.BIT_BLUE;
playerBody.createFixture(fixdef).setUserData("PLAYER");
//create foot sensor
shape.setAsBox(2/PPM,2/PPM,new Vector2(0,-5/PPM),0);
fixdef.shape=shape;
fixdef.filter.categoryBits= BIT_PLAYER;
fixdef.filter.maskBits=Box2DVariables.BIT_BLUE;
fixdef.isSensor=true;
playerBody.createFixture(fixdef).setUserData("FOOT");
}
private void createTiles(){
//load tile map
tileMap = new TmxMapLoader().load(LEVEL_1);
//orthomap = new OrthogonalTiledMapRenderer(tileMap,1);
tileSize=(int)tileMap.getProperties().get("tilewidth", Integer.class);
System.out.println("Tile Size " +tileSize);
TiledMapTileLayer layer;
layer=(TiledMapTileLayer)tileMap.getLayers().get("Blue");
createLayer(layer,Box2DVariables.BIT_BLUE);
layer=(TiledMapTileLayer)tileMap.getLayers().get("Green");
createLayer(layer,Box2DVariables.BIT_GREEN);
layer=(TiledMapTileLayer)tileMap.getLayers().get("Red");
createLayer(layer,Box2DVariables.BIT_RED);
System.out.println("Layer Height " +layer.getHeight());
System.out.println("Layer Width " +layer.getWidth());
}
private void createLayer(TiledMapTileLayer layer,short bits){
BodyDef bodydef=new BodyDef();
FixtureDef fixdef=new FixtureDef();
//go through cells in layer
for(int row=0;row<layer.getHeight();row++){
for(int col=0;col<layer.getWidth();col++){
//get cells
TiledMapTileLayer.Cell cell=layer.getCell(col,row);
//check if cell exists
if(cell==null) continue;
if(cell.getTile()==null) continue;
//create body and fixture from cell
bodydef.type= BodyDef.BodyType.StaticBody;
bodydef.position.set((col+0.5f)*tileSize/PPM,(row+0.5f)*tileSize/PPM);
ChainShape cs=new ChainShape();
Vector2[] v=new Vector2[3];
//bottom left
v[0]=new Vector2(-tileSize/2/PPM,-tileSize/2/PPM);
//top left
v[1]=new Vector2(-tileSize/2/PPM,tileSize/2/PPM);
//top right corner
v[2]=new Vector2(tileSize/2/PPM,tileSize/2/PPM);
cs.createChain(v);
fixdef.friction=0;
fixdef.shape =cs;
fixdef.filter.categoryBits=Box2DVariables.BIT_BLUE;
fixdef.filter.maskBits=BIT_PLAYER;
fixdef.isSensor=false;
world.createBody(bodydef).createFixture(fixdef);
}
}
}
}
I have loaded the Texture in MyMainClass.java and it is stored in HashMap in Content class
I am getting the background image as Texture named 'back', and seperating them as TextureRegions sky,clouds and mountains in Play constructor of Play class and passing them in Background class.
TextureRegion sky = new TextureRegion(back, 0, 0, 320, 240);
TextureRegion clouds = new TextureRegion(back, 0, 240, 320, 240);
TextureRegion mountains = new TextureRegion(back, 0, 480, 320, 240);
backgrounds = new Background[3];
backgrounds[0] = new Background(sky,cam, 0f);
backgrounds[1] = new Background(clouds, cam, 0.1f);
backgrounds[2] = new Background(mountains, cam, 0.2f);
Background class
package com.mygdx.manager;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.mygdx.game.MyMainGame;
public class Background {
private TextureRegion image;
private OrthographicCamera camera;
private float scale;
private float x;
private float y;
private int drawX;
private int drawY;
private float dx;
private float dy;
public Background(TextureRegion image, OrthographicCamera camera, float scale) {
this.image = image;
this.camera = camera;
this.scale = scale;
drawX = MyMainGame.Width / image.getRegionWidth() + 1;
drawY = MyMainGame.Height / image.getRegionHeight() + 1;
System.out.println("Image width"+image.getRegionWidth());
System.out.println("Image height"+image.getRegionHeight());
}
public void setVector(float dx, float dy) {
this.dx = dx;
this.dy = dy;
}
public void update(float ts) {
x += (dx * scale) * ts;
y += (dy * scale) * ts;
}
public void render(SpriteBatch sb) {
float x = ((this.x + camera.viewportWidth / 2 - camera.position.x) * scale) % image.getRegionWidth();
float y = ((this.y + camera.viewportHeight / 2 - camera.position.y) * scale) % image.getRegionHeight();
sb.begin();
int colXOffset = x > 0 ? -1 : 0;
int rowYOffset = y > 0 ? -1 : 0;
for (int rowY = 0; rowY < drawY; rowY++) {
for (int colX = 0; colX < drawX; colX++) {
sb.draw(image, x + (colX + colXOffset) * image.getRegionWidth(), y + (rowY + rowYOffset) * image.getRegionHeight());
}
}
sb.end();
}
}
I am getting an error in Play class in render method as null pointer exception.Dont really know whats going on! sb.setProjectionMatrix is giving nullpointer exception!
public void render() {
//clear screen
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
//
cam.position.set(playerBody.getPosition().x * PPM +MyMainGame.Width / 4,MyMainGame.Height / 2,0);
// cam.setToOrtho(false,playerBody.getPosition().x * PPM +MyMainGame.Width / 4,MyMainGame.Height / 2);
//cam.setPosition(playerBody.getPosition().x * PPM +MyMainGame.Width / 4, MyMainGame.Height / 2);
cam.setToOrtho(false,10,7);
cam.update();
sb.setProjectionMatrix(cam.combined);
for(int i = 0; i < backgrounds.length; i++) {
backgrounds[i].render(sb);
}
orthomap.setView(cam);
orthomap.render();
b2d.render(world,B2DCAM.combined);
}
How do I split the background image and add it as shown above in my Game?

libgdx world to screen pos and factors

I want to draw a texture on a body, which is a box.
How do I convert the coordinates of the body to screen coordinates?
I know that the other way around is with camera.unproject(pos), is it similar to this?
I see a lot of people using constants such as WORLD_TO_SCREEN = 32, but I currently don't have that in my game. Is that a problem, and how can I implement it now? Because it seems like people that are using these factors can convert world to screen positions easily. I currently have a camera and an ExtendViewport
camera = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
viewport = new ExtendViewport(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, camera);
camera.position.set(VIEWPORT_WIDTH/2, VIEWPORT_HEIGHT/2, 0f);
Viewport width and height are set to this
public static final int VIEWPORT_WIDTH = 20;
public static final int VIEWPORT_HEIGHT = 22;
I don't really know what I'm doing, I read documentation and read some tutorials, but if someone could give some explanation about these variables and the world_to_screen factor that would really help me out.
I also set APP_WIDTH=1280, APP_HEIGHT = 720
Do viewport width and height mean that for box2d my screen is 20 meters wide and 22 meters high?
(asking it again because I added a lot the question and I would really like to know these things)
[EDIT]
So I'm trying to draw a ground picture on the ground body
float x = stage.getGround().getX();
float y = stage.getGround().getY();
float w = stage.getGround().getWidth();
float h = stage.getGround().getHeight();
stage.act(delta);
stage.draw();
stage.updateCamera();
Texture texture = new Texture(Gdx.files.internal("ground.png"));
Sprite sprite = new Sprite(texture);
sprite.setSize(w, h);
sprite.setPosition(x-sprite.getWidth()/2, y-sprite.getHeight()/2);
But I don't see it anywhere
[EDIT 2]
Stage Class
public class Mission1Stage extends Stage{
public static final int VIEWPORT_WIDTH = 20;
public static final int VIEWPORT_HEIGHT = 22;
private World world;
private Ground ground;
private LeftWall leftWall;
private Rocket rocket;
private static final float TIME_STEP = 1 / 300f;
private float accumulator = 0f;
private OrthographicCamera camera;
private Box2DDebugRenderer renderer;
private Viewport viewport;
private SpriteBatch spriteBatch = new SpriteBatch();
private Vector3 touchPoint;
private ShapeRenderer shapeRenderer;
private Button boostButton;
private Skin boostSkin;
private Button boostLeftButton;
private Skin boostLeftSkin;
private Button boostRightButton;
private Skin boostRightSkin;
private Button resetButton;
private Skin resetSkin;
private Game game;
private boolean isTouched = false;
public Mission1Stage(Game game) {
setUpWorld();
renderer = new Box2DDebugRenderer();
shapeRenderer = new ShapeRenderer();
setupCamera();
setUpButtons();
addActor(new Background(ground));
}
private void setUpWorld() {
world = WorldUtils.createWorld();
setUpGround();
setUpRocket();
}
private void setUpGround() {
ground = new Ground(WorldUtils.createGround(world));
addActor(ground);
}
private void setUpLeftWall() {
leftWall = new LeftWall(WorldUtils.createLeftWall(world));
}
private void setUpRocket() {
rocket = new Rocket(WorldUtils.createRocket(world));
addActor(rocket);
}
private void setupCamera() {
camera = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
viewport = new ExtendViewport(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, camera);
camera.position.set(VIEWPORT_WIDTH/2, VIEWPORT_HEIGHT/2, 0f);
camera.update();
}
private void setUpButtons() {
boostSkin = new Skin(Gdx.files.internal("skin/flat-earth-ui.json"));
boostButton = new Button(boostSkin);
boostButton.setSize(80,80);
boostButton.setPosition(Gdx.graphics.getWidth()-boostButton.getWidth()*2,0);
boostButton.setTransform(true);
boostButton.scaleBy(0.5f);
Gdx.input.setInputProcessor(this);
addActor(boostButton);
boostLeftSkin = new Skin(Gdx.files.internal("skin/flat-earth-ui.json"));
boostLeftButton = new Button(boostLeftSkin);
boostLeftButton.setSize(100, 100);
boostLeftButton.setPosition(0, 0);
addActor(boostLeftButton);
boostRightSkin = new Skin(Gdx.files.internal("skin/flat-earth-ui.json"));
boostRightButton = new Button(boostRightSkin);
boostRightButton.setSize(100, 100);
boostRightButton.setPosition(boostLeftButton.getWidth(), 0);
addActor(boostRightButton);
resetSkin = new Skin(Gdx.files.internal("skin/flat-earth-ui.json"));
resetButton = new Button(resetSkin);
resetButton.setSize(100, 100);
resetButton.setPosition(Gdx.graphics.getWidth()-100, Gdx.graphics.getHeight()-100);
addActor(resetButton);
}
#Override
public void act(float delta) {
super.act(delta);
handleInput();
accumulator += delta;
while(accumulator >= delta) {
world.step(TIME_STEP, 6, 2);
accumulator -= TIME_STEP;
}
}
#Override
public void draw() {
super.draw();
renderer.render(world, camera.combined);
float x = getGround().getBody().getPosition().x;
float y = getGround().getBody().getPosition().y;
float w = getGround().getWidth() * 2;
float h = getGround().getHeight() * 2;
spriteBatch.setProjectionMatrix(getCamera().combined);
Texture texture = new Texture(Gdx.files.internal("ground.png"));
Sprite sprite = new Sprite(texture);
sprite.setSize(w, h);
sprite.setPosition(x-sprite.getWidth()/2, y-sprite.getHeight()/2);
spriteBatch.begin();
sprite.draw(spriteBatch);
spriteBatch.end();
}
public void handleInput() {
if(boostButton.isPressed()) {
rocket.boost();
}
if(boostLeftButton.isPressed()) {
rocket.turnLeft();
}
if(boostRightButton.isPressed()) {
rocket.turnRight();
}
if(resetButton.isPressed()) {
}
}
public boolean resetScreen() {
if(resetButton.isPressed()) return true;
return false;
}
public void updateCamera() {
}
public Ground getGround() {
return ground;
}
public void resize(int width, int height) {
viewport.update(width, height);
camera.position.x = VIEWPORT_WIDTH / 2;
camera.position.y = VIEWPORT_HEIGHT /2;
}
private void translateScreenToWorldCoordinates(int x, int y) {
getCamera().unproject(touchPoint.set(x, y, 0));getCamera();
}
}
Screen class
public class Mission1Screen implements Screen{
private Game game;
private Mission1Stage stage;
private SpriteBatch spriteBatch = new SpriteBatch();
private Skin boostSkin;
private Button boostButton;
public Mission1Screen(Game game) {
this.game = game;
stage = new Mission1Stage(game);
}
#Override
public void show() {
}
#Override
public void render(float delta) {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if(stage.resetScreen()) {
game.setScreen(new Mission1Screen(game));
}
stage.act(delta);
stage.draw();
stage.updateCamera();
}
#Override
public void resize(int width, int height) {
stage.resize(width, height);
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
}
}
[EDIT 3]
public class Main extends Game {
#Override
public void create () {
this.setScreen(new Mission1Screen(this));
}
#Override
public void render () {
super.render();
}
#Override
public void dispose () {
}
}
We mostly use Pixel to meter conversion because box2d best works in meters (0-10) but you can avoid this conversion by using small worldwidth and height of your viewport. I mostly prefer 48 and 80 as viewport width and height.
You can use unproject(vector3) method of camera that translate a point given in screen coordinates to world space. I am using this method in touchdown because I get screen coordinate as parameter then I need to convert it into camera world space so that I can generate object at a particular position in world.
public class MyGdxTest extends Game implements InputProcessor {
private SpriteBatch batch;
private ExtendViewport extendViewport;
private OrthographicCamera cam;
private float w=20;
private float h=22;
private World world;
private Box2DDebugRenderer debugRenderer;
private Array<Body> array;
private Vector3 vector3;
#Override
public void create() {
cam=new OrthographicCamera();
extendViewport=new ExtendViewport(w,h,cam);
batch =new SpriteBatch();
Gdx.input.setInputProcessor(this);
world=new World(new Vector2(0,-9.8f),true);
array=new Array<Body>();
debugRenderer=new Box2DDebugRenderer();
vector3=new Vector3();
BodyDef bodyDef=new BodyDef();
bodyDef.type= BodyDef.BodyType.StaticBody;
bodyDef.position.set(0,0);
Body body=world.createBody(bodyDef);
ChainShape chainShape=new ChainShape();
chainShape.createChain(new float[]{1,1,55,1});
FixtureDef fixtureDef=new FixtureDef();
fixtureDef.shape=chainShape;
fixtureDef.restitution=.5f;
body.createFixture(fixtureDef);
chainShape.dispose();
}
#Override
public void render() {
super.render();
Gdx.gl.glClearColor(0,1,1,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
world.step(1/60f,6,2);
batch.setProjectionMatrix(cam.combined);
batch.begin();
world.getBodies(array);
for (Body body:array){
if(body.getUserData()!=null) {
Sprite sprite = (Sprite) body.getUserData();
sprite.setPosition(body.getPosition().x-sprite.getWidth()/2, body.getPosition().y-sprite.getHeight()/2);
sprite.setRotation(body.getAngle()*MathUtils.radDeg);
sprite.draw(batch);
}
}
batch.end();
debugRenderer.render(world,cam.combined);
}
#Override
public void resize(int width, int height) {
super.resize(width,height);
extendViewport.update(width,height);
cam.position.x = w /2;
cam.position.y = h/2;
cam.update();
}
private void createPhysicsObject(float x,float y){
float sizeX=2,sizeY=2;
BodyDef bodyDef=new BodyDef();
bodyDef.position.set(x,y);
bodyDef.type= BodyDef.BodyType.DynamicBody;
Body body=world.createBody(bodyDef);
PolygonShape polygonShape=new PolygonShape();
polygonShape.setAsBox(sizeX,sizeY);
FixtureDef fixtureDef=new FixtureDef();
fixtureDef.shape=polygonShape;
fixtureDef.restitution=.2f;
fixtureDef.density=2;
body.createFixture(fixtureDef);
body.setFixedRotation(false);
polygonShape.dispose();
Sprite sprite=new Sprite(new Texture("badlogic.jpg"));
sprite.setSize(2*sizeX,2*sizeY);
sprite.setPosition(x-sprite.getWidth()/2,y-sprite.getHeight()/2);
sprite.setOrigin(sizeX,sizeY);
body.setUserData(sprite);
}
#Override
public void dispose() {
batch.dispose();
debugRenderer.dispose();
world.dispose();
}
#Override
public boolean keyDown(int keycode) {
return false;
}
#Override
public boolean keyUp(int keycode) {
return false;
}
#Override
public boolean keyTyped(char character) {
return false;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
vector3.set(screenX,screenY,0);
Vector3 position=cam.unproject(vector3);
createPhysicsObject(vector3.x,vector3.y);
return false;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return false;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
return false;
}
#Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
#Override
public boolean scrolled(int amount) {
return false;
}
}

How to make .tmx map fill the whole screen

I'm loading a .tmx map using Libgdx and the map is not filling the whole screen. I cannot figure out the problem so as a last resort I'm asking a question here.I'm following a tutorial on YouTube and he did not cover this problem and as a result I cannot continue. I have tried multiple things with no sucess. The map is width: 240 tiles, height: 13 tiles, and tiles are 16 by 16
,
.
This is the code. I think the problem has to do with
renderer = new OrthogonalTiledMapRenderer(map, 1/DBZ.PPM),
gameCam.position.set(gamePort.getWorldWidth()/2,
gamePort.getWorldHeight()/2, 0);,
gameCam = new OrthographicCamera();
gamePort = new FitViewport(DBZ.V_WIDTH/DBZ.PPM, DBZ.V_HEIGHT/DBZ.PPM, gameCam);
public class PlayScreen implements Screen {
private DBZ game;
private OrthographicCamera gameCam;
private Viewport gamePort;
private Hud hud;
private TmxMapLoader maploader;
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;
private World world;
private Box2DDebugRenderer b2dr;
private Goku player;
private TextureAtlas atlas;
public PlayScreen(DBZ game){
atlas = new TextureAtlas("goku.pack");
this.game= game;
gameCam = new OrthographicCamera();
gamePort = new FitViewport(DBZ.V_WIDTH/DBZ.PPM, DBZ.V_HEIGHT/DBZ.PPM, gameCam);
hud = new Hud(game.batch);
maploader= new TmxMapLoader();
map = maploader.load("level1.tmx");
renderer = new OrthogonalTiledMapRenderer(map, 1/DBZ.PPM);
gameCam.position.set(gamePort.getWorldWidth()/2, gamePort.getWorldHeight()/2, 0);
world = new World(new Vector2(0,-10),true);
b2dr = new Box2DDebugRenderer();
new B2WorldCreator(world,map);
player = new Goku(world, this);
}
public TextureAtlas getAtlas(){
return atlas;
}
#Override
public void show() {
}
public void handleInput(float dt){
if (Gdx.input.isKeyJustPressed(Input.Keys.UP))
player.b2body.applyLinearImpulse(new Vector2(0, 4f), player.b2body.getWorldCenter(), true);
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT) && player.b2body.getLinearVelocity().x <= 2)
player.b2body.applyLinearImpulse(new Vector2(0.1f, 0), player.b2body.getWorldCenter(), true);
if (Gdx.input.isKeyPressed(Input.Keys.LEFT) && player.b2body.getLinearVelocity().x >= -2)
player.b2body.applyLinearImpulse(new Vector2(-0.1f, 0), player.b2body.getWorldCenter(), true);
}
public void update(float dt){
handleInput(dt);
world.step(1/60f, 6, 2);
player.update(dt);
gameCam.position.x = player.b2body.getPosition().x;
gameCam.update();
renderer.setView(gameCam);
}
#Override
public void render(float delta) {
update(delta);
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.render();
b2dr.render(world, gameCam.combined);
game.batch.setProjectionMatrix(gameCam.combined);
game.batch.begin();
player.draw(game.batch);
game.batch.end();
game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
hud.stage.draw();
}
#Override
public void resize(int width, int height) {
gamePort.update(width, height);
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
map.dispose();
renderer.dispose();
world.dispose();
b2dr.dispose();
hud.dispose();
}
}
public class DBZ extends Game{
public SpriteBatch batch;
public static final int V_WIDTH = 400;
public static final int V_HEIGHT = 208;
public static final float PPM = 100;
#Override
public void create () {
batch = new SpriteBatch();
setScreen(new PlayScreen(this));
}
#Override
public void render () {
super.render();
}
#Override
public void dispose () {
batch.dispose();
}
}
public class Goku extends Sprite {
public World world;
public Body b2body;
private TextureRegion gokuStand;
public Goku(World world, PlayScreen screen){
super(screen.getAtlas().findRegion("goku_sprite"));
this.world = world;
defineGoku();
gokuStand = new TextureRegion(getTexture(), 5,12,59,85);
setBounds(0,0,59/DBZ.PPM,85/DBZ.PPM);
setRegion(gokuStand);
}
public void defineGoku(){
BodyDef bdef = new BodyDef();
bdef.position.set(32/DBZ.PPM,32/DBZ.PPM);
bdef.type = BodyDef.BodyType.DynamicBody;
b2body = world.createBody(bdef);
FixtureDef fdef = new FixtureDef();
PolygonShape shape = new PolygonShape();
shape.setAsBox(59/2/DBZ.PPM, 85/2/DBZ.PPM);
fdef.shape = shape;
b2body.createFixture(fdef);
}
public void update(float dt){
setPosition(b2body.getPosition().x - getWidth()/2, b2body.getPosition().y - getHeight()/2 );
}
}
Problem is in placement of camera, attach camera with player so that viewport of camera cover whole screen. I am trying to fix this.
public float gokuStartingPositionX;
public PlayScreen(DBZ game){
.....
new B2WorldCreator(world,map);
gokuStartingPositionX=64/DBZ.PPM; //added in your method
player = new Goku(world, this);
}
public void update(float dt){
handleInput(dt);
world.step(1/60f, 6, 2);
player.update(dt);
//camera position is decided by player position and keep camera in this way so it cover whole viewport width with screen
gameCam.position.x = player.b2body.getPosition().x + gamePort.getWorldWidth()/2 - gokuStartingPositionX;
gameCam.update();
renderer.setView(gameCam);
}
Small modification in Goku
public Goku(World world, PlayScreen screen){
super(screen.getAtlas().findRegion("goku_sprite"));
this.world = world;
defineGoku(screen.gokuStartingPositionX);
gokuStand = new TextureRegion(getTexture(), 5,12,59,85);
setBounds(0,0,59/DBZ.PPM,85/DBZ.PPM);
setRegion(gokuStand);
}
public void defineGoku(float startX){
BodyDef bdef = new BodyDef();
bdef.position.set(startX,32/DBZ.PPM);
bdef.type = BodyDef.BodyType.DynamicBody;
b2body = world.createBody(bdef);
...
}
I believe what you are looking for is the ability to clamp the camera to the map.
This can be achieved using MathUtils.clamp.
gameCam.position.x = MathUtils.clamp(gameCam.position.x, viewportWidth/2 , 38 - viewportWidth/2);
gameCam.position.y = MathUtils.clamp(gameCam.position.y, viewportHeight/2, 208 - viewportHeight/2);
EDIT: Your updated PlayScreen:
package com.edwin.game.Screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.edwin.game.DBZ;
import com.edwin.game.Scenes.Hud;
import com.edwin.game.Sprites.Goku;
import com.edwin.game.Tools.B2WorldCreator;
/**
* Created by Edwin on 3/20/2017.
*/
public class PlayScreen implements Screen {
private DBZ game;
private OrthographicCamera gameCam;
private Viewport gamePort;
private Hud hud;
private TmxMapLoader maploader;
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;
private World world;
private Box2DDebugRenderer b2dr;
private Goku player;
private TextureAtlas atlas;
private float viewportWidth;
private float viewportHeight;
public PlayScreen(DBZ game){
atlas = new TextureAtlas("goku.pack");
this.game= game;
gameCam = new OrthographicCamera();
gamePort = new FitViewport(DBZ.V_WIDTH/DBZ.PPM, DBZ.V_HEIGHT/DBZ.PPM, gameCam);
hud = new Hud(game.batch);
maploader= new TmxMapLoader();
map = maploader.load("level1.tmx");
renderer = new OrthogonalTiledMapRenderer(map, 1/DBZ.PPM);
viewportWidth = gamePort.getWorldWidth();
viewportHeight= gamePort.getWorldHeight();
world = new World(new Vector2(0,-10),true);
b2dr = new Box2DDebugRenderer();
new B2WorldCreator(world,map);
player = new Goku(world, this);
}
public TextureAtlas getAtlas(){
return atlas;
}
#Override
public void show() {
}
public void handleInput(float dt){
if (Gdx.input.isKeyJustPressed(Input.Keys.UP))
player.b2body.applyLinearImpulse(new Vector2(0, 4f), player.b2body.getWorldCenter(), true);
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT) && player.b2body.getLinearVelocity().x <= 2)
player.b2body.applyLinearImpulse(new Vector2(0.5f, 0), player.b2body.getWorldCenter(), true);
if (Gdx.input.isKeyPressed(Input.Keys.LEFT) && player.b2body.getLinearVelocity().x >= -2)
player.b2body.applyLinearImpulse(new Vector2(-0.1f, 0), player.b2body.getWorldCenter(), true);
}
public void update(float dt){
handleInput(dt);
world.step(1/60f, 6, 2);
player.update(dt);
gameCam.position.x = player.b2body.getPosition().x;
// cam pos / var to clamp / min val / max val
gameCam.position.x = MathUtils.clamp(gameCam.position.x, viewportWidth/2 , 38 - viewportWidth/2);
gameCam.position.y = MathUtils.clamp(gameCam.position.y, viewportHeight/2, 208 - viewportHeight/2);
System.out.println(gameCam.position.x);
gameCam.update();
renderer.setView(gameCam);
}
#Override
public void render(float delta) {
update(delta);
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.render();
//
//
b2dr.render(world, gameCam.combined);
game.batch.setProjectionMatrix(gameCam.combined);
game.batch.begin();
player.draw(game.batch);
game.batch.end();
game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
hud.stage.draw();
}
#Override
public void resize(int width, int height) {
gamePort.update(width, height);
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
map.dispose();
renderer.dispose();
world.dispose();
b2dr.dispose();
hud.dispose();
}
}

Libgdx updating animation

I'm learning libgdx and currently doing a flappy bird demo. For fun I tried to implement when the score reached a certain number the bird sprite texture will update and change to another color. Instead of using the spritebatch and changing the color through tinting I wanted to create a new texture(png file).
The problem is since it is a sprite it needs to be animated so the wings will flap. When I try and update the texture at runtime it will only work but the animation wont play.
Here is my bird class:
public class Bird {
private static final int GRAVITY = -15;
private static final int MOVEMENT = 100;
private Vector3 position;
private Vector3 velocity;
private Rectangle bounds;
private Animation birdAnimation;
private Texture birdTexture;
private TextureRegion textureRegion;
private Sound flap;
public Bird(int x, int y){
position = new Vector3(x, y, 0);
velocity = new Vector3(0, 0, 0);
textureRegion = new TextureRegion(returnTexture());
birdAnimation = new Animation(textureRegion, 3, 0.5f);
bounds = new Rectangle(x, y, returnTexture().getWidth() / 3, returnTexture().getHeight());
flap = Gdx.audio.newSound(Gdx.files.internal("sfx_wing.ogg"));
}
public void update(float dt){
textureRegion = new TextureRegion(returnTexture());
birdAnimation = new Animation(textureRegion, 3, 0.5f);
birdAnimation.update(dt);
if(position.y > 0){
velocity.add(0, GRAVITY, 0);
}
velocity.scl(dt);
position.add(MOVEMENT * dt, velocity.y, 0);
if(position.y < 0){
position.y = 0;
}
velocity.scl(1/dt);
bounds.setPosition(position.x, position.y);
}
public TextureRegion getTexture() {
return birdAnimation.getFrame();
}
public Texture returnTexture(){
if(PlayState.score > 1){
return birdTexture = new Texture("birdanimation1.png");
}else{
return birdTexture = new Texture("birdanimation.png");
}
}
public Vector3 getPosition() {
return position;
}
public void jump(){
velocity.y = 250;
flap.play(0.15f);
}
public Rectangle getBounds(){
return bounds;
}
public void dispose(){
returnTexture().dispose();
flap.dispose();
}
}
Here is my animation class:
public class Animation {
private Array<TextureRegion> frames;
private float maxFrameTime;
private float currentFrameTime;
private int frameCount;
private int frame;
public Animation(TextureRegion region, int frameCount, float cycleTime){
frames = new Array<TextureRegion>();
int frameWidth = region.getRegionWidth() / frameCount;
for(int i = 0; i < frameCount; i++){
frames.add(new TextureRegion(region, i * frameWidth, 0, frameWidth, region.getRegionHeight()));
}
this.frameCount = frameCount;
maxFrameTime = cycleTime / frameCount;
frame = 0;
}
public void update(float dt){
currentFrameTime += dt;
if(currentFrameTime > maxFrameTime){
frame++;
currentFrameTime = 0;
}
if(frame >= frameCount){
frame = 0;
}
}
public TextureRegion getFrame(){
return frames.get(frame);
}
}
Here's my render code in my play state:
#Override
public void render(SpriteBatch sb) {
sb.setProjectionMatrix(cam.combined);
sb.begin();
sb.draw(bg, cam.position.x - (cam.viewportWidth /2), 0);
sb.draw(bird.getTexture(), bird.getPosition().x, bird.getPosition().y);
for(Tube tube : tubes){
sb.draw(tube.getTopTube(), tube.getPosTopTube().x, tube.getPosTopTube().y);
sb.draw(tube.getBottomTube(), tube.getPosBotTube().x, tube.getPosBotTube().y);
}
sb.draw(ground, groundPos1.x, groundPos1.y);
sb.draw(ground, groundPos2.x, groundPos2.y);
font.draw(sb, text, cam.position.x - gl.width / 2, cam.position.y + 200);
sb.end();
}
If you need any other classes just ask. I'm probably making a stupid mistake or just coding it entirely wrong for what I'm trying to achieve.
Thanks, Jackson
You need to NOT load your textures every single frame.
libgdx has its own Animation class so you don't need to make your own.
Here is an example on animation from libgdx's github:
2D Animation
To make it simple, just have 2 animaitons on Bird and switch between them when you need to.

Rectangles are not colliding?

Player Class
public class Player extends Sprite implements InputProcessor {
public Vector2 velocity = new Vector2();
private float speed = 500;
public Rectangle rectangle;
public Player(Sprite sprite){
super(sprite);
this.rectangle = sprite.getBoundingRectangle();
}
public void draw(SpriteBatch spriteBatch){
update(Gdx.graphics.getDeltaTime());
super.draw(spriteBatch);
}
public void update(float delta) {
rectangle = new Rectangle(getX() + velocity.x * delta,0,rectangle.getWidth(),rectangle.getWidth());
setX(getX() + velocity.x * delta);
}
}
PlayScreen Class
public class PlayScreen implements Screen {
private Player player;
private OrthographicCamera camera;
private OrthogonalTiledMapRenderer renderer;
private TiledMap map;
private Rectangle rightRectangle, leftRectangle, playerRectangle;
//private ShapeRenderer shapeRenderer;
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.render();
renderer.getSpriteBatch().begin();
player.draw(renderer.getSpriteBatch());
renderer.getSpriteBatch().end();
//shapeRenderer.begin(ShapeType.Filled);
//shapeRenderer.setColor(0, 1, 0, 1);
//shapeRenderer.rect(
// player.getX() + player.velocity.x * delta, 0,
// player.rectangle.getWidth(), player.rectangle.getHeight());
//shapeRenderer.end();
}
#Override
public void resize(int width, int height) {
camera.viewportWidth = width;
camera.viewportHeight = height;
camera.update();
}
#Override
public void show() {
camera = new OrthographicCamera();
map = new TiledMap();
renderer = new OrthogonalTiledMapRenderer(map);
//shapeRenderer = new ShapeRenderer();
player = new Player(new Sprite(new Texture("img/player.png")));
rightRectangle = new Rectangle(1280,0,0,720);
leftRectangle = new Rectangle(0,0,0,720);
boolean wallLeft = leftRectangle.overlaps(player.rectangle);
boolean wallRight = rightRectangle.overlaps(player.rectangle);
if(wallLeft){
System.out.println("wallLeft Overlap");
player.velocity.x = 0;
}
else if(wallRight){
System.out.println("wallRight Overlap");
player.velocity.x = 0;
}
player.setPosition(
Gdx.graphics.getWidth()/2f - player.getWidth()/2f,
Gdx.graphics.getHeight()/2f - player.getHeight()/2f
- Gdx.graphics.getHeight()/5f);
}
}
Doesn't seem to be colliding correctly. The rightRectangle and leftRectangle are my screen side bounds. When I use the shapeRenderer, it produces the ShapeRendered rectangle and it will follow my player around. However, I believe that my player.rectangle is not moving at all for some reason, resulting in it not colliding with my side bounds. Any help would be greatly appreciated!
rightRectangle = new Rectangle(1280,0,0,720);
leftRectangle = new Rectangle(0,0,0,720);
A Rectangle is defined as Rectangle(x, y, width, height). It looks like you are trying to define it incorrectly as Rectangle(x1, y1, x2, y2). In the above, you have created two rectangles of 0 width.

Categories

Resources