gpt4 book ai didi

java - 如何实现两个不可旋转矩形之间的碰撞响应

转载 作者:行者123 更新时间:2023-12-01 17:43:23 25 4
gpt4 key购买 nike

我需要帮助制作一个自上而下的射击游戏,我不知道如何阻止我的玩家直接穿过方 block ,下面是我的 Player 类和 keyInput 类。 我是游戏编程的新手,我搜索了很多不同的解决方案,但不知道如何实现它们......(抱歉,如果我不是很详细,我只是不知道如何正确解释我的代码)*图像是运行代码时发生的情况,我可以移动播放器

package main;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

public class Player extends GameObject{

Handler handler;


private int timer = 20;
private boolean canFire = false;
public static int direction = 1;
public Player(int x, int y, ID id , Handler handler) {
super(x, y, id);
this.handler = handler;



}
public Rectangle getBounds() {
return new Rectangle (x, y, 16, 16);

}
public void tick() {
x += velX;
y += velY;


x = Game.clamp(x, 0, Game.WIDTH - 31);
y = Game.clamp(y, 1, Game.HEIGHT - 54);
if(timer >= 0) timer --;





if(KeyInput.facing[0] == "Up") {

direction = 4;
} else if(KeyInput.facing[1] == "Down") {

direction = 3;
}else if(KeyInput.facing[2] == "Left") {

direction = 2;
}else if(KeyInput.facing[3] == "Right") {

direction = 1;
}


if(KeyInput.Fire == true) {
if(timer <=0)
{

handler.addObject(new Bullet(x + 8, y + 8,ID.Bullet, handler, direction));
timer = 20;
}

}


collision();
}

private void collision() {
for (int i = 0; i < handler.object.size(); i++) {
GameObject tempObject = handler.object.get(i);

if(tempObject.getId() == ID.Block)
{
if(getBounds().intersects(tempObject.getBounds()))
{
//collision events

}
}
}

}
public void render(Graphics g) {

g.setColor(Color.white);
g.fillRect((int)x,(int)y,24,24);


}

}
package main;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class KeyInput extends KeyAdapter{

private Handler handler;
private boolean[] keyDown = new boolean[4];
private double Multi = 1;
public static String[] facing = new String[4];
public static boolean Fire = false;


public KeyInput(Handler handler) {
this.handler = handler;
keyDown[0] = false;
keyDown[1] = false;
keyDown[2] = false;
keyDown[3] = false;
facing[0] = "";
facing[1] = "";
facing[2] = "";
facing[3] = "";
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();

GameObject tempObject = handler.object.get(0);

if (key == KeyEvent.VK_W) {tempObject.setVelY((int)(-3* Multi)); keyDown[0] = true;facing[0] = "Up";}

if (key == KeyEvent.VK_S) {tempObject.setVelY((int)(3* Multi)); keyDown[1] = true;facing[1] = "Down";}

if (key == KeyEvent.VK_A) {tempObject.setVelX((int) (-3* Multi)); keyDown[2] = true;facing[2] = "Left";}

if (key == KeyEvent.VK_D) {tempObject.setVelX((int)(3 * Multi)); keyDown[3] = true;facing[3] = "Right";}

if (key == KeyEvent.VK_SHIFT) {Multi = 2.2;}

if (key == KeyEvent.VK_SPACE) {Fire = true;}

if(key == KeyEvent.VK_ESCAPE) System.exit(0);
}

public void keyReleased(KeyEvent e) {

int key = e.getKeyCode();
GameObject tempObject = handler.object.get(0);
if (key == KeyEvent.VK_W) {keyDown[0] = false;facing[0] = "";}//tempObject.setVelY(0);
if (key == KeyEvent.VK_S) {keyDown[1] = false;facing[1] = "";}//tempObject.setVelY(0);
if (key == KeyEvent.VK_A) {keyDown[2] = false;facing[2] = "";}//tempObject.setVelX(0);
if (key == KeyEvent.VK_D) {keyDown[3] = false; facing[3] = "";}//tempObject.setVelX(0);
if (key == KeyEvent.VK_SHIFT) {Multi = 1;}
if (key == KeyEvent.VK_SPACE) {Fire = false;}

if(!keyDown[0] && !keyDown[1]) tempObject.setVelY(0);
if(!keyDown[2] && !keyDown[3]) tempObject.setVelX(0);
}
}

最佳答案

至少可以使用两种策略来应对碰撞:纠正或预防。

校正意味着您移动角色,然后检查它是否发生碰撞,如果发生碰撞,则应用校正。这意味着,您找到玩家应该所在的位置并相应地移动它。这称为“响应碰撞”。

另一方面,预防意味着在移动之前进行一些检查,然后仅将玩家移动到碰撞点。例如,这需要一些光线转换技术。

就您的情况而言,考虑到代码的组织方式,您可能希望实现“更正”方法。

您有一段代码可以检查是否发生冲突(我假设它有效)。当检测到碰撞时,您可以调用某种方法来纠正位置。

private void collision() {
for (int i = 0; i < handler.object.size(); i++) {
GameObject tempObject = handler.object.get(i);

if(tempObject.getId() == ID.Block)
{
if(getBounds().intersects(tempObject.getBounds()))
{
//collision events

}
}
}
}

gamedev stackexchange 上有一个有趣的问题,您可以查看如何实现碰撞响应:https://gamedev.stackexchange.com/questions/160248/2d-rectangle-collision-resolution

我强烈建议使用具体化 vector 而不是简单的数字来表示位置和速度。从长远来看,它使一切变得更容易理解。

关于java - 如何实现两个不可旋转矩形之间的碰撞响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60906640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com