Hi Guys!
Heres my problem, I have a game and this game uses box2d and libgdx.
I have an orthographic camera.
I have a player that can move to right and left and up/down.
The borders of the camera are blocked with edge shapes. So the player cannot pass the borders.
As a fact, the camera only moves on the .y Axis.
The player dont go out of the camera view.
–
I start the game by setting 3 edge Shapes.
Left wall
Right Wall
Bottom || ground
Problem : As the camera Moves up, how can i move the Shapes with me?
Codes :
At the Start:
/**
* Add walls
*/
/**
* left side
*/
leftShape = new EdgeShape();
// bottom left
Vector2 bottomLeft = viewport.unproject(new Vector2(0, Gdx.graphics.getHeight()));
// top left
Vector2 topLeft = viewport.unproject(new Vector2());
leftShape.set(bottomLeft, topLeft);
/**
* right side
*/
rightShape = new EdgeShape();
// bottom right
Vector2 bottomRight = viewport.unproject(new Vector2(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()));
// top right
Vector2 topRight = viewport.unproject(new Vector2(Gdx.graphics.getWidth(), 0));
rightShape.set(bottomRight, topRight);
/**
* Bottom Wall
*/
bottomShape = new EdgeShape();
bottomShape.set(bottomRight, bottomLeft);
//Walls Friction and restitution
float wallFriction = 1f;
float wallRestitution = 0.1f;
Filter wallFilter = new Filter();
wallFilter.categoryBits = CollisionLogic.getCATERGORY_WALLS();
wallFilter.maskBits = CollisionLogic.getMASK_WALLS();
/**
* Bottom Wall
*/
BodyDef bottomWallDef = new BodyDef();
ground = world.createBody(bottomWallDef);
Fixture bottomWallFixture = ground.createFixture(bottomShape, 1f);
bottomWallFixture.setFriction(wallFriction);
bottomWallFixture.setRestitution(wallRestitution);
bottomWallFixture.setFilterData(wallFilter);
/**
* Left wall
*/
BodyDef leftWall = new BodyDef();
leftBodyWall = world.createBody(leftWall);
Fixture leftWallFixture = leftBodyWall.createFixture(leftShape, 1f);
leftWallFixture.setFriction(wallFriction);
leftWallFixture.setRestitution(wallRestitution);
leftWallFixture.setFilterData(wallFilter);
/**
* Right Wall
*/
BodyDef rightWall = new BodyDef();
rightBodyWall = world.createBody(rightWall);
Fixture rightWallFixture = rightBodyWall.createFixture(rightShape, 1f);
rightWallFixture.setFriction(wallFriction);
rightWallFixture.setRestitution(wallRestitution);
rightWallFixture.setFilterData(wallFilter);
I tried this : but i think i shouldnt use * PTM ( pixels to meter)
private void updateWalls() {
/**
* Left Side
*/
Vector2 bottomLeft = viewport.unproject(new Vector2(0, viewport.getScreenHeight() + viewport.getCamera().position.y));
leftBodyWall.setTransform(bottomLeft.x * PTM, bottomLeft.y * PTM, 0);
/**
* Right side
*/
Vector2 bottomRight = viewport.unproject(new Vector2(Gdx.graphics.getWidth(), Gdx.graphics.getHeight() + viewport.getCamera().position.y));
rightBodyWall.setTransform(bottomRight.x * PTM, bottomRight.y * PTM, 0);
/**
* Bottom/Ground
*/
bottomShape.set(bottomRight, bottomLeft);
ground.setTransform(bottomLeft.x * PTM, bottomLeft.y * PTM, 0);
}
EXTRA CODE :
- THe camera.position.y is a test only!
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
/**
* Update
*/
stage.act(delta);
player.update(delta);
updateWalls();
/**
* Update Box2D World
*/
world.step(1 / 60f, 6, 4);
int w = background.getWidth();
int h = background.getHeight();
float y = viewport.getCamera().position.y += 0 * (delta * 1);
viewport.getCamera().update();
batch.setProjectionMatrix(viewport.getCamera().combined);
batch.begin();
int repeat = (int) viewport.getWorldHeight() / h + 2;
batch.draw(background, -viewport.getWorldWidth() / 2, -viewport.getWorldHeight() / 2 + h * (int) (y / h) * PTM, w * PTM, h * repeat * PTM, 0, repeat, 1, 0);
Box2DSprite.draw(batch, world);
for (SmokeEffect smoke : smokeEffects) {
smoke.update(delta);
smoke.render(batch);
}
batch.end();
/**
* Render Box2D World
*/
renderer.render(world, viewport.getCamera().combined);
stage.draw();
for (Body body : removeBodies) {
body.getWorld().destroyBody(body);
removeBodies.removeValue(body, true);
}
}
@Override
public void resize(int width, int height) {
viewport.update(width, height);
stage.getViewport().update(width, height, true);
}