0
\$\begingroup\$

I have a Background class containing 5 layers, I've created a collision box for 5th layer. Here is the code for my Background class:

package com.gitlab.runner.tiger;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Rectangle;
import com.gitlab.runner.tiger.utilities.ParallaxBackground;
import com.gitlab.runner.tiger.utilities.ParallaxLayer;

/**
 * Created by Pintu on 4/17/2017.
 */
public class Background extends AbstractGameObject {

    private World world;
    ParallaxLayer l1, l2, l3, l4, l5, l6;
    ParallaxBackground bg;

    final float bgSpeed = 10f;
    //collison box for 5th layer
    Rectangle hitBox, nextHitBox;


    public Background(float x, float y, float width, float height, World world) {
        super(x, y, width, height);
        this.world = world;
        //creating parallax layer(region,parallaxRatio,padding)
        l1 = new ParallaxLayer(Assets.bgLayerRegion1, bgSpeed, 0);
        l2 = new ParallaxLayer(Assets.bgLayerRegion2, 4, 0);
        l3 = new ParallaxLayer(Assets.bgLayerRegion3, 3, 0);
        l4 = new ParallaxLayer(Assets.bgLayerRegion4, 2, 0);
        l5 = new ParallaxLayer(Assets.bgLayerRegion5, 1, 0);
        l6 = new ParallaxLayer(Assets.bgLayerRegion6, bgSpeed, 0);
        ParallaxLayer[] layers = {l1, l2, l3, l4, l5, l6};
        bg = new ParallaxBackground(layers, world.getCamera());

        hitBox = new Rectangle(0, 0, World.GAME_WIDTH, 12.5f);
        nextHitBox = new Rectangle(hitBox.x+World.GAME_WIDTH,0, World.GAME_WIDTH, 12.5f);

    }

    @Override
    public void update(float delta) {
        scrolling(delta);
    }

    //Scrolls the background across the screen
    private void scrolling(float delta) {
        //Moving camera
        //posBck2 =0,posBck2=at width of bg

        //posBck2 = (int) (posBck1 + width);
        // if (world.getCamera().position.x >= (posBck2 + world.getCamera().viewportWidth / 2)) {
        //   posBck1 = posBck2;
        //}
        hitBox.x = l5.positionX;
        hitBox.y = l5.positionY;
        nextHitBox.x = hitBox.x + World.GAME_WIDTH;
        hitBox.y = l5.positionY;
        world.getCamera().position.x += bgSpeed * delta;
        bg.moveX(1f * delta);


    }

    @Override
    public void draw(SpriteBatch batch) {
       /* for (TextureRegion layer : layers
                ) {

            batch.draw(layer, posBck1, y, width, height);
        }
        for (TextureRegion layer : layers
                ) {

            batch.draw(layer, posBck2, y, width, height);
        }*/
        bg.render(batch);


    }

    public void drawDebug(ShapeRenderer renderer) {

        renderer.rect(hitBox.getX(), hitBox.getY(), hitBox.getWidth(), hitBox.getHeight());
        renderer.rect(nextHitBox.getX(), nextHitBox.getY(), nextHitBox.getWidth(), nextHitBox.getHeight());

    }


}

Here is the code for the Player class:

package com.gitlab.runner.tiger;

import com.badlogic.gdx.graphics.g2d.*;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

/**
 * Created by Pintu on 4/19/2017.
 */
public class Player extends AbstractGameObject {
    //Player Phyics Variable(acceleration,position,speed,gravity)
    final float ACC = 0.046875f * 10;
    final float FRC = 0.046875f * 10;
    final int TOP = 20;
    float GRAVITY = -0.21875f * 5;
    float xsp;
    //Test textureRegion
    TextureRegion texture;
    Animation<TextureRegion> run;
    TextureAtlas playerAtlas;
    //StateTime to keep track of animation frame
    float stateTime = 0;

    //gettter ,setters
    public float getPositionX() {
        return x;
    }

    public void stopGravity() {
        GRAVITY=0;
    }

    //enum states from each animation
    enum State {
        RUNNING
    }

    //currentState to keep track of current animation
    State currentState = State.RUNNING;

