0
\$\begingroup\$

I am just starting to build the game "engine" and I am using a sprite drawn in the same position as a rectangle and the two always move together. I have another rectangle which is the floor when the two collide the sprite/rectangle will stay on top of the "floor" rectangle for a small while then it will disappear. I assume it is falling through it as when I hold the key to move the sprite up again it re-appears.

The class I am using to represent my walls and floor is here:

public class Solids {
    Array<Rectangle> floors;
    Array<Rectangle> walls;

    public Solids(){
        floors = new Array<Rectangle>();
        walls = new Array<Rectangle>();

        floors.add(new Rectangle(0, 0, 1280, 10));
    }

    public Array<Rectangle> getFloors(){
        return floors;
    }

    public Array<Rectangle> getWalls(){
        return walls;
    }

}

I am representing my character in a class called entities which looks like this:

public class Entity {

    Rectangle bottom, left, right, top;
    Sprite sprite;
    Texture texture;
    Solids solids;
    int action;
    float velocity = 100;
    float width, height;
    float velocityY = 0;

    public Entity(Rectangle b, Texture t){
        solids = new Solids();
        bottom = b;     
        texture = t;
        width = b.width;
        height = b.height;
        sprite = new Sprite(t, 0, 0, (int)width, (int)height);
        this.setPosition(0, 0);

    }

    public void setPosition(float x, float y){
        bottom.x = x;
        bottom.y = y;
        sprite.setPosition(x, y);
    }

    public void draw(SpriteBatch batch){
        sprite.draw(batch);
    }

    public void moveLeft(float delta){
        bottom.x -= velocity * delta;
        sprite.setPosition(bottom.x, bottom.y);;
    }

    public void moveRight(float delta){
        bottom.x += velocity * delta;
        sprite.setPosition(bottom.x, bottom.y);;
    }

    public void jump(){
        velocityY = 15;
    }

    public void update(float delta){
        velocityY -= 50 * delta;
        bottom.y += velocityY;
        sprite.setPosition(bottom.x, bottom.y);
    }

    /*public int hits(Rectangle r){
        if(bottom.overlaps(r)){
            return 1;
        }
        return -1;
    }

    public void action(int type, int y){
        if(type == 1){
            setPosition((int)bottom.x, y);
        }
    }*/



    public void isCollidedFloor(){
        /* need to know if collided with floor or wall for jumping need two arrays also need array of entities */
        for(int i = 0; i < solids.getFloors().size; i++){
            if(bottom.overlaps(solids.getFloors().get(i))){
                action(solids.getFloors().get(i));
            }
        }

    }

    public void action(Rectangle r){
            setPosition(bottom.x, r.y+1);
    }


}

The calls worth noting to this class from the main class are as follows:

    player = new Entity(new Rectangle(0.0f, 0.0f, 128f,128f), new Texture(Gdx.files.internal("woman.png")));
    player.setPosition(1280/2, 100);
player.update(Gdx.graphics.getDeltaTime());
player.isCollidedFloor();

So my question is, why is this happening, tried just creating one Rectangle outside of the solids class and it still doesn't work. I have been playing around for hours and just cant seem to figure it out. Thanks in advance.

\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$

You need to set velocity to 0 after a collision. Otherwise the negative velocity will keep building up until you pass through the floor in a single update.

Change your action function to this:

    public void action(Rectangle r){
        setPosition(bottom.x, r.y+1);
        velocityY = 0;
}

For a more general solution, see the answer here: Tile based collision detection failing when player is going too fast

\$\endgroup\$
1
  • \$\begingroup\$ Perfect, I'm not quite sure how I didn't manage to see this. Working now thank you. \$\endgroup\$ Commented Oct 1, 2015 at 15:57

You must log in to answer this question.

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