Java动画,以1x、1y的频率更新动画
当我的动画正在进行时,人物会粘在一起我认为这是因为如果图形获得速度x=5y=5,我移动它们,然后检查它们是否击中任何物体,我的图形可能已经在第二个图形内。 我想检查他们是否更频繁地命中任何东西,但我不确定如何将我的方法放入actionPerformed方法中。 图形的速度不是恒定的。 你有什么想法、例子或建议吗Java动画,以1x、1y的频率更新动画,java,swing,animation,collision-detection,Java,Swing,Animation,Collision Detection,当我的动画正在进行时,人物会粘在一起我认为这是因为如果图形获得速度x=5y=5,我移动它们,然后检查它们是否击中任何物体,我的图形可能已经在第二个图形内。 我想检查他们是否更频繁地命中任何东西,但我不确定如何将我的方法放入actionPerformed方法中。 图形的速度不是恒定的。 你有什么想法、例子或建议吗 public class PaintFigures extends JPanel implements ActionListener { static List<Figu
public class PaintFigures extends JPanel implements ActionListener {
static List<Figure> figuresList = new ArrayList<Figure>();
Timer t = new Timer(5, this);
public PaintFigures(List<Figure> figuresList) {
PaintFigures.figuresList = figuresList;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
t.start();
for (Figure figure : figuresList) {
figure.drawItself(g2d);
}
}
@Override
public void actionPerformed(ActionEvent e) {
FiguresUpdate.update(figuresList); // Check if they hit anything (other figure or frame)
FiguresUpdate.move(figuresList); // move them
repaint();
}
}
这与你所适用的法律有关。如果反弹时间不够长,它们可能会永远粘在一起。一个简单的规则是:如果两个图形发生碰撞,并且图1低于图2(f1.xf2.x),则f1会反弹一点,否则(f1.x>f2.x)会反弹一点。它现在似乎对我有用。您需要检查法律以及它们给出的值(newSpeedF1X等)
检测到公共静态无效图形(正方形f1,正方形f2){
//弹性碰撞
//图1
双新闻速度1x=(f1.getVelocityX()*(f1.getMass()-f2.getMass())+(2*f2
.getMass()*f2.getVelocityX())
/(f1.getMass()+f2.getMass());
双新闻速度1y=(f1.getVelocityY()*(f1.getMass()-f2.getMass())+(2*f2
.getMass()*f2.getVelocityY())
/(f1.getMass()+f2.getMass());
//图2
双新闻速度2x=(f2.getVelocityX()*(f2.getMass()-f1.getMass())+(2*f1
.getMass()*f1.getVelocityX())
/(f1.getMass()+f2.getMass());
双新闻速度2Y=(f2.getVelocityY()*(f2.getMass()-f1.getMass())+(2*f1
.getMass()*f1.getVelocityX())
/(f1.getMass()+f2.getMass());
System.out.println(“prev”+f1.getprevx()+“”+newSpeedF1X+“”+newSpeedF1Y+“”+newSpeedF2X+“”+newSpeedF2Y);
//f1.setLocationX(f1.getLocationX()+(newSpeedF1X));
//f1.setLocationY(f1.getLocationY()+(newSpeedF1Y));
//f2.setLocationX(f2.getLocationX()+(newSpeedF2X));
//setLocationY(f2.getLocationY()+(newSpeedF2Y));
如果(f1.getLocationX()您好首先您将它们移到了这里:
f1.setLocationX(f1.getLocationX() + (newSpeedF1X));
f1.setLocationY(f1.getLocationY() + (newSpeedF1Y));
f2.setLocationX(f2.getLocationX() + (newSpeedF2X));
f2.setLocationY(f2.getLocationY() + (newSpeedF2Y));
所以这不是频率问题
问题必须在公式中,而且是这样。
你得到:
而在newSpeedF2Y中应该是Velocityyy1f1而不是X
newSpeedF2Y = velocityYF2*(MassF2-MassF1)+(2*MassF1*VelocityYF1)/(MassF1+MassF2)
附加备注
墙壁反弹检测:
我注意到你的人像被卡在墙上,当他们离开堤岸时,你把他们的速度改成-速度,这样他们就不能出去了。
为了避免数字卡在墙上,您应该这样做:
public void wallXBounceDetect(Figure f) {
f.setVelocityX(wallBounceDetect(f.getLocationX(), f.getWidth(), canvas.getWidth(), f.getVelocityX()));
}
public void wallYBounceDetect(Figure f) {
f.setVelocityY(wallBounceDetect(f.getLocationY(), f.getHeight(), canvas.getHeight(), f.getVelocityY()));
}
public double wallBounceDetect(double location, double size, double maxValue, double velocity) {
if ((location < 0 && velocity < 0) || (location + size > maxValue && velocity > 0)) {
return -velocity;
}
return velocity;
}
public void wallXBounceDetect(图f){
f、 setVelocityX(wallbuncedetect(f.getLocationX(),f.getWidth(),canvas.getWidth(),f.getVelocityX());
}
公共空隙壁厚检测(图f){
f、 setVelocityY(wallbuncedetect(f.getLocationY(),f.getHeight(),canvas.getHeight(),f.getVelocityY());
}
公共双墙BounceDetect(双位置、双尺寸、双最大值、双速度){
如果((位置<0和速度<0)| |(位置+大小>最大值和速度>0)){
返回速度;
}
返回速度;
}
其中canvas是你的类,它使用扩展了JPanel的方法PaintComponent。你需要给出完整的代码。它很长,我不确定这是个好主意。但是你可以问我一个关于代码的问题,我会粘贴代码,写伪代码,或者只是写它是如何工作的。不要在paint
中启动你的计时器,事实上,你不应该e使用paint
,但更喜欢paintComponent
,而不是考虑提供一个演示您的问题的示例。这不是一个代码转储,而是您正在做的一个示例,它突出了您遇到的问题。这将减少混乱并获得更好的响应。我将在下面检查您的答案,新年快乐。
for (int j = i + 1; j < list.size(); j++) {
int ij=j%list.size();
if (list.get(i).getBounds().intersects(list.get(j).getBounds())
&& (!list.get(i).getDidHeBounce())
&& (!list.get(ij).getDidHeBounce())
) {
System.out.println(list.get(i).getClass().getSimpleName());
FigureBounceDetected(list.get(i), list.get(ij));
}
public void drawItself(Graphics2D g){
Color c=g.getColor();
Graphics2D g2d = (Graphics2D) g;
square = new Rectangle2D.Double(locationX,locationY,height,width);
g.setColor(Color.BLUE);
g.fill(square);
g.setColor(c);
}
f1.setLocationX(f1.getLocationX() + (newSpeedF1X));
f1.setLocationY(f1.getLocationY() + (newSpeedF1Y));
f2.setLocationX(f2.getLocationX() + (newSpeedF2X));
f2.setLocationY(f2.getLocationY() + (newSpeedF2Y));
//figure 1
newSpeedF1X = velocityXF1*(MassF1-MassF2)+(2*MassF2*VelocityXF2)/(MassF1+MassF2)
newSpeedF1Y = velocityYF1*(MassF1-MassF2)+(2*MassF2*VelocityXF2)/(MassF1+MassF2)
// Figure 2
newSpeedF2X = velocityXF2*(MassF2-MassF1)+(2*MassF1*VelocityXF1)/(MassF1+MassF2)
newSpeedF2Y = velocityYF2*(MassF2-MassF1)+(2*MassF1*VelocityXF1)/(MassF1+MassF2)
newSpeedF2Y = velocityYF2*(MassF2-MassF1)+(2*MassF1*VelocityYF1)/(MassF1+MassF2)
public void wallXBounceDetect(Figure f) {
f.setVelocityX(wallBounceDetect(f.getLocationX(), f.getWidth(), canvas.getWidth(), f.getVelocityX()));
}
public void wallYBounceDetect(Figure f) {
f.setVelocityY(wallBounceDetect(f.getLocationY(), f.getHeight(), canvas.getHeight(), f.getVelocityY()));
}
public double wallBounceDetect(double location, double size, double maxValue, double velocity) {
if ((location < 0 && velocity < 0) || (location + size > maxValue && velocity > 0)) {
return -velocity;
}
return velocity;
}