javascript實現(xiàn)多邊形碰撞檢測
更新時間:2020年10月24日 16:09:44 作者:newmiracle宇宙
這篇文章主要介紹了javascript如何實現(xiàn)多邊形碰撞檢測,幫助大家更好的理解和使用js,感興趣的朋友可以了解下
javascript多邊形碰撞檢測
原理就是 循環(huá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; //交點個數(shù) for( var i = 0; i < count; i++ ) { var p1 = posPolygon[i]; var p2 = posPolygon[(i + 1) % count]; //下一個節(jié)點 // 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 // 只統(tǒng)計單邊交點,即點的正向方向 if(x > pos.x) cross ++; } return cross % 2 == 1; }
以上就是javascript實現(xiàn)多邊形碰撞檢測的詳細(xì)內(nèi)容,更多關(guān)于javascript多邊形碰撞檢測的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
淺聊一下Javascript中的數(shù)據(jù)類型和類型轉(zhuǎn)換
在JavaScript中,理解數(shù)據(jù)類型,如何區(qū)分它們,以及它們?nèi)绾伪晦D(zhuǎn)換是至關(guān)重要的,在這篇文章中,我們將探討這些主題,以幫助大家鞏固JavaScript基礎(chǔ)2023-08-08一文詳細(xì)分析前端請求中的“Unsupported?Media?Type”問題
在Web開發(fā)中,前后端交互頻繁遇到HTTP狀態(tài)碼415錯誤,這表明服務(wù)器無法處理因Content-Type不匹配的請求,常見于POST或PUT請求,必須確保請求頭中的Content-Type與服務(wù)器期望的一致,跨域請求中,需要的朋友可以參考下2024-10-10JavaScript通過select動態(tài)更換圖片的方法
這篇文章主要介紹了JavaScript通過select動態(tài)更換圖片的方法,涉及javascript動態(tài)操作圖片src的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03JavaScript實現(xiàn)動態(tài)數(shù)據(jù)可視化的示例詳解
動態(tài)數(shù)據(jù)可視化能夠?qū)⒋罅繑?shù)據(jù)以直觀、生動的方式呈現(xiàn),幫助用戶更好地理解和分析數(shù)據(jù),本文主要為大家介紹了如何使用JavaScript實現(xiàn)這一功能,需要的可以參考下2024-02-02