繁体   English   中英

等距3d碰撞检测

[英]Isometric 3d collision detection

我正在为游戏制作等轴测图,可以绘制它,但是我不知道如何在没有Threejs或其他3d库的情况下实现某种3d碰撞检测。

有可能的? 也许让一个对象块可以帮助? 我已经搜索过,但只找到了库。

这是我的JavaScript代码:

 var canvas = document.getElementById('canvas'), ctx = canvas.getContext('2d'), width = canvas.width = window.innerWidth, height = canvas.height = window.innerHeight, stop = false; var tw, th; var player; setup(); draw(); function setup(){ ctx.translate(width/2,50); tw = 60; //tile width th = 30; // tile height player = new Player(2,3,3); }; function draw(){ ctx.clearRect(-width/2,-50,width*1.5,height+50); for(var i = 0; i < 5; i++){ for(var j = 0; j < 5; j++){ drawBlock(i,j,1,tw,th); } } if(!stop){ requestAnimationFrame(draw); } } function drawBlock(x,y,z,w,h){ var top = "#eeeeee", right = '#cccccc', left = '#999999'; ctx.save(); ctx.translate((xy)*w/2,(x+y)*h/2); ctx.beginPath(); ctx.moveTo(0,-z*h); ctx.lineTo(w/2,h/2-z*h); ctx.lineTo(0,hz*h); ctx.lineTo(-w/2,h/2-z*h); ctx.closePath(); ctx.fillStyle = "black"; ctx.stroke(); ctx.fillStyle = top; ctx.fill(); ctx.beginPath(); ctx.moveTo(-w/2,h/2-z*h); ctx.lineTo(0,hz*h); ctx.lineTo(0,h); ctx.lineTo(0,h); ctx.lineTo(-w/2,h/2); ctx.closePath(); ctx.fillStyle = "black"; ctx.stroke(); ctx.fillStyle = left; ctx.fill(); ctx.beginPath(); ctx.moveTo(w/2,h/2-z*h); ctx.lineTo(0,hz*h); ctx.lineTo(0,h); ctx.lineTo(0,h); ctx.lineTo(w/2,h/2); ctx.closePath(); ctx.fillStyle = "black"; ctx.stroke(); ctx.fillStyle = right; ctx.fill(); ctx.restore(); } function drawTile(x,y,stroke,col){ ctx.save(); ctx.translate((xy)*tw/2,(x+y)*th/2); ctx.beginPath(); ctx.moveTo(0,0); ctx.lineTo(tw/2,th/2); ctx.lineTo(0,th); ctx.lineTo(-tw/2,th/2); ctx.closePath(); if(stroke){ ctx.stroke(); }else{ ctx.fillStyle = col; ctx.fill(); } ctx.restore(); } function Player(x,y,z){ this.x = x; this.y = y; this.z = z; this.w = 10; //width this.h = 10; //height } 
 canvas{ width:100%; height:100%; } 
 <canvas id="canvas"></canvas> 

您追求的是立方体碰撞。 我得到的第一个搜索结果是有关MDN的名为3D碰撞检测的文章:

在数学上,这看起来像这样:

 f(A,B) = (AminX <= BmaxX ∧ AmaxX >= BminX) ∧ (AminY <= BmaxY ∧ AmaxY >= BminY) ∧ (AminZ <= BmaxZ ∧ AmaxZ >= BminZ) 

在JavaScript中,我们将使用以下代码:

 function intersect(a, b) { return (a.minX <= b.maxX && a.maxX >= b.minX) && (a.minY <= b.maxY && a.maxY >= b.minY) && (a.minZ <= b.maxZ && a.maxZ >= b.minZ); } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM