gpt4 book ai didi

java - 侧面碰撞

转载 作者:行者123 更新时间:2023-11-30 11:02:02 25 4
gpt4 key购买 nike

我正在制作一个 tilemap 游戏,我已经了解了简单的碰撞。然而,为了让游戏正常运行,我必须知道矩形的哪一侧撞到了墙/砖。只需在主碰撞中放置一个简单的碰撞代码:

if(spriteX < brickX + brickwidth) {}

没用。目前主要的碰撞代码是:

for(int counter = 0; counter < 31; counter++) {

if(spriteX + 40 >= collisionX[counter] && collisionX[counter] + 100 >= spriteX
&& spriteY + 40 >= collisionY[counter] && collisionY[counter] + 100 >= spriteY) {

velX = 0;
velY = 0;

collisions = counter;

} else {

if(counter == collisions && jumping == false) {

fall();
}
}
}

如果你想要整个类(class):

package Main;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

public class Panel extends JPanel implements Runnable, KeyListener {

// dimensions

public static final int width = 800;
public static final int height = 800;
public static final int scale = 1;

// main loop

private Thread thread;
private boolean running = false;
private int FPS = 60;
private int targetTime = 1000 / FPS;

// drawing

private Graphics2D g;
private BufferedImage image;

int x;
int y;

boolean makeCollision = false;

// sprite

int spriteX = 210;
int spriteY = 200;
int velX = 0;
int velY = 10;

public boolean notOnGround = true;

int counter;
int collisionsCounter;
int jumps = 0;

public int row;
public int column;

public boolean collision;
public boolean jumping = false;

public String side = null;

// tilemap

int[][] map = {

{1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 1, 0, 0, 1, 1},
{1, 0, 0, 0, 0, 1, 1, 1},
{1, 0, 0, 0, 1, 1, 0, 1},
{1, 0, 0, 1, 1, 0, 0, 1},
{1, 0, 0, 1, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1}

};

int[] collisionX = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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* scale, height * scale));
setFocusable(true);
requestFocus();

}

public void addNotify() {
super.addNotify();

if(thread == null) {

running = true;
addKeyListener(this);
thread = new Thread(this);
thread.start();

}

}

public void init() {

image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
g = (Graphics2D) image.getGraphics();

}

public void update() {

if(spriteY < jump[0]) {

System.out.println(jump[0]);

jumping = false;

fall();

}

}

public void draw() {

g.clearRect(0, 0, WIDTH, HEIGHT);

x = 0;
y = 0;

for(column = 0; column <= 7; column++) {

x = 0;

for(row = 0; row <= 7; row++) {

changeColor(row, column, g);

g.fillRect(x, y, 100, 100);

x = x + 100;

}

y = y + 100;

}

g.setColor(Color.YELLOW);
g.fillRect(spriteX, spriteY, 40, 40);

spriteX += velX;
spriteY += velY;

for(int counter = 0; counter < 31; counter++) {

if(spriteX + 40 >= collisionX[counter] && collisionX[counter] + 100 >= spriteX
&& spriteY + 40 >= collisionY[counter] && collisionY[counter] + 100 >= spriteY){

velX = 0;
velY = 0;

collisions = counter;

} else {

if(counter == collisions && jumping == false) {

fall();

}

}

}

}

public void changeColor(int rowGive, int columnGive, Graphics g) {

if(map[rowGive][columnGive] == 1) {

g.setColor(Color.BLACK);

if(counter < 30) {

collisionX[counter] = x;
collisionY[counter] = y;

}

counter++;

} else {

g.setColor(Color.WHITE);

}

}

public void fall() {

velY = 5;

}

public void drawToScreen() {

Graphics g2 = getGraphics();
g2.drawImage(image, 0, 0, width * scale, height * scale, null);
g2.dispose();

}

public void run() {

init();

long wait;
long elapsed;
long start;

while(running) {

start = System.nanoTime();
update();
draw();
drawToScreen();

elapsed = System.nanoTime() - start;

wait = targetTime - elapsed / 1000000;

if(wait < 0) wait = 5;

try {

thread.sleep(wait);

} catch(Exception e) {

e.printStackTrace();

}

}

}

public void keyPressed(KeyEvent e) {

int code = e.getKeyCode();

if(code == KeyEvent.VK_RIGHT) {

velX = 5;

}
if(code == KeyEvent.VK_LEFT) {
velX = -5;

}
if(code == KeyEvent.VK_SPACE && jumping == false) {

jumping = true;

velY = -5;
jump[0] = spriteY - 100;

}

}
public void keyReleased(KeyEvent e) {



}
public void keyTyped(KeyEvent e) {


}}

最佳答案

让我们试着把问题稍微分解一下。您不需要找出矩形何时撞到墙上:您需要找出矩形的左侧何时撞到墙的右侧以及何时矩形的右侧碰到墙的左侧

更准确地说:不要测试对象。将对象划分为碰撞面(在您的情况下为左侧和右侧),找到一种对它们进行建模的方法(通常 x 和 x+width 是矩形的左侧和右侧,如果 x 是左上角的 x 坐标) .并在“或”条件下同时测试两者。

if(spriteX < brickX + brickwidth || spriteX + spritewidth > brickX) {}

edit 更仔细地阅读你的完整类(class),看起来你做了类似的事情,但你使用了“&&”条件,这意味着你永远不可能是真的,因为双方的碰撞不可能同时发生时间。但我可能错了,你能告诉我吗?

关于java - 侧面碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30929466/

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