gpt4 book ai didi

java - 我的碰撞是 buggy ,有时会剧烈振动或出界

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

我正在使用 Beginning Android Games 中的游戏引擎并从头开始制作突破游戏。我的 Racket /球/积木可以正常工作,但现在我正在解决错误。随机地,我的乒乓球会撞到任何墙壁,它会粘在上面并左右振动,然后飞出屏幕。另一个例子是,有时乒乓球击打 Racket 的方式不是弹开它,而是穿过它,然后在穿过它后继续改变方向,我觉得它不遵守我的界限。我在 nexus 7 上测试这里是 pong 类

public class Pong {
public float x, y;
public int speedX, speedY;
public static final int LEFT = 0;
public static final int RIGHT = 0;
public static final int WIDTH = 32;
public static final int HEIGHT = 32;
public static final int DIAMETER = 15;
public int direction;
public boolean inBounds;

public Pong(int x, int y) {
this.x = x;
this.y = y;
speedX = 500;
speedY = 500;
inBounds = true;
}

public void stop() {
speedX = 0;
speedY = 0;
}

public void update(float deltaTime) {
if (inBounds) {
x += speedX * deltaTime;
y += speedY * deltaTime;
if (x + WIDTH >= 800) {
speedX *= -1;
}
if (x <= 0) {
speedX *= -1;
}
if (y >= 1280) {
// inBounds = false;
speedY *= -1;
}
if (y <= 0) {
speedY *= -1;
}
}

}

public void reverse() {
speedY *= -1;
}

}

我的划桨课

public class Paddle {
public static final int LEFT = 0;
public static final int RIGHT = 1;
public static final int NEUTRAL = 2;
public static final int WIDTH = 192;
public static final int HEIGHT = 32;
public float x;
public int y;
public int speed;
public int direction;

public Paddle(int x, int y) {
this.x = x;
this.y = y;

}

public void turnLeft() {
speed = 500;
direction = LEFT;
}

public void turnRight() {
speed = 500;
direction = RIGHT;
}

public void stop() {
speed = 0;
}

public void update(float deltaTime) {

if (direction == LEFT && x > 0) {
x -= speed * deltaTime;
} else if (direction == RIGHT && x + WIDTH < 800) {
x += speed * deltaTime;
} else if (direction == NEUTRAL) {
stop();
}

}
}

我做的碰撞检测可能是错误的

private boolean paddleCollision(Paddle paddle, Pong pong) {
if (pong.x <= paddle.x + Paddle.WIDTH) {
if (pong.x >= paddle.x) {
if (pong.y + Pong.HEIGHT >= paddle.y) {
return true;
}
}
}
return false;

}

private boolean tileCollision(Tile tile, Pong pong) {
if (tile == null) {
return false;
}
if (pong.x <= tile.x + Tile.WIDTH) {
if (pong.x >= tile.x) {
if (pong.y <= tile.y + Tile.HEIGHT) {
if (pong.y + Pong.HEIGHT >= tile.y) {
return true;
}
}
}
}
return false;
}

最佳答案

我不确定这是解决方案,但是:我不太喜欢 Paddle::update() Pong::update() 方法,因为 if (x > 0 ... x < 800)你要确保 initial 位置有效,然后将最终位置设置为可能在屏幕之外的位置......我会选择类似的东西:

public class Paddle {
public void stop() {
speed = 0;
direction = NEUTRAL;
}

public void update(float deltaTime) {
// here, x is valid
x += (direction == LEFT?-1:+1) * speed * deltaTime;

if (x < 0) {
x = 0;
stop();
}
else if (x > 800) {
x = 800;
stop();
}

// here, x is valid
}
}

public class Pong {
public void update(float deltaTime) {
if (inBounds) {
x += speedX * deltaTime;
y += speedY * deltaTime;
if (x + WIDTH >= 800) {
speedX *= -1;
x = 800-WIDTH-1;
}
if (x <= 0) {
speedX *= -1;
x = 1;
}
if (y >= 1280) {
speedY *= -1;
y = 1279;
}
if (y <= 0) {
speedY *= -1;
y = 1;
}

// here, Pong is in screen
}

}
}

关于java - 我的碰撞是 buggy ,有时会剧烈振动或出界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22924455/

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