简体   繁体   中英

Circle to Circle collision function Javascript

I wrote a circle to circle collision function but I think I made a little mistake and I can't see to find it.

 function testCollisionArc(arc1,arc2){ return arc1.x + arc1.r + arc2.r > arc2.x && arc1.x < arc2.x + arc1.r + arc2.r && arc1.y + arc1.r + arc2.r > arc2.y && arc1.y < arc2.y + arc1.r + arc2.r }

A circle hasnt edges and borders ( :0 ). You need to get the distance between both:

var distance=Math.sqrt(Math.pow(arc1.x-arc2.x,2)+Math.pow(arc1.y-arc2.y,2));//the 'sentence of Pythagoras' as we say in German
return arc1.r+arc2.r>distance;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM