javascript实现多边形碰撞检测
程序员文章站
2022-06-22 09:42:55
javascript多边形碰撞检测原理就是 循环每个顶点判断是不是在多边形内const app = new pixi.application({ antialias: true });document...
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多边形碰撞检测的资料请关注其它相关文章!
上一篇: iOS-国际化脚本工具