# Java 2d topdown collision

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:

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?