0
\$\begingroup\$

Im making a game and Im working on the collision. First Ill tell you what I want (Ill put in pictures), then Ill leave the code.

What I want: I want the player to stop exactly before its collision box (a rectangle) intersects with a wall collision box. Right now this only happens sometimes, sometimes it doesnt go all the way to the wall, that is probably because the velocity is too big I guess. Here are the pictures:

This is when the collision works correctly This is when the collision doesnt work correctly

Left: it works good, it goes all the way to the wall (btw the red square is the collision box).

Right: The player only goes down that much and leaves a gap inbetween it and the wall (the circle shows the gap) and you can not go down to go all the way to the wall.

The movment code:

if(canMove){    
    move(xVol, yVol);
    xVol = yVol = 0f; // resets the velotcity
}

public void move(float xv, float yv){
    if(!collisionv(yv)){
        y += yv; // moves the player on its y
        for(Entity e : PlayState.e){
            if(e.getID() == ID.Player){
                e.getCollisionRect().setY(y); // moves the collision box
            }
        }
    }

    if(!collisionh(xv)){
        x += xv; // moves the player on its x
        for(Entity e : PlayState.e){
            if(e.getID() == ID.Player){
                e.getCollisionRect().setX(x); // moves the collision box
            }
        }
    }

}

public boolean collisionv(float yv){
// Vertical collision checking
    boolean ret = false;
// makes a rectangle that represends the players collision rect after it moved to check if it will be colliding with anything
    Rectangle p = new Rectangle(x, y + yv, getCollisionRect().getWidth(), getCollisionRect().getHeight());

    for(Entity e:PlayState.e){

        if(e.getID() != ID.Player && c.trueCollision(p, e.getCollisionRect())){
    // This true collision method just returns if the rectangles are intersecting
            ret = true;
        }
    }
    return ret;
}

public boolean collisionh(float xv){
// Horizontal collision checking
    boolean ret = false;
    // makes a rectangle that represends the players collision rect after it moved to check if it will be colliding with anything
    Rectangle p = new Rectangle(x + xv, y, getCollisionRect().getWidth(), getCollisionRect().getHeight());

    for(Entity e:PlayState.e){
        if(e.getID() != ID.Player && c.trueCollision(p, e.getCollisionRect())){
            ret = true;
        }
    }
    return ret;
}

My question: How do I make it move all the way to the wall, never leaving a gap inbetween the player and the wall?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Check out my answer: Platformer collision problems (C++)

Maybe it doesn't seem so simple to implement with your codes but that collision system would be the best choice for you (I think).

\$\endgroup\$

You must log in to answer this question.

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