A rectangle vs rectangle is great too. So, essentially, after initializing all the rectangles (having each rect cover the entire platform) when you create your player/platforms, it’s as simple as:
public boolean collisionAt(Player player) {
// Loop through all platforms in the map
for (Platform platform : platformList) {
// If the player's rectangle is inside this particular platform's rectangle (intersects() is a method in Rectangle
// that takes another Rectangle as an argument and checks this for you)
if (player.getCollisionRect().intersects(platform.getCollisionRect())) {
return true; // Collision! Return true so player knows
}
}
return false; // Nothing returned true, so report false
}
Like ra4king said, this only checks the current position so in your update move player, then check. Move back if there was a collision. So your player’s update method might look like this:
public void update(float elapsedTime) {
// ....
// Other updates
// ....
// Move player
x += xVel;
y += yVel;
// Check map now that I've moved
if (map.collisionAt(this)) {
// We can't go here now, so 'undo'
x -= xVel;
y -= yVel;
}
}
EDIT: Actually, wouldn’t it be more readable to pass an x or y and collisionAt() makes a player rect before the loop? That sounds more readable with negligible overhead. Correct me if I’m wrong.