我有最後一個問題。 在節點服務器中,我加載.json文件並放入場景以檢測衝突。
節點:
var loader = new THREE.JSONLoader();
fs.readFile(__dirname+'/public/js/essai/lobby.js', 'utf8', function (err, data) {
if (err) {
console.log('Error: ' + err);
return;
}
data = JSON.parse(data);
var model = loader.parse(data);
var mesh = new THREE.Mesh(model.geometry, new THREE.MeshBasicMaterial());
mesh.scale.set(40,40,40);
scene.add(mesh);
collisable.push(mesh);
});
爲了檢測碰撞:
var collisions, i,
// Maximum distance from the origin before we consider collision
distance = 32,
// Get the obstacles array from our world
obstacles = GameEngine.getInstance().collidableMeshList;//basicScene.world.getObstacles();
// For each ray
for (i = 0; i < this.rays.length; i += 1) {
// We reset the raycaster to this direction
this.caster.set(this.design.position, this.rays[i]);
// Test if we intersect with any obstacle mesh
collisions = this.caster.intersectObjects(obstacles);
// And disable that direction if we do
if (collisions.length > 0 && collisions[0].distance <= distance) {
// Yep, this.rays[i] gives us : 0 => up, 1 => up-left, 2 => left, ...
if ((i === 0 || i === 1 || i === 7) && this.direction.z === 1) {
console.log("collisions"); //
} else if ((i === 3 || i === 4 || i === 5) && this.direction.z === -1) {
console.log("collisions");
}
if ((i === 1 || i === 2 || i === 3) && this.direction.x === 1) {
console.log("collisions");
} else if ((i === 5 || i === 6 || i === 7) && this.direction.x === -1) {
console.log("collisions");
}
}
}
當我嘗試在客戶端上的碰撞,它的做工精細,但在服務器上,他不檢測衝突。 所以,我已經嘗試在我的客戶端加載json文件以查看加載是否正確。
當我加載像這樣在我的客戶,現場確定:
loader.load("essai/lobby.js", function(geometry) {
mesh = new THREE.Mesh(geometry, new THREE.MeshNormalMaterial());
mesh.scale.set(40, 40, 40);
scene.add(mesh);
animate();
});
當我加載像這樣的客戶,這是不正常:
$.getJSON("essai/lobby.js", function(data) {
var model = loader.parse(data);
var mesh = new THREE.Mesh(model.geometry, new THREE.MeshBasicMaterial());
mesh.scale.set(40,40,40);
scene.add(mesh);
animate();
});
沒有錯誤,但沒有出現。
所以我認爲是相同的服務器端,爲什麼碰撞永遠不會被檢測到。
在服務器端,我不使用animate(),因爲我認爲這不是必需的。 和計算器取代我的車沒關係。
所以我想也許加載程序不能正確加載網格,或者我無法在客戶端上進行檢測衝突。 你覺得呢?
Thx。
爲了檢測在客戶端碰撞我使用:
this.rays = [
new THREE.Vector3(0, 0, 1), //up
new THREE.Vector3(1, 0, 1), //up left
new THREE.Vector3(1, 0, 0), //left
new THREE.Vector3(1, 0, -1), // down left
new THREE.Vector3(0, 0, -1), //down
new THREE.Vector3(-1, 0, -1), // down right
new THREE.Vector3(-1, 0, 0), //rigth
new THREE.Vector3(-1, 0, 1) //up right
];
this.caster = new THREE.Raycaster();
this.direction = new THREE.Vector3(0, 0, 0);
當我了,我設定的方向:this.direction.set(0,0,1); 當我倒下時,我設置了方向:this.direction.set(0,0,-1); ...
你可能不想做的一件事是聲明一個名爲'JSON'的變量:http://stackoverflow.com/questions/4857072/is-the-json-object-global-in- node-js – fakewaffle