    //Constructor
    public Player(float x, float y, float width, float height) {
        super(x, y, width, height);
        texture = Assets.playerRegion;
        playerAtlas = Assets.playerAtlas;
        //Initialise Animation using frames
        run = new Animation<TextureRegion>(1 / 4f, playerAtlas.getRegions(), Animation.PlayMode.LOOP);


    }

    //Update and Draw code
    @Override
    public void update(float delta) {
        if (delta == 0)
            return;
        stateTime += delta;
        //Player Run pyhsics
       /* if (xsp < TOP) {
            xsp += ACC;
        } else {
            xsp -= Math.min(Math.abs(xsp), FRC) * Math.signum(xsp);
        }*/

        setPosition(getX(), getY() + GRAVITY);

    }

    @Override
    public void draw(SpriteBatch batch) {
        //based on the state get animation frame
        TextureRegion region = null;
        switch (currentState) {
            case RUNNING:
                region = run.getKeyFrame(stateTime);
                break;
        }

        batch.draw(region, getX(), getY(), width, height);

    }

    public void drawDebug(ShapeRenderer renderer) {
        renderer.rect(this.getBoundingRectangle().x, this.getBoundingRectangle().y, this.getBoundingRectangle().getWidth(), this.getBoundingRectangle().getHeight());
    }


}

In another World class, I'm checking the collisions between them but didn't get satisfying results. Here is the code for my World class:

package com.gitlab.runner.tiger;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Intersector;

/**
 * Created by Pintu on 4/17/2017.
 */
public class World {
    //Constants - GameWidth,GameHeight
    public static final int GAME_WIDTH = 100;
    public static final int GAME_HEIGHT = 100;
    float viewportWidth, viewportHeight;
    float aspectRatio;
    public OrthographicCamera camera;
    //All game object reference here
    public Background bg;
    public Player player;

    public World() {
        viewportWidth = Gdx.graphics.getWidth();
        viewportHeight = Gdx.graphics.getHeight();
        aspectRatio = viewportHeight / viewportWidth;
        //setting up camera,camera width -half of game width,camera height - multiplied by aspect ratio;
        camera = new OrthographicCamera(GAME_WIDTH, GAME_HEIGHT * aspectRatio);
        camera.position.set(GAME_WIDTH / 2, (aspectRatio / 2) * GAME_HEIGHT, 0);
        //All game object created here
        bg = new Background(0, 0, GAME_WIDTH, GAME_HEIGHT, this);
        player = new Player(100, 100f, 10, 10);
    }

    public void update(float delta) {
        //update camera every frame
        camera.update();
        //check collison

        checkCollision();
        player.setX(camera.position.x);
        //All game objects updated here
        bg.update(delta);
        player.update(delta);

    }

    private void checkCollision() {
        float oldX = player.getX(), oldY = player.getY();

        if (Intersector.overlaps(player.getBoundingRectangle(), bg.hitBox)) {
           // Gdx.app.log("TAG", bg.hitBox.y + bg.hitBox.getHeight() + ",");
            player.setPosition(bg.hitBox.x + bg.hitBox.getWidth(), bg.hitBox.y + bg.hitBox.getHeight());
            Gdx.app.log("TAG", "COLLISON");
        } else if (player.getBoundingRectangle().overlaps(bg.nextHitBox)) {
            player.setPosition(bg.nextHitBox.x + bg.hitBox.getWidth(), bg.nextHitBox.y + bg.nextHitBox.getHeight());
        }

    }

    public OrthographicCamera getCamera() {
        return camera;
    }
}

The problem is that I'm getting this

enter image description here

Please check my code to see if there is problem in my collision code or somewhere else. Thank you in advance.

\$\endgroup\$
2
  • \$\begingroup\$ So, the player sprite is falling below the top of the ground bounding box? I think this is because your call to player.update(delta) causes the player to move down due to gravity. Try moving the call to checkCollision() after player.update(delta). \$\endgroup\$
    – Victor T.
    Commented Apr 20, 2017 at 13:23
  • \$\begingroup\$ @VictorT. ok i'll try \$\endgroup\$
    – Saurabh
    Commented Apr 20, 2017 at 18:31

1 Answer 1

-1
\$\begingroup\$

Thanks!you for your guidance that was a silly mistake ,this is the right way.

//All game objects updated here
bg.update(delta);
 player.update(delta);
//Checks collision
checkCollision();
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .