public void update() {
for(int i = 0; i < Frame.teilesArray.size();i++){ //teilesArray is a Array with all tiles from the map (every block has 64x64 px)
if(Frame.teilesArray.get(i).getimgInt()== 0 || Frame.teilesArray.get(i).getimgInt()== 1){ // means that only the two special blocks will check of collision not all blocks only this blocks
if(bounding.intersects(Frame.teilesArray.get(i).getBounding())){ // bounding is the rectangle of the Player and Frame.teilesArray.get(i).getBounding() is the rectangle of one block in the Array list
Rectangle intersection = (Rectangle) bounding.createIntersection(Frame.teilesArray.get(i).getBounding()); // made a new rectangle out off the intersection
// adding variables for the object the player can collide with
double minX = Frame.teilesArray.get(i).getBounding().getMinX();
double maxX = Frame.teilesArray.get(i).getBounding().getMaxX();
double minY = Frame.teilesArray.get(i).getBounding().getMinY();
double maxY = Frame.teilesArray.get(i).getBounding().getMaxY();
if (bounding.getMinY() < maxY && bounding.getMaxY() > minY ) {
// collision is vertical
if (bounding.getMaxY() > minY) {
System.out.println("Player auf Block: Block: MinX="+minX+", MaxX="+maxX+", MinY="+minY+", MaxY="+maxY+" | Playerrec: MinX="+bounding.getMinX()+", MaxX="+bounding.getMaxX()+", MinY="+bounding.getMinY()+", MaxY="+bounding.getMaxY()+" | Player: Ply_y="+ply_y +", Ply_x="+ply_x+" | Intersection Höhe="+intersection.getHeight());
// bottom of player has passed the top border of collision object, move up
ply_y -= intersection.getHeight();
} else {
System.out.println("Player unter Block: Block: MinX="+minX+", MaxX="+maxX+", MinY="+minY+", MaxY="+maxY+" | Playerrec: MinX="+bounding.getMinX()+", MaxX="+bounding.getMaxX()+", MinY="+bounding.getMinY()+", MaxY="+bounding.getMaxY()+" | Player: Ply_y="+ply_y +", Ply_x="+ply_x+" | Intersection Höhe="+intersection.getHeight());
// opposite case, move down
ply_y += intersection.getHeight();
}
}
if (bounding.getMinX() < maxX && bounding.getMaxX() > minX) {
// collision is horizontal
if (bounding.getMaxX() > minX) {
System.out.println("Player Links von Block: Block: MinX="+minX+", MaxX="+maxX+", MinY="+minY+", MaxY="+maxY+" | Playerrec: MinX="+bounding.getMinX()+", MaxX="+bounding.getMaxX()+", MinY="+bounding.getMinY()+", MaxY="+bounding.getMaxY()+" | Player: Ply_y="+ply_y +", Ply_x="+ply_x+" | Intersection Breite="+intersection.getWidth());
// right border of player has passed the left border of collision object, move left
ply_x -= intersection.getWidth();
} else {
System.out.println("Player Rechts von Block: Block: MinX="+minX+", MaxX="+maxX+", MinY="+minY+", MaxY="+maxY+" | Playerrec: MinX="+bounding.getMinX()+", MaxX="+bounding.getMaxX()+", MinY="+bounding.getMinY()+", MaxY="+bounding.getMaxY()+" | Player: Ply_y="+ply_y +", Ply_x="+ply_x+" | Intersection Breite="+intersection.getWidth());
// opposite case, move right
ply_x += intersection.getWidth();
}
}
}
}
}