2012-11-19 146 views
6

我試圖用光線投射繪製一條線。基本上我想設置一些從我的「玩家」對象出來的線路。Three.js光線投射碰撞檢測

(像這樣:https://gamedev.stackexchange.com/questions/35013/how-to-handle-3d-collisions-using-raycasting-with-a-reflection-vector

我想要這個,所以我就可以使用我可以看到我的碰撞檢測直觀。

我知道我可以使用不同的方式來做碰撞檢測,但我用這種方式作爲學習檢測。

我的問題是下面的代碼繪製一條線,但它似乎隨機更改長度,並不總是指向相同的角度。

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);   

讚賞任何幫助。

回答

1

我正在嘗試在看到該模型後做同樣的事情..因爲我試圖以同樣的方式做到這一點,並不能解決它,我會提供一個替代方案。

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); 
    } 

} 

所以現在你有一條線從你的網格中射出來,進入最接近的相交點。

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