cocos2d-x JS 各類點、圓、矩形之間的簡單碰撞檢測


這里總結了一下點、圓、矩形之間的簡單碰撞檢測算法

(ps:矩形不包括旋轉狀態)

 

點和圓的碰撞檢測:

1.計算點和圓心的距離

2.判斷點與圓心的距離是否小於圓的半

 

 
        
[javascript]  view plain  copy
 
  1. isCollision: function(point, circle)  
  2. {  
  3.         //點與圓心的距離  
  4.         var distance = Math.sqrt(Math.pow(point.x - circle.x, 2) + Math.pow(point.y - circle.y, 2));  
  5.   
  6.         //圓的半徑  
  7.         var radius = circle.getContentSize().width / 2;  
  8.   
  9.         //如果點與圓心距離小於圓的半徑,返回true  
  10.         if(radius > distance)  
  11.         {  
  12.             return true;  
  13.         }  
  14.         else  
  15.         {  
  16.             return false;  
  17.         }  
  18. }  

 

點和矩形的碰撞檢測:
1.獲得矩形的左上角坐標p1和右下角坐標p2
2.判斷點p的x坐標是否大於p1的x坐標,並且小於p2的x坐標,並且p的y坐標大於p2的y坐標,並且小於p2

 

[javascript]  view plain  copy
 
  1. isCollision: function(point, rect)  
  2. {  
  3.         //獲得矩形的左上角坐標p1和右下角坐標p2  
  4.         var p1 = cc.p(rect.x - rect.width/2, rect.y + rect.height/2);  
  5.         var p2 = cc.p(rect.x + rect.width/2, rect.y - rect.height/2);  
  6.   
  7.         //判斷點p的x坐標是否大於p1的x坐標,並且小於p2的x坐標,並且p的y坐標大於p2的y坐標,並且小於p2的y坐標  
  8.         if(point.x > p1.x && point.x < p2.x && point.y > p2.y && point.y < p1.y)  
  9.         {  
  10.             return true;  
  11.         }  
  12.         else  
  13.         {  
  14.             return false;  
  15.         }  
  16. }  

 

 

圓和圓的碰撞檢測:
1.計算兩圓心之間的距離
2.判斷兩圓心之間的距離是否小於兩圓的半徑之和

 

[javascript]  view plain  copy
 
  1. isCollision: function(circle1, circle2)  
  2. {  
  3.         //圓心與圓心的距離  
  4.         var distance = Math.sqrt(Math.pow(circle1.x - circle2.x, 2) + Math.pow(circle1.y - circle2.y, 2));  
  5.   
  6.         //圓心半徑  
  7.         var r1 = circle1.getContentSize().width / 2;  
  8.         var r2 = circle2.getContentSize().width / 2;  
  9.   
  10.         //如果圓心與圓心距離小於兩圓的半徑,返回true  
  11.         if(r1 + r2 > distance)  
  12.         {  
  13.             return true;  
  14.         }  
  15.         else  
  16.         {  
  17.             return false;  
  18.         }  
  19. }  

 

 

矩形和矩形的碰撞檢測:

在水平方向上,判斷兩個矩形中點x坐標的距離是否小於兩個矩形寬度一半之和

在垂直方向上,判斷兩個矩形中點y坐標的距離是否小於兩個矩形高度一半之和

 

[javascript]  view plain  copy
 
  1. isCollision: function(rect1, rect2)  
  2. {  
  3.         //獲取矩形1的寬高  
  4.         var width1 = rect1.width;  
  5.         var height1 = rect1.height;  
  6.   
  7.         //獲取矩形2的寬高  
  8.         var width2 = rect2.width;  
  9.         var height2 = rect2.height;  
  10.   
  11.         var flag;  
  12.         if(rect1.x >= rect2.x && rect2.x <= rect1.x - width1/2 - width2/2)  
  13.         {  
  14.             flag = false;  
  15.         }  
  16.         else if(rect1.x <= rect2.x && rect2.x >= rect1.x + width1/2 + width2/2)  
  17.         {  
  18.             flag = false;  
  19.         }  
  20.         else if(rect1.y >= rect2.y && rect2.y <= rect1.y - height1/2 - height2/2)  
  21.         {  
  22.             flag = false;  
  23.         }  
  24.         else if(rect1.y <= rect2.y && rect2.y >= rect1.y + height1/2 + height2/2)  
  25.         {  
  26.             flag = false;  
  27.         }  
  28.         else  
  29.         {  
  30.             flag = true;  
  31.         }  
  32. }  

 

 

圓和矩形的碰撞檢測:

 

[javascript]  view plain  copy
 
  1. isCollision: function(circle, rect)  
  2. {  
  3.         //圓的半徑  
  4.         var radius = circle.width / 2;  
  5.   
  6.         //圓形中心與矩形中心的相對坐標  
  7.         var x = circle.x - rect.x;  
  8.         var y = circle.y - rect.y;  
  9.   
  10.         var minX = Math.min(x, rect.width/2);  
  11.         var maxX = Math.max(minX, -rect.width/2);  
  12.         var minY = Math.min(y, rect.height/2);  
  13.         var maxY = Math.max(minY, -rect.height/2);  
  14.   
  15.         if((maxX - x) * (maxX - x) + (maxY - y) * (maxY - y) <= radius * radius)  
  16.         {  
  17.             return true;  
  18.         }  
  19.         else  
  20.         {  
  21.             return false;  
  22.         }  
  23. }  

 

源碼下載:點擊打開鏈接


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM