我正在尝试在javascript和html5画布中执行一些简单的碰撞。到目前为止,我有这样的:Javascript停止在画布中重复碰撞检测
checkCollision = (function(){
var l, i, ni,dis = 0;
var ob1,ob2 = {};
return function(){
//collisions is the array holding all the objects
l = collisions.length;
i = 0;
ni = 1;
while(i<l){
//grab the first object
ob1 = collisions[i];
while(ni<l){
//get the object to check against
ob2 = collisions[ni];
//find the distance between the two
dis = Math.sqrt(Math.pow((ob1.pos[0]-ob2.pos[0]),2)+Math.pow((ob1.pos[1]-ob2.pos[1]),2));
//rad is the radius
if(ob1.rad+ob2.rad >= dis){
console.log("collision")
}
//move forward second increment
ni++;
}
i++;
//keep one place ahead
ni=i+1;
}
};
})();
我这样做是没有任何形式的帮助,但现在我想我的大脑实在是太多了玉米粥推测这个最后一部分了。碰撞发生在每一帧,我不想要。当碰撞第一次发生时,我只想让它发射一次。我试着给每个对象一个碰撞变量,如果已经发生了碰撞,但它不能很好地工作。有些它会发生一次,有些会不断发射。 有没有人有任何指针?
顺便说一句,这只是简单的圆形碰撞 – Isaiah