好的,现在我正在开发一个基于布朗运动的小型 derp-ish 游戏,并且我正在研究粒子的碰撞墙功能,但它不起作用。基本上,我用弧度表示粒子的方向,每当它撞到墙上时,我都会在方向上添加 Pi 弧度来翻转它,但由于某种原因,它要么没有被调用,要么不工作。这些是我拥有的代码片段,欢迎提出任何建议。
import java.awt.*;
public class Particle implements Actor {
protected double xPos;
protected double yPos;
protected Velocity v;
protected Color hue;
protected boolean needsUpdate;
public Particle(){
this((Math.random()*500),(Math.random()*500),new Velocity((int)(Math.random()*500),(Math.random()*Velocity.TAU)));
}
public Particle(double x, double y, Velocity vel){
xPos=x;
yPos=y;
v=vel;
hue=Color.red;
needsUpdate=false;
}
public void draw(Graphics g) {
g.setColor(hue);
g.fillOval((int)xPos, (int)yPos, 16, 16);
}
public void act() {
xPos+=v.getSlopefromDirection();
yPos+=1;
}
public void onHitWall(int dir) {
v.setDirection((v.getDirection()+Math.PI)%(Math.PI*2));
}
public void onHitOther(Actor other) {
}
public boolean canCollide() {
return true;
}
public int getLeftX() {
return (int)xPos;
}
public int getRightX() {
return (int)xPos+4;
}
public int getTopY() {
return (int)yPos;
}
public int getBottomY() {
return (int)yPos+4;
}
}
这是我用来显示它的类:
import java.awt.*;
import java.util.*;
import javax.swing.*;
import static java.lang.System.out;
public class Feild extends JFrame{
protected ArrayList<Actor> actors;
private final int size=500;
protected Thread th;
public Feild(ArrayList<Actor> a){
super("A Brownian Love Story");
setSize(size, size);
actors=a;
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
th = new Thread();
while(true){
paint(getGraphics());
}
}
public void paint(Graphics g){
super.paint(g);
//g.fillRect(0, 0, 500, 500);
for(int i=0;i<actors.size();i++){
if(actors.get(i).getLeftX()<=0)
actors.get(i).onHitWall(1);
if(actors.get(i).getRightX()>=500)
actors.get(i).onHitWall(2);
if(actors.get(i).getTopY()<=0)
actors.get(i).onHitWall(1);
if(actors.get(i).getBottomY()>=500)
actors.get(i).onHitWall(2);
actors.get(i).act();
actors.get(i).draw(g);
}
for(int i=0;i<1000;i++){out.println(i);}
}
}
所以我尝试将绘制功能更改为在执行后检查碰撞,它仍然看起来像 onHitWall 被跳过,即使在放入打印语句之后它不是。