簡體   English   中英

物體碰撞檢測,強制玩家移動0

[英]object collision detection, force player movement 0

我不明白為什么當我撞到我的物體時我的角色會凍結。 例如,如果我撞到collision(),我可以左右移動,但是不能上移;如果我撞到collision2(),我可以在所有地方凍結,但是如果我撞到邊界,我仍然可以向各個方向移動問題。 xa和ya仍設置為0。

package com.questkings.game;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;

public class Player{

int x = 1; // Location of player
int y = 314; // location of player
int xa = 0; // Representation of where the player goes
int ya = 0; // Representation of where the player goes
private int speed = 2;
int[] playerPos = {x, y};
private static final int WIDTH = 30;
private static final int HEIGHT = 30;

private Game game;

public Player(Game game){
    this.game=game;
}

public void move(){
    if(x + xa < 0) // Left Bounds
        xa = 0;
    else if (x + xa > game.getWidth() - WIDTH) // Right Bounds
        xa = 0;
    else if (y + ya < 0) // Top Bounds
        ya = 0;
    else if(y + ya > game.getHeight() - WIDTH)
        ya = 0;
    else if (collision()) // Tile bounds
        ya = 0;
    else if (collision2()){
        ya = 0;
        xa = 0;
    }

    x = x + xa;
    y = y + ya;
}

// Method to find where player is located
public int[] Playerposition(){
    return playerPos;
}

public void Gravity(){

}

public void paint(Graphics2D g2d){
    //Draws player to screen
    g2d.drawImage(getPlayerImg(), x, y, null);
}

public Image getPlayerImg(){
    ImageIcon ic = new ImageIcon("C:/Users/AncientPandas/Desktop/KingsQuest/Misc/Images/Sprites/player.png");
    return ic.getImage();
}

public void keyReleased(KeyEvent e){
    xa = 0;
    ya = 0;
}

public void keyPressed(KeyEvent e){
    if (e.getKeyCode() == KeyEvent.VK_S)
        xa = -speed;
    if (e.getKeyCode() == KeyEvent.VK_F)
        xa = speed;
    if (e.getKeyCode() == KeyEvent.VK_E)
        ya = -speed;
    if (e.getKeyCode() == KeyEvent.VK_D)
        ya = speed;
}

public Rectangle getBoundsPlayer(){
    return new Rectangle(x, y, WIDTH, HEIGHT);
}

private boolean collision(){
    return game.maplayout.getBoundsBlock().intersects(getBoundsPlayer());
}

private boolean collision2(){
    return game.maplayout.getBoundsBlock2().intersects(getBoundsPlayer());
}


}

在我看來,如果collision()返回true,則您的Player將無法移動,因為yaxa設置為零。 這意味着一旦您擊中某物,您就會被困在那里直到您不擊中它,這完全超出了您的控制范圍。 如果您與之相撞的東西靜止不動,您將永遠無法擺脫。

我要解決的問題是移動檢查碰撞:

// handle hitting sides like normal
x = x + xa;
y = y + ya;
// now that you've moved, check if you're colliding
if (collision()){
  // if you have, go back to where you were
  x = x - xa;
  y = y - ya;
}

這樣,您可以安全地返回到以前的位置,因為您以某種方式到達了那里而不會發生碰撞。

我不確定您的碰撞邏輯是否可以解決這個問題,所以我唯一能說的是如果您正在碰撞,請嘗試將yaxa乘以-1,這將使您朝相反的方向前進,並希望擺脫傷害方式。 這個想法是,朝着您想要的方向前進會給您帶來麻煩,因此,以相反的方式前進應該可以解決它。 這樣做的問題是,如果向后移動不會阻止您碰撞,那么您將再次反轉方向,並且很可能最終會在您開始的地方繼續碰撞。 這形成了一個無限循環,在這個循環中您的玩家很可能看起來像是在癲癇發作,但是我看過很多游戲都發生過類似的物理錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM