2017-03-19 80 views
1

处理处理中的碰撞非常简单。但是,如何确定与路径的碰撞?例如:如果您愿意,可以想象一下特朗的灯光循环,其中亮灯周期的轨迹不会消失。在Tron中,如果一个周期与另一个周期相交,而它本身包括在内,那么它就会“死亡”。如何在Processing中有效地找到此事件?处理:与小径碰撞

回答

2

一个诡秘的解决方法是将线绘制到PImage中,并检查某个位置的颜色是否与背景相同(例如,预先存在的线,因此是碰撞)。

这里的概念的粗略(略低效(由于get()/set()电话)证明:

PImage buffer; 

//how mutch we scale down = how pixely this will look 
int multiplier = 8; 
//scaled down width/height 
int w,h; 
//cursor position 
int px,py; 
//cursor velocity; 
int vx,vy; 


void setup(){ 
    size(400,400); 
    noSmooth(); 

    w = width/multiplier; 
    h = height/multiplier; 

    buffer = createImage(w,h,RGB); 
    clear(); 
} 
void clear(){ 
    java.util.Arrays.fill(buffer.pixels,color(0)); 
    buffer.updatePixels(); 
} 
void draw(){ 
    //update cursor 
    px += vx; 
    py += vy; 
    //check edges 
    if(px < 0){ 
    px = w-1; 
    } 
    if(px > w){ 
    px = 0; 
    } 
    if(py < 0){ 
    py = h-1; 
    } 
    if(py > h){ 
    py = 0; 
    } 
    //check collision 
    if(keyPressed){ 
    if(keyCode == UP || keyCode == DOWN || keyCode == LEFT || keyCode == RIGHT){ 
     checkSelfIntersection(); 
    } 
    } 
    //paint cursor 
    buffer.set(px,py,color(0,192,0)); 

    //render on screen 
    image(buffer,0,0,width,height); 
} 
void checkSelfIntersection(){ 
    //if the pixel ahead is not the same colour as the background 
    if(buffer.get(px+vx,py+vy) > color(0)){ 
    clear(); 
    println("Cycle go BOOM!"); 
    } 
} 
void keyPressed(){ 
    if(keyCode == UP){ 
    vy = -1; 
    } 
    if(keyCode == DOWN){ 
    vy = +1; 
    } 
    if(keyCode == LEFT){ 
    vx = -1; 
    } 
    if(keyCode == RIGHT){ 
    vx = +1; 
    } 
} 
void keyReleased(){ 
    vx = vy = 0; 
} 

类似的概念可以跟踪点的列表,并检查可以做,如果一个新的起点已在该列表(碰撞)的一部分,或者不是:

ArrayList<PVector> path = new ArrayList<PVector>(); 


//cursor position 
int px,py; 
//cursor velocity; 
int vx,vy; 

void setup(){ 
    size(400,400); 
    noFill(); 
    strokeWeight(10); 
} 
void draw(){ 
    //update cursor 
    px += vx; 
    py += vy; 
    //check edges 
    if(px < 0){ 
    px = 0; 
    } 
    if(px > width){ 
    px = width; 
    } 
    if(py < 0){ 
    py = 0; 
    } 
    if(py > height){ 
    py = height; 
    } 
    //check collision 
    if(keyPressed){ 
    if(keyCode == UP || keyCode == DOWN || keyCode == LEFT || keyCode == RIGHT){ 
     checkSelfIntersection(); 
    } 
    } 

    background(255); 
    beginShape(); 
    for(int i = 0 ; i < path.size(); i++){ 
    PVector p = path.get(i); 
    vertex(p.x,p.y); 
    } 
    endShape(); 
} 
void checkSelfIntersection(){ 
    PVector cursor = new PVector(px,py); 
    if(path.contains(cursor)){ 
    path.clear(); 
    println("Cycle go BOOM!"); 
    }else{ 
    path.add(cursor); 
    } 
} 
void keyPressed(){ 
    if(keyCode == UP){ 
    vy = -5; 
    } 
    if(keyCode == DOWN){ 
    vy = +5; 
    } 
    if(keyCode == LEFT){ 
    vx = -5; 
    } 
    if(keyCode == RIGHT){ 
    vx = +5; 
    } 
} 
void keyReleased(){ 
    vx = vy = 0; 
} 

的概念不是从如何游戏,如蛇/异形大作战的/ etc检查自相交

不同。

注意我在小键盘上更新键盘上的“光标”会导致作弊:这样可以避免线条间的空隙。如果您尝试用鼠标替换,您会注意到,如果鼠标移动速度较快,碰撞检查可能会失败,因为它会根据记录点列表检查一个点。另一种方法可能是将点列表分成两行,并检查新点是否与其中的任何点相交。您可能还想检查此similar question

-2

堆栈溢出不是真的为一般的“我该怎么做”类型的问题而设计的。这是针对具体的“我试过X,预计Y,但得到Z”类型的问题。这就是说,我会尽力在一般意义上提供帮助。

您可能只需跟踪循环所形成的线条,其形式为球员转身的所有积分的ArrayList。然后在每一步中,您都可以检查玩家是否与其中任何一条线相交。

更具体地说,您可能想要在上一个玩家坐标和下一个玩家坐标之间形成另一条线。然后检查这条线是否与其他线路相交,使用公式,我相信您可以通过谷歌搜索或两个公式找到。

除非你在谈论非常大的游戏场(如在数百万行),否则你可能不需要做任何比这更聪明的事情。因此,询问效率有点早。

当然有许多其他方法来解决这个问题。您也可以使用跟踪路径的二维数组,也可以使用基于像素的碰撞,或者使用任意数量的其他解决方案。关键是你需要尝试一些东西,并发布一个MCVE以及一个特定的问题,如果你卡住了,我们会从那里开始。祝你好运。