hello, I was facing a problem regarding the collision detection on my game here's the code on the update loop
Code:
Input input = gc.getInput();
if (input.isKeyDown(Input.KEY_UP)) {
Vector2f position = owner.getPosition();
CollidableObject colObj = isCollidingWithEntities();
if (colObj != null) {
System.out.println(colObj.getPosition().getY());
position.y = colObj.getCollisionShape().getCenterY() + 16;
System.out.println("position reset:"+position.y);
}else{
position.y -= MOVE_AMOUNT * i;
}
owner.setPosition(position);
}
if (input.isKeyDown(Input.KEY_DOWN)) {
Vector2f position = owner.getPosition();
CollidableObject colObj = isCollidingWithEntities();
if (colObj != null) {
System.out.println("colObj:"+colObj.getPosition());
System.out.println("owner:"+owner.getPosition());
System.out.println("collision will happen");
}else{
System.out.println("no collision");
position.y += MOVE_AMOUNT * i;
}
owner.setPosition(position);
}
heres the code for the isCollidingWithEntities method
Code:
private CollidableObject isCollidingWithEntities() {
playerEntity = new CollidableObject(playerShape, owner.getPosition());
for (CollidableObject colObj : collidableObjects) {
if (colObj.isCollidingWith(playerEntity)) {
System.out.println("There will be a collision");
return colObj;
}
}
return null;
}
heres the output when i move the player to collide with a wall when pressing the up key.
Quote:
There will be a collision
colObj:[Vector2f 992.0,1696.0 (1964.8104)]
owner:[Vector2f 1022.5,1727.5 (2007.4269)]
here's the link of the collision tile and the position of the player entity. the white box is the tile collided and the red is the player.

the tile is 32x32 px also the player entity is 32x32 px also. when pressing the upkey with collision i was placing the player entity on the bottom of the collided tile so that it wouldn't pass through. but when pressing the down button it still detect a collision even if i placed it correctly.. did i miss something?