javascript - Experiencing something odd when using THREE.Raycaster for collision detection (r68) -
i've been using three.raycaster
test collisions many things in game engine far, it's great , works well.
however, i've run quite peculiar cannot seem figure out. point of view, logic , code sound expected result not correct. perhaps i'm missing obvious thought i'd ask help.
i casting rays out center of top of group of meshes, 1 one, in circular arc. meshes children of parent object3d
, goal test collisions between origin mesh , other meshes children of parent. test rays, using three.arrowhelper
.
here's image of result of code - http://imgur.com/ipzyusa
in image, arrowhelper
objects positioned (origin:direction) how want them. yeah, there's wrong picture, code produces is:
var degree = math.pi / 16, tiles = this.tilescontainer.children, tilesnum = tiles.length, raycaster = new three.raycaster(), raydirections, raydirectionsnum, rayorigin, raydirection, collisions, tile, i, j, k; (i = 0; < tilesnum; i++) { tile = tiles[i]; rayorigin = new three.vector3( tile.position.x, tile.geometry.boundingbox.max.y, tile.position.z ); raydirections = []; (j = 0; j < math.pi * 2; j += degree) { raydirections.push(new three.vector3(math.sin(j), 0, math.cos(j)).normalize()); } raydirectionsnum = raydirections.length; (k = 0; k < raydirectionsnum; k++) { raydirection = raydirections[k]; raycaster.set(rayorigin, raydirection); collisions = raycaster.intersectobjects(tiles); this.testray(rayorigin, raydirection, collisions); } }
the testray
method looks this:
testray: function (origin, direction, collisions) { var arrowhelper = new three.arrowhelper( direction, origin, 1, (collisions.length === 0) ? 0xff0000 : 0x0000ff ); this.scene.add(arrowhelper); }
now, obviously, off image. rays collide other meshes should blue, while not collide should red. it's clear image totally out of whack, , when inspect collisions, off results. lot of rays appear blue in image, i'm getting huge number of collisions, 30 collisions single ray sometimes, nothing others when right next other tiles.
i can't figure out might be. how can many rays should blue red? , how can rays tiles @ edge of level have blue collisions tiles not exist?
really scratching head (read: bashing head repeatedly) on one, super appreciated!
the solution outside code , not, @ least don't believe, related outdated r68
build.
when making tile meshes, needed set three properties on them
tilemesh.matrixautoupdate = false; tilemesh.updatematrix(); tilemesh.updatematrixworld(); // new
i doing first two, not last one. why necessary, not know, seems little odd me fixed problem. had axishelper
in scene, if @ original image, you'll notice arrowhelper
objects blue pointing towards axishelper
. weird because axishelper
added scene, not tilescontainer
. adding arrowhelper
objects tilescontainer
did not help.
the process render scene had raycaster code run before axishelper
added scene , before initial render happened. problem fixed if moved raycaster code call after axishelper
added, hacky solution.
so true fix add .updatematrixworld()
tiles. result looks http://imgur.com/8lewqxl, correct (the arrowhelper
objects have been shortened in length don't overlap).
big manthrax on one.
Comments
Post a Comment