我写一个程序,允许用户左键单击面板,并添加一个反弹球,并用鼠标右键单击要删除一个球。球以不变的速度从所有的墙壁弹出。我完成了所有这些。我想不出的最后一部分是处理与其他弹跳球的碰撞。应该发生与从墙壁反弹相同的结果。任何帮助将不胜感激。下面是球至今的移动代码...弹跳球的碰撞
@SuppressWarnings("AccessingNonPublicFieldOfAnotherObject")
private void processMovement(long interval)
{
/* Compute the distance moved in the interval. Decompose the distance
moved into x and y components, and determine the next position of the Ball
by those amounts. */
float x = position.x + velocity.getDistanceX(interval);
float y = position.y + velocity.getDistanceY(interval);
position.setLocation(x, y);
/* Collisions with the walls. If so, adjust the speed and direction for
the next period. */
/* Collision with the east or west (vertical) wall. */
float wall;
if(x <= (wall = 0.0F) ||
x >= (wall = model.getDimension().width - SIZE))
{
x = wall;
velocity.reverseX();
}
/* Collision with the north or south (horizontal) wall. */
if(y <= (wall = 0.0F) ||
y >= (wall = model.getDimension().height - SIZE))
{
y = wall;
velocity.reverseY();
}
position.setLocation(x, y);
}
你尝试过这么远吗?为什么它不工作?这是作业还是课程作业? – Bobulous 2015-04-04 23:02:08
您需要一个集合来存储每个球对象。然后你需要检查他们对方的位置,看看是否发生了碰撞。 quradtree结构是一个有效的解决方案 – 2015-04-04 23:35:39