【发布时间】:2014-09-07 16:22:23
【问题描述】:
我正在尝试开发我的第一个 android 游戏,它是名为“brick断路器”的游戏。 我相信每个人都知道。
一切都很好,直到我来到弹跳部分。
为了处理冲突,我使用这个找到的代码:
scene.registerUpdateHandler(new IUpdateHandler() {
public void reset() { }
public void onUpdate(final float pSecondsElapsed) {
if(ball.collidesWith(paddle)) {
ball.bounceWithRectangle(paddle);
}
else if (ball.getY() >= Game.getCAMERA_HEIGHT() - 30) {
scene.setBackground(new ColorBackground(255f, 0f, 0f));
}
else {
for (int i = 0; i < bricks.length; i++) {
for (int j = 0; j < bricks[0].length; j++) {
scene.setBackground(new ColorBackground(0f, 0f, 0f));
if(ball.collidesWith(bricks[i][j])) {
bricks[i][j].setPosition(CAMERA_HEIGHT+20, CAMERA_WIDTH+20);
scene.getTopLayer().removeEntity(bricks[i][j]);
ball.bounceWithRectangle(bricks[i][j]);
}
}
}
}
}
});
这就是 Ball.java 类的样子:
package com.example.zaidimas;
import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import java.lang.Math;
public class Ball extends AnimatedSprite {
float velocity = 100;
int i =0;
private Engine mEngine;
public Ball(float positionX, float positionY, TiledTextureRegion positionTextureRegion, Engine mEngine) {
super(positionX, positionY, positionTextureRegion);
this.mEngine = mEngine;
}
protected void onManagedUpdate(final float pSecondsElapsed) {
if(this.mX < 0) {
this.setVelocityX(velocity);
} else if(this.mX + this.getWidth() > Game.getCAMERA_WIDTH()) {
this.setVelocityX(-velocity);
}
if(this.mY < 0) {
this.setVelocityY(velocity);
} else if(this.mY + this.getHeight() > Game.getCAMERA_HEIGHT()) {
this.setVelocityY(-velocity);
}
super.onManagedUpdate(pSecondsElapsed);
}
public void bounceWithRectangle(Rectangle rectangle){
this.setVelocityY(-this.getVelocityY());
}
}
它有效,而且很清楚。
但问题是球总是以相同的角度从所有表面反弹。 我认为这个角度是90度。
- 我很想知道如何让球以不同的角度反弹,如何 计算这些角度和所有其他有帮助的信息 让游戏更加逼真。
我已经找到了一些信息,但我无法将其调整为这个项目。
- 也许有人可以更详细地向我解释一下,给一些 示例甚至帮助在此项目中修复它?
我希望我的问题很清楚。
附:游戏基于AndEngine aaand 最重要的是——对不起我的英语不好:D
祝你早日愉快!
【问题讨论】:
-
我建议你对这里涉及的物理学做一些研究。在这些类型的碰撞中,反弹角始终与入射角相同。
标签: android math game-physics