【发布时间】:2015-04-04 22:11:54
【问题描述】:
我正在编写一个程序,允许用户左键单击面板并添加一个弹跳球,然后右键单击以删除一个球。球以恒定的速度从所有墙壁反弹。我已经完成了这一切。我无法弄清楚的最后一部分是处理与其他弹跳球的碰撞。应该会出现与从墙壁反弹的结果相同的结果。任何帮助将不胜感激。这是到目前为止球运动的代码......
@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);
}
【问题讨论】:
-
到目前为止您尝试过什么?为什么它不起作用?这是家庭作业还是课程作业?
-
您需要一个集合来存储每个球对象。然后,您需要根据彼此的位置检查它们以查看是否发生碰撞。 quradtree 结构是解决此问题的有效解决方案
标签: java multithreading