您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關利用javascript實現多邊形碰撞檢測,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
javascript多邊形碰撞檢測
原理就是 循環每個頂點判斷是不是在多邊形內
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; //交點個數 for( var i = 0; i < count; i++ ) { var p1 = posPolygon[i]; var p2 = posPolygon[(i + 1) % count]; //下一個節點 // p1p2這條邊與水平線平行 if( p1.y == p2.y ) continue; // 交點在p1p2的延長線上 if( pos.y < Math.min( p1.y, p2.y ) ) continue; // 交點在p1p2的延長線上 if( pos.y > Math.max( p1.y, p2.y ) ) continue; // 計算交點 X 左邊 : (p2.y - p1.y)/(p2.x - p1.x) = (y - p1.y)/(x - p1.x) // 直線 K 值相等, 交點y = pos.y let x = (pos.y - p1.y) * (p2.x - p1.x) / (p2.y - p1.y) + p1.x // 只統計單邊交點,即點的正向方向 if(x > pos.x) cross ++; } return cross % 2 == 1; }
關于利用javascript實現多邊形碰撞檢測就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。