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实现多边形碰撞检测的详细内容,更多关于javascript多边形碰撞检测的资料请关注其它相关文章!
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
更新日志
2024年11月25日
2024年11月25日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]