【发布时间】:2018-12-03 23:59:11
【问题描述】:
我正在用java制作一个乒乓球类型的游戏,我试图让球从墙上反弹,但是每当球击中球时它就会停下来,它不会从墙上反射出来,我似乎看不到找出原因。
处理球运动的球类
public class Ball {
private double x;
private double y;
private double time;
private double xreflection=1.0;
private double yreflection=1.0;
private BallTrajectory traj=new BallTrajectory(20, 20);
public Ball(double x, double y) {
this.x=x;
this.y=y;
}
public void tick() {
time+=1.0/60.0;
if(x==0)
xreflection=1.0;
else if(x==Game.Width-15)
xreflection=-1.0;
if(y==0)
yreflection=1.0;
else if(y==Game.Height-15)
yreflection=-1.0;
x+=traj.xvel()*xreflection;
y-=traj.yvel(time)*yreflection;
}
public void render(Graphics g) {
g.setColor(Color.pink);
g.fillOval((int)x, (int)y, 15,15);
}
}
这个类处理球在抛射运动中的轨迹
public class BallTrajectory {
private double initvel;
private double theta;
public BallTrajectory(double initvel, double theta) {
this.initvel=initvel;
this.theta=theta;
}
public double xvel() {
double xvelo=initvel*Math.cos(Math.toRadians(theta));
return xvelo;
}
public double yvel(double time) {
double yvelo=initvel*Math.sin(Math.toRadians(theta))-(9.8*time);
return yvelo;
}
public double xpos(double time) {
double xpos=initvel*Math.cos(Math.toRadians(theta))*time;
return xpos;
}
public double ypos(double time) {
double ypos=initvel*Math.sin(Math.toRadians(theta))*time-.5*9.8*Math.pow(time, 2);
return ypos;
}
【问题讨论】:
-
你真的需要开始学习如何调试代码,包括桌面检查你的逻辑、打印语句和使用调试器
-
我在想,因为值是
doubles,它们不太可能是“完全”的目标值,相反,您可能想使用if (x <= 0) {等等
标签: java animation graphics game-development pong