我正在开发一款游戏,你必须避免使用太空飞船的小行星(精灵)。不幸的是我的碰撞方法不起作用。我可以在手机上运行代码,但不会打印出“碰撞”。我想某种方式我搞砸了一些坐标,但我找不到错误。有什么想法吗?
public class CollisionManager{
private List<Asteroid> asteroids = new ArrayList<Asteroid>();
private Spaceship s = new Spaceship();
public void checkCollisions(){
for(int i = 0; i < asteroids.size(); i++)
{
if(asteroids.get(i).getBounds().contains(s.getBounds()) )
{
System.out.println("COLLISION");
}
}
}
}
主类:
[...]
@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if(ScreenManager.getCurrentScreen() != null){
ScreenManager.getCurrentScreen().update();
ScreenManager.getCurrentScreen().update(renderer);
ScreenManager.getCurrentScreen().update(cam);
ScreenManager.getCurrentScreen().checkCollisions();
}
if(ScreenManager.getCurrentScreen() != null){
ScreenManager.getCurrentScreen().render(batch);
ScreenManager.getCurrentScreen().render(renderer);
}
}
小行星:
[...]
public void update(float deltaT) {
p.x += v.x * deltaT;
p.y += v.y * deltaT;
}
public Circle getBounds()
{
return new Circle(p.x, p.y, radius);
}
飞船:
[...]
public void update() {
System.out.println("update");
if (Gdx.input.isTouched()) {
y = (height - Gdx.input.getY()) /height * MyGdxGame.HEIGHT;
x = Gdx.input.getX() / width * MyGdxGame.WIDTH;
sprite.setPosition(-sprite.getWidth() / 2 + x, sprite.getHeight()/2
+ y);
}
public Circle getBounds()
{
return new Circle(sprite.getX(), sprite.getY(), sprite.getWidth());
}