亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用矩形進行游戲沖突檢測

使用矩形進行游戲沖突檢測

收到一只叮咚 2022-09-01 16:37:22
我從我的第一款游戲開始,但無法弄清楚碰撞檢測應該如何工作。目標是玩家不應該能夠進入墻壁。我有以下類玩家(),墻(),級別()和游戲()玩家類是加載玩家并為玩家處理移動和碰撞。墻正在創建墻,我在這個類中有一個Arraylist,它正在為創建的每一面墻創建一個矩形。級別類正在加載創建和加載所有級別。游戲類是處理整個游戲循環等等。我試圖將x和y位置與玩家和墻壁進行比較,現在我正在嘗試使用尚未成功的.intersect類。玩家類的一部分:public void collision() {Rectangle r3 = getOffsetBounds();for (int i = 0; i < Wall.wallList.size(); i++) {    Rectangle w1 = Wall.wallList.get(i);    if (r3.intersects(w1)) {    }}}public int moveUp(int velY) {    collision();    y -= velY;    return y;}public int moveDown(int velY) {    collision();    y += velY;    return y;}public int moveLeft(int velX) {    collision();    x -= velX;      return x;}public int moveRight(int velX) {    collision();    x += velX;    return x;}public Rectangle getOffsetBounds() {    return new Rectangle(x + velX, y + velY, width, height);}public int getX() {    return x;}public void setX(int x) {    this.x = x;}public int getY() {    return y;}public void setY(int y) {    this.y = y;}public int getVelX() {    return velX;}public void setVelX(int velX) {    this.velX = velX;}public int getVelY() {    return velY;}public void setVelY(int velY) {    this.velY = velY;}}墻面類的一部分:static ArrayList<Rectangle> wallList = new ArrayList<Rectangle>();public int getX() {    return x;}public void setX(int x) {    this.x = x;}public int getY() {    return y;}public void setY(int y) {    this.y = y;}public void setVisisble(Boolean visible) {    this.visible = visible;}public Rectangle getBounds() {    return new Rectangle(x,y,width,height);}public Rectangle setBounds(int x,int y,int width,int height) {    this.x = x;    this.y = y;    this.width = width;    this.height = height;    return new Rectangle(x,y,width,height);}}設置墻列表的“級別”類的一部分                if(level1[i][j]==1) {                w = new Wall(j*25, i*25, width, height);                w.paint(g);                Wall.wallList.add(w.setBounds(j*25+10, i*25+10, width, height));                }
查看完整描述

1 回答

?
守著星空守著你

TA貢獻1799條經驗 獲得超8個贊

這就是我總是如何處理任何碰撞

if (((x1<x2 && x1+w1>x2) || (x2<x1 && x2+w2>x1)) && 
    ((y1<y2 && y1+h1>y2) || (y2<y1 && y2+h2>y1)))


查看完整回答
反對 回復 2022-09-01
  • 1 回答
  • 0 關注
  • 86 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號