我有最后一个问题。在节点服务器中,我加载 .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() ,因为我认为没有必要。并且计算取代我的车没关系。
所以我想也许加载器没有正确加载网格,或者我不能像在客户端上那样检测碰撞。你怎么看?
谢谢。
要检测客户端上的冲突,我使用:
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); ...