2015-02-06 20 views
1

我正在制作一个简单的加工程序,用于创建一定数量的随机移动的点,然后当点击鼠标时点移动到鼠标的位置。加工点管理

我将这些点设置为省略号,以便更容易看清楚。

//number of points 
int ptnum=2; 
//list of points 
Point[] points=new Point[ptnum]; 

//class to create points 
class Point 
{ 
    float xpos; 
    float ypos; 

    //constructor 
    Point(float x, float y){ 
    xpos=x; 
    ypos=y; 
    } 

    //return x-coordinate 
    float ptx(){ 
    return xpos; 
    } 
    //return y-coordinate 
    float pty(){ 
    return ypos; 
    } 

    //points randomly moving 
    void randMove(){ 
    xpos+=random(-2,2); 
    ypos+=random(-2,2); 
    } 

    //display points 
    void display(){ 
    fill(0); 
    ellipse(xpos,ypos,2,2); 
    } 

    //move points to mouse 
    void move(){ 
    if(xpos>mouseX){ 
     xpos-=1; 
    } 
    if(ypos>mouseY){ 
     ypos-=1; 
    } 
    if(ypos<mouseY){ 
     ypos+=1; 
    } 
    if(xpos<mouseX){ 
     xpos+=1; 
    } 
    } 
} 


void setup(){ 
    size(640,360); 

    //create ptnum of points 
    for(int i=0; i<ptnum; i++){ 
    points[i]=new Point(random(1,width-1),random(1,height-1)); 
    } 
} 

//each point to random move 
void randomMovement(){ 
    for(int i=0; i<ptnum; i++){ 
    points[i].randMove(); 
    } 
} 

//each point to display 
void ptDisplay(){ 
    for(int i=0; i<ptnum; i++){ 
    points[i].display(); 
    } 
} 

//each point to move 
void ptMove(){ 
    for(int i=0; i<ptnum; i++){ 
    points[i].move(); 
    } 
} 

void draw(){ 
    //start 
    background(255,255,255); 
    noFill(); 
    ptDisplay(); 
    //========== 

    //if mouse clicked, move points to mouse XandY, if not-randommove 
    if(mousePressed){ 
    ptMove(); 
    } 
    else{ 
    randomMovement(); 
    } 
} 

我试图让它也可以互相交互,例如,他们不能互相接触。有人能帮我解决这个问题吗?我在这一个上有一个大脑放屁。如果有人对代码有任何建议,我很乐意听到他们的声音。

非常感谢您的帮助,我们对此表示感谢。

回答

1

只是计算绘制函数内的每个点的位置。 然后,运行for循环以查看是否有任何碰撞。由于绘图函数每秒运行多次,结果将足够好。

当您碰撞时,您可以将每个点的方向改变180度。