我正在进行一次跳跃运行游戏,但我的碰撞检测无法正常工作。 这是我的碰撞检测的支柱:
for(int i = 0; i < Frame.teilesArray.size();i++){
if(Frame.teilesArray.get(i).getimgInt()== 0 || Frame.teilesArray.get(i).getimgInt()== 1){
if(bounding.intersects(Frame.teilesArray.get(i).getBounding())){
Rectangle intersection = (Rectangle) bounding.createIntersection(Frame.teilesArray.get(i).getBounding());
}
}
}
这就是我试图解决的问题,但它不起作用:
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
if (bounding.OUT_TOP < Frame.teilesArray.get(i).getBounding().OUT_BOTTOM ) {
ply_y += intersection.getHeight();
}
if (bounding.OUT_LEFT < Frame.teilesArray.get(i).getBounding().OUT_RIGHT && richtung == 2) { //richtung links
ply_x += intersection.getWidth();
}
if (bounding.OUT_BOTTOM > Frame.teilesArray.get(i).getBounding().OUT_TOP ) {
ply_y -= intersection.getHeight();
}
if (bounding.OUT_RIGHT > Frame.teilesArray.get(i).getBounding().OUT_LEFT && richtung == 1) { //richtung rechts
ply_x -= intersection.getWidth();
}
}
}
}
洞穴代码:
package main;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Player {
private Rectangle bounding;
private float ply_x;
private float ply_y;
private float f_dx;
private float f_dy;
private float f_gravity;
private float f_counter;
private float neededTime;
private boolean b_air;
private boolean b_airimg;
private boolean letzterichtung;
private int richtung;
private BufferedImage playerImage;
private BufferedImage[] laufr;
private BufferedImage[] laufl;
public Player() {
laufr = new BufferedImage[3];
laufl = new BufferedImage[3];
ply_x = 0;
ply_y = 640;
f_counter = 0;
neededTime = 10;
f_dx = 5;
f_dy = -9;
f_gravity = 0.4f;
b_air = false;
b_airimg = false;
letzterichtung = true;
richtung = 0;
try {
playerImage = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(0, 0, 64, 64);
laufr[0] = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(64, 0, 64, 64);
laufr[1] = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(128, 0, 64, 64);
laufr[2] = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(192, 0, 64, 64);
laufl[0] = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(64, 64, 64, 64);
laufl[1] = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(128, 64, 64, 64);
laufl[2] = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(192, 64, 64, 64);
} catch (IOException e) {e.printStackTrace();}
bounding = new Rectangle((int) ply_x, (int) ply_y, playerImage.getWidth(), playerImage.getHeight());
}
@SuppressWarnings("static-access")
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();
bounding.x = (int) ply_x;
bounding.y = (int) ply_y;
} 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();
bounding.x = (int) ply_x;
bounding.y = (int) ply_y;
}
}
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();
bounding.x = (int) ply_x;
bounding.y = (int) ply_y;
} 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();
bounding.x = (int) ply_x;
bounding.y = (int) ply_y;
}
}
}
}
}
if(getBounding().x >= 400){
ply_x = 399;
}
if(getBounding().x <=0 ){
ply_x = 0;
}
if(keyCheck.keysCheck(KeyEvent.VK_A)){
ply_x -= f_dx;
if(b_air == false){
if(f_counter >= neededTime*1.5f){
f_counter = 0;
}
if(f_counter == 0) {
playerImage = laufl[0];
f_counter++;
}else if(f_counter == 5){
playerImage = laufl[1];
f_counter++;
}else if(f_counter == 10){
playerImage = laufl[2];
f_counter++;
}else {
f_counter++;
}
richtung = 2;
letzterichtung = false;
}
}
if(keyCheck.keysCheck(KeyEvent.VK_D)){
ply_x += f_dx;
if(b_air == false){
if(f_counter >= neededTime*1.5f){
f_counter = 0;
}
if(f_counter == 0) {
playerImage = laufr[0];
f_counter++;
}else if(f_counter == 5){
playerImage = laufr[1];
f_counter++;
}else if(f_counter == 10){
playerImage = laufr[2];
f_counter++;
}else {
f_counter++;
}
}
richtung = 1;
letzterichtung = true;
}
if(keyCheck.keysCheck(KeyEvent.VK_D)==false && keyCheck.keysCheck(KeyEvent.VK_A)==false){
if(richtung == 1 || letzterichtung == true && b_air == false && b_airimg ==false){
try {
playerImage = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(0, 0, 64, 64);
} catch (IOException e) {e.printStackTrace();}
}
else if(richtung == 2 || letzterichtung == false && b_air == false && b_airimg ==false){
try {
playerImage = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(0, 64, 64, 64);
} catch (IOException e) {e.printStackTrace();}
}
richtung = 0;
}
if(keyCheck.keysCheck(KeyEvent.VK_SPACE)){
b_air = true;
}
if(b_air ==true){
if(b_airimg == false){
if (letzterichtung == true)
{
try {
playerImage = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(256, 0, 64, 64);
} catch (IOException e) {e.printStackTrace();}
}else {
try {
playerImage = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(256, 64, 64, 64);
} catch (IOException e) {e.printStackTrace();}
}
b_airimg = true;
}
if(ply_y >= 646){
f_dy = -9;
b_air = false;
b_airimg = false;
}
f_dy += f_gravity;
ply_y += f_dy;
}
bounding.x = (int) ply_x;
bounding.y = (int) ply_y;
}
public Rectangle getBounding() {
return bounding;
}
public BufferedImage getPlayerimage() {
return playerImage;
}
public void setPlayerLocation(int ply_x, int ply_y){
this.ply_x = ply_x;
this.ply_y = ply_y;
bounding.x = (int) this.ply_x;
bounding.y = (int) this.ply_y;
System.out.println(""+this.ply_x+" "+this.ply_y);
}
}
答案 0 :(得分:0)
如果是这样的话,请尝试使用此代码:
// 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) {
// bottom of player has passed the top border of collision object, move up
ply_y -= intersection.getHeight();
} else {
// opposite case, move down
ply_y += intersection.getHeight();
}
}
if (bounding.getMinX() < maxX && bounding.getMaxX() > minX ) {
// collision is horizontal
if (bounding.getMaxX() > minX) {
// right border of player has passed the left border of collision object, move left
ply_x -= intersection.getWidth();
} else {
// opposite case, move right
ply_x += intersection.getWidth();
}
}