色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

javascript實現多邊形碰撞檢測

瀏覽:91日期:2023-10-10 10:15:42

javascript多邊形碰撞檢測

原理就是 循環每個頂點判斷是不是在多邊形內

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; //交點個數 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實現多邊形碰撞檢測的詳細內容,更多關于javascript多邊形碰撞檢測的資料請關注好吧啦網其它相關文章!

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 99久久免费观看 | 成人午夜大片免费看爽爽爽 | 欧美级| 韩国三级 mp4 | 日韩欧一级毛片在线播无遮挡 | 国产成人精品视频 | 国产亚洲精品一区999 | 福利网址在线 | 成人国产精品高清在线观看 | 亚洲在线免费 | 久久午夜鲁丝片午夜精品 | 99久久精品国产一区二区 | 岛国片欧美一级毛片 | 夜色成人性y| 久久精品国产欧美日韩亚洲 | 日本黄大片影院一区二区 | 国产欧美亚洲精品一区 | 免费一级片网站 | 欧美在线一级毛片视频 | 国产精品三级在线观看 | 日韩美女一区二区三区 | 最新99国产成人精品视频免费 | 怡红院免费播放全部视频 | 免费特黄视频 | 免费高清在线爱做视频 | 国产成人亚洲精品无广告 | 成人99国产精品一级毛片 | 日本精品久久 | 亚洲国产精品一区二区久久 | 久久99国产精品久久99果冻传媒 | 成人伊人青草久久综合网 | 成人午夜在线观看国产 | 国产一级片在线 | 九九成人免费视频 | 国产东北色老头老太性视频 | 国产精品美女一区二区三区 | 手机看片日韩国产一区二区 | 日韩毛片免费在线观看 | 鲁老汉精品视频在线观看 | 久久精品免费一区二区视 | 高清免费国产在线观看 |