溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

利用javascript實(shí)現(xiàn)多邊形碰撞檢測(cè)

發(fā)布時(shí)間:2020-10-26 14:19:49 來(lái)源:億速云 閱讀:177 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)利用javascript實(shí)現(xiàn)多邊形碰撞檢測(cè),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

javascript多邊形碰撞檢測(cè)

原理就是 循環(huán)每個(gè)頂點(diǎn)判斷是不是在多邊形內(nèi)

const app = new PIXI.Application({ antialias: true });
document.body.appendChild(app.view);

const graphics = new PIXI.Graphics();


// draw polygon
const path = [600, 370, 700, 460, 780, 420, 730, 570, 590, 520];

graphics.lineStyle(0);
graphics.beginFill(0x3500FA, 1);
graphics.drawPolygon(path);
graphics.endFill();

app.stage.addChild(graphics);

 var xuanzhuan = PIXI.Sprite.from('/moban/images/share.jpg');
 xuanzhuan.width=120;
 xuanzhuan.height=120;
 xuanzhuan.x=13;
 xuanzhuan.y=33;
  app.stage.addChild(xuanzhuan);


  xuanzhuan.interactive = true;

  xuanzhuan.buttonMode = true;
   xuanzhuan
    .on('pointerdown', onDragStart)
    .on('pointerup', onDragEnd)
    .on('pointerupoutside', onDragEnd)
    .on('pointermove', onDragMove);

  function onDragStart(event) {
  // store a reference to the data
  // the reason for this is because of multitouch
  // we want to track the movement of this particular touch
  this.data = event.data;
  this.alpha = 0.5;
  this.dragging = true;
}

function onDragEnd() {
  this.alpha = 1;
  this.dragging = false;
  // set the interaction data to null
  this.data = null;
}
   var  posPolygon=[];
     var dianlist={};
    dianlist['x']=600;
    dianlist['y']=370; 
    posPolygon.push(dianlist)
  var dianlist={};
    dianlist['x']=700;
    dianlist['y']=460; 
    posPolygon.push(dianlist)  
  var dianlist={};
    dianlist['x']=780;
    dianlist['y']=420; 
    posPolygon.push(dianlist)   
  var dianlist={};
    dianlist['x']=730;
    dianlist['y']=570; 
   posPolygon.push(dianlist) 
  var dianlist={};
    dianlist['x']=590;
    dianlist['y']=520; 
    posPolygon.push(dianlist)
function onDragMove() {
  if (this.dragging) {
    const newPosition = this.data.getLocalPosition(this.parent);
    this.x = newPosition.x;
    this.y = newPosition.y;

    var baoweihe=this.getBounds();
    var youxiajiaox=baoweihe.x+baoweihe.width;
    var youxiajiaoy=baoweihe.y+baoweihe.height;

    var poslist=[];
     var pos={};
    pos['x']=baoweihe.x;
    pos['y']=baoweihe.y;    
    poslist.push(pos);      

  var pos={};
    pos['x']=youxiajiaox;
    pos['y']=baoweihe.y;    
    poslist.push(pos);      
    var pos={};
    pos['x']=youxiajiaox;
    pos['y']=youxiajiaoy;    
    poslist.push(pos);

    var pos={};
    pos['x']=baoweihe.x;
    pos['y']=youxiajiaoy;    
    poslist.push(pos);    

     
  
    var ispengzhuang=PolygonInPolygon(poslist, posPolygon,5);
    if(ispengzhuang){
      alert('碰撞了');
    }


  }
}

function PolygonInPolygon(posPolygonA, posPolygonB, count){
  console.log(posPolygonA);
    var count1=posPolygonA.length;
   for(var i=0;i<count1;i++ ){
    var pos=posPolygonA[i];
      console.log(pos);
     var ispengzhuang=PointInPolygon( pos, posPolygonB, count);
     if(ispengzhuang){
      alert('碰撞了')
     }
   }
}

function PointInPolygon( pos, posPolygonB, count)
{
  var cross = 0; //交點(diǎn)個(gè)數(shù)
    
  for( var i = 0; i < count; i++ )
  {
    var p1 = posPolygon[i];
    var p2 = posPolygon[(i + 1) % count]; //下一個(gè)節(jié)點(diǎn)
 
    // p1p2這條邊與水平線平行
    if( p1.y == p2.y )
      continue;
 
    // 交點(diǎn)在p1p2的延長(zhǎng)線上
    if( pos.y < Math.min( p1.y, p2.y ) )
      continue;
 
    // 交點(diǎn)在p1p2的延長(zhǎng)線上
    if( pos.y > Math.max( p1.y, p2.y ) )
      continue;
      
    // 計(jì)算交點(diǎn) X 左邊 : (p2.y - p1.y)/(p2.x - p1.x) = (y - p1.y)/(x - p1.x)
    // 直線 K 值相等, 交點(diǎn)y = pos.y
    let x = (pos.y - p1.y) * (p2.x - p1.x) / (p2.y - p1.y) + p1.x
    // 只統(tǒng)計(jì)單邊交點(diǎn),即點(diǎn)的正向方向
    if(x > pos.x)
      cross ++;
  }
 
  return cross % 2 == 1;
}

關(guān)于利用javascript實(shí)現(xiàn)多邊形碰撞檢測(cè)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI