javascript實(shí)現(xiàn)多邊形碰撞檢測(cè)
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 polygonconst 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;}
以上就是javascript實(shí)現(xiàn)多邊形碰撞檢測(cè)的詳細(xì)內(nèi)容,更多關(guān)于javascript多邊形碰撞檢測(cè)的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Ajax引擎 ajax請(qǐng)求步驟詳細(xì)代碼2. JS語法也可以有C#的switch表達(dá)式3. JSP實(shí)時(shí)顯示當(dāng)前系統(tǒng)時(shí)間的四種方式示例解析4. python 利用toapi庫(kù)自動(dòng)生成api5. 多級(jí)聯(lián)動(dòng)下拉選擇框,動(dòng)態(tài)獲取下一級(jí)6. 詳解Struts2中對(duì)未登錄jsp頁面實(shí)現(xiàn)攔截功能7. 老生常談.NET中的 COM 組件8. CSS3+Js實(shí)現(xiàn)響應(yīng)式導(dǎo)航條9. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)10. ASP.NET MVC前臺(tái)動(dòng)態(tài)添加文本框并在后臺(tái)使用FormCollection接收值
