Three.js Ray Casting for collision detection

I am trying to draw a line using beam casting. Basically, I want to tweak some lines coming from my player object in all directions.

(e.g .: https://gamedev.stackexchange.com/questions/35013/how-to-handle-3d-collisions-using-raycasting-with-a-reflection-vector )

I want to use it later. Visually I see my collision detection.

I know that I can use different collision detection methods, but I use this method as a training detection.

My problem is that the code below draws a line, but it seems to randomly change the length and does not always indicate the same angle.

var ray = new THREE.Ray( player.model.objects.position, new THREE.Vector3(1, 1, 1));

 var geometry = new THREE.Geometry(); // my issue is here. I don't think this is the right way use a ray to workout the second vector? // EDIT: Realized this should be set at player position and outwards. //var newx = 300 * ray.direction.x; //var newz = 300 * ray.direction.z; // EDIT CODE UPDATE var newx = (player.model.objects.position.x) + (60 * ray.direction.x); var newz = (player.model.objects.position.z) + (60 * ray.direction.z); // THREE.Vector3 {x: 1310.1526178356803, y: 0, z: 1290.8237947033065} console.log(player.model.objects.position); geometry.vertices.push( player.model.objects.position); geometry.vertices.push( new THREE.Vector3(newx, player.model.objects.position.y, newz)); var line = new THREE.Line(geometry, material); scene.add(line); 

code>

Any help appreciated.

+6
source share
1 answer

I tried to do the same when I saw this model. Since I tried to do it the same way and could not understand, I will offer an alternative.

 var line; function update() { // Z- DIRECTION raycaster.ray.direction.set(0, 0, -1); var geometry = new THREE.Geometry(); intersections = raycaster.intersectObjects( objects ); if ( intersections.length > 0 ) { var geometry = new THREE.Geometry(); // POSITION OF MESH TO SHOOT RAYS OUT OF geometry.vertices.push( obj.position ); geometry.vertices.push( intersections[0].point ); scene.remove(line); line = new THREE.Line(geometry, new THREE.LineBasicMaterial({color: 0x990000})); scene.add(line); } } 

So now you have a line firing from your grid at any nearest intersection.

https://dl.dropbox.com/u/42766757/guy.png

+1
source

All Articles