2012-02-19 74 views
0

好吧,现在我正在研究一个基于布朗运动的小型锐化游戏,我正在研究撞击壁功能的粒子,但它不起作用。基本上,我用弧度表示粒子的方向,每当它碰到墙时,我都会将Pi弧度添加到方向来翻转它,但由于某种原因,它要么不会被调用,要么不会工作。这些是我拥有的代码,欢迎任何建议。我有一些(阅读:EXTREMELY)破的命中墙功能,我不明白我在做什么错

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即将被跳过,尽管在放置打印语句后它不是。

+2

首先,它是一个“字段”,而不是“Feild”。现在,什么是'速度'?此外,在寻求帮助之前,您至少应尝试调试代码 - 在“onHitWall”中插入打印语句很简单。最后,“这不起作用”对问题没有帮助。它有什么作用? – Borealid 2012-02-19 03:04:03

+0

1.)手指的滑动我没有打算纠正。 2.)从描述可以推断,它实质上是一个数据结构,其中包含一个速度和方向的组成部分 3)描述的非触发部分暗示它看起来像跳过 – natman3400 2012-02-19 03:10:39

回答

1

你现在面临的问题是,你的粒子会移动到撞到墙上,然后下一次你看完时,他们仍然会在墙上,再次转身,然后继续进入墙壁。

如果您在执行act()函数后检查碰撞,应该可以解决问题。

他们会进入墙壁,看到他们在墙上,然后转身。下一个循环,他们将移回墙外,继续前进。

+0

谢谢,现在你已经提到了这个逻辑错误,它就像拇指一样伸出手。 再次感谢您的意见。 – natman3400 2012-02-19 03:07:37

+0

等一下,由于某种原因,它仍然不起作用。在主体中描述。 – natman3400 2012-02-19 03:16:10

+0

现在我已经更仔细地观察了act()函数,您的Y轴方向被忽略了。实际上,您的代码只能在x轴上正确碰撞。发布Velocity的代码,因为它看起来并不像斜率函数在做你认为的那样。 – xthexder 2012-02-19 03:20:46

相关问题