处理THREE.js中的碰撞

时间:2015-08-09 05:40:27

标签: javascript three.js

嘿伙计们,我一直在利用这个很棒的图书馆three.js

开展我的一个小项目

现在我一直在使用https://github.com/stemkoski/stemkoski.github.com/blob/master/Three.js/Collision-Detection.html的示例来处理碰撞检测,当一个对象超过使用voxel's的另一个对象时更是如此。

要引用我的问题,我使用的是http://threejs.org/examples/#canvas_interactive_voxelpainter示例。

无论如何要继续,当我在屏幕上呈现voxel时,多维数据集上方的任何内容都允许我在voxel below volex范围内渲染另一个stemkoski内容我不会让我渲染:

Underneath volex within certain radius will not render

以上显示的是立方体: enter image description here

现在,这是我使用 checkOverlapObject: function(voxel) // THIS IS USED TO SEE IF WE ARE OVER LAPPING ANY OBJECTS { var originPoint = voxel.position.clone(); var collidableObjs = this.rooms; for (var vertexIndex = 0; vertexIndex < voxel.geometry.vertices.length; vertexIndex++) { var localVertex = voxel.geometry.vertices[vertexIndex].clone(); console.log(localVertex); var globalVertex = localVertex.applyMatrix4( voxel.matrix ); console.log(globalVertex); var directionVector = globalVertex.sub( voxel.position ); console.log(directionVector); console.log(originPoint); console.log(directionVector.clone().normalize()); if(collidableObjs.length > 0) { var ray = new THREE.Raycaster( originPoint, directionVector.clone().normalize() ); var collisionResults = ray.intersectObjects( collidableObjs ); if ( collisionResults.length > 0 && collisionResults[0].distance < directionVector.length() ) { console.log(collisionResults); console.log(collisionResults[0].distance); console.log( directionVector.length() ); return false } } } return true; }, 提供的示例整理的简洁小功能:

volex

现在发生的事情是,在实际添加呈现的volex之前,用户可以预览他们是否有权添加volex所以我们传递的 var voxel = new THREE.Mesh( this.room.cubeGeometry, this.room.cubeTmpHoverMaterial ); voxel.geometry.computeBoundingBox(); voxel.position.copy( intersect.point ).add( intersect.face.normal ); voxel.position.divideScalar( 50 ).floor().multiplyScalar( 50 ).addScalar( 25 ); voxel.material.color.setHex(this.colorTmpHover); 由:< / p>

checkOverlapObject

进入我们的screen/grid函数,查看对象是否与已渲染到little neat上的对象重叠

现在遵循我所做的console.log函数,我已将T…E.Vector3 {x: 25, y: 25, z: 25} <!-- our localVertex T…E.Vector3 {x: 25, y: 25, z: 25} <!-- our globalVertex T…E.Vector3 {x: 0, y: 0, z: -350} <!-- our directionVector T…E.Vector3 {x: 25, y: 25, z: 375} <!-- our originPoint T…E.Vector3 {x: 0, y: 0, z: -1} <!-- our directionVector.clone().normalize() [Object, Object] <!-- our collisionResults 225 <!-- our collisionResults[0].distance 350 <!-- our directionVector.length() 放在输出参数上,这里有一些输出:

volex

此数据基于第一张图片。

请理解我还有其他2 blocks on the grid占用2 blocks on the grid或更多。所以原因是,我有一个位置的中心位置但我需要考虑对象的其余部分,如果它占用already rendered volex以检查是否与size_t CurlWriteCallback(char* buf, size_t size, size_t nmemb, void* up) { TRACE("CURL - Response received:\n%s", buf); TRACE("CURL - Response handled %d bytes:\n%s", size*nmemb); // tell curl how many bytes we handled return size*nmemb; } // ... CURL *curl = curl_easy_init(); if(curl) { CURLcode res; curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &CurlWriteCallback); curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/"); res = curl_easy_perform(curl); curl_easy_cleanup(curl); } 我重叠#如果他们互相接触,就要小心。

关于可能出现什么问题的任何建议或想法?

1 个答案:

答案 0 :(得分:0)

如上所述,我终于找到了解决方案。我为对象collision/overlap创建了一个自定义函数。

首先,上面显示的上述collision代码有什么问题,它处理raycasterdirection的{​​{1}}开始 - 直线拍摄在其object对面,看看是否有任何与orgin接触的内容。现在,如果ray的任何部分正在触及另一个overlap/collision

,那么我们有object时,这很好,但不是很充分

正如我们的object

As described with our rays

所以这是我的解决方案。

当我们在飞机上有一个物体时,我们知道它的一切,rays的坐标 - 在这个例子中我没有处理x,y,z所以忽略它。

我们关心的是对象y和对象width = x。所以,实质上当我们检查一个对象是否是depth/length = z另一个对象时,我们只需要处理overlapping的{​​{1}}和我们已经min x/z and max x/z to be added object >

我们如何做到这一点,循环我们的对象以rendered obj循环添加starting min z position。此起始for位置包含我们的min z。这为我们position z + min z of object提供了准确的z位置。

我们继续使用plane或我们的对象添加width并使用starting min x position循环。不要忘记我们for为我们提供了x

的确切位置

现在原因很简单。我们想从我们要添加的对象plane开始。我们position 0,0 - length/z一直向上递增increment,同时执行此处理我们width/x上已呈现的每个对象,并检查是否有任何对象确切点与我们匹配。

以下是实际方法:

plane

以下是我们的结果:

result of function above

这确实提供了一个确切的答案,如果你的物体触及它不应该接触的东西,因为它处理的点到点。

我希望这有帮助!请随时使用。

另请注意,如果您不小心使用此方法,这可能会影响 checkOverlapObject: function(voxel) // THIS IS USED TO SEE IF WE ARE OVER LAPPING ANY OBJECTS { var collidableObjs = this.rooms; //lets get our voxel min and max vars var voxelMX = voxel.geometry.boundingBox.max.x; var voxelMZ = voxel.geometry.boundingBox.max.z; var voxelmX = voxel.geometry.boundingBox.min.x; var voxelmZ = voxel.geometry.boundingBox.min.z; // we need to get our voxel position ad to do some math to see if voxel min and max do not go beyound our boundries for our plane var voxelPX = voxel.position.x; var voxelPZ = voxel.position.z; var totalPositionVoxelminZ = (voxelPZ + voxelmZ); var totalPositionVoxelminX = (voxelPX + voxelmX); var totalPositionVoxelMAXZ = (voxelPZ + voxelMZ); var totalPositionVoxelMAXX = (voxelPX + voxelMX); // start loop for object to add Z cordinate for(var length = totalPositionVoxelminZ; length < totalPositionVoxelMAXZ; length++) { // start loop for object to add X cordinate for(var width = totalPositionVoxelminX; width < totalPositionVoxelMAXX; width++) { for(var i = 0; i < collidableObjs.length;i++) { //lets get our voxel min and max vars var thisvoxelMX = this.rooms[i].geometry.boundingBox.max.x; var thisvoxelMZ = this.rooms[i].geometry.boundingBox.max.z; var thisvoxelmX = this.rooms[i].geometry.boundingBox.min.x; var thisvoxelmZ = this.rooms[i].geometry.boundingBox.min.z; // we need to get our voxel position ad to do some math to see if voxel min and max do not go beyound our boundries for our plane var thisvoxelPX = this.rooms[i].position.x; var thisvoxelPZ = this.rooms[i].position.z; var thistotalPositionVoxelminZ = (thisvoxelPZ + thisvoxelmZ); var thistotalPositionVoxelminX = (thisvoxelPX + thisvoxelmX); var thistotalPositionVoxelMAXZ = (thisvoxelPZ + thisvoxelMZ); var thistotalPositionVoxelMAXX = (thisvoxelPX + thisvoxelMX); for(var insideZ = thistotalPositionVoxelminZ; insideZ < thistotalPositionVoxelMAXZ; insideZ++) { for(var insideX = thistotalPositionVoxelminX; insideX < thistotalPositionVoxelMAXX; insideX++) { if(insideZ == length && insideX == width) { return false; } } } } } } return true; } 的使用效果。我正在努力在有数百个对象要处理的情况下更好地优化它。因此,如果您有任何关于修改或添加现有代码的建议,以便更好地完成此任务并在数百个对象通过的情况下提供更好的性能,请随时提供详细信息!