我试图使一个物体追逐另一个运动物体,并在其到达被追逐物体的位置时停止。我已经尝试过使用TranslateonAxis,如下所示
out = new FileWriter( "C:/Users/Documents/Live/data.CSV" );
out.write ("User, Figure 1,Figure 2, Figure 3, Figure 4 ");
out.write ("\n");
out.write ( "User" ) ;
out.write (",");
out.write ( variableOrZero( "Figure1" ) );
out.write (",");
out.write ( variableOrZero( "Figure2" ) );
out.write (",");
out.write ( variableOrZero( "Figure3" ) );
out.write (",");
out.write ( variableOrZero( "Figure4" ) );
out.close();
使追赶者到达目标位置(被追逐对象的位置)的位置,但追赶者对象最终完全沿另一个方向移动。我认为原因可能是由于物体的世界位置和局部位置不同。
这是我的代码
this.el.object3D.translateOnAxis(targetposition, distance);
我尝试使用move组件将#orca移向#boats(可以使用WASD进行移动)。代码-
<a-scene physics="gravity: 0">
<a-assets>
<a-asset-item id="boat" src="../images/models/surfboard/scene.gltf"></a-
asset-item>
<a-asset-item id="orca1" src="../images/models/orca/scene.gltf"></a-asset-
item>
</a-assets>
<a-entity position="0 1.8 0">
<a-camera id="camera" look-controls="enabled: false">
<a-entity id="boats" position="0 -2 -4" rotation="0 -90 0"
scale=".02 .02 .02" gltf-model="#boat" static-body></a-entity>
</a-camera>
</a-entity>
<a-entity id="orca" position="-1 0.7 -40" gltf-model="#orca1" static-body move></a-entity>
</a-scene>
附带说明-我希望有更好的解释性材料来解释worldToLocal和LocaltoWorld方法。
答案 0 :(得分:2)
1)您需要通过将世界空间转换为局部空间来获得船的位置。
// Get orca's object in regard to the target object
vec3 = this.el.object3D.worldToLocal(target.object3D.position.clone())
从T的角度看,它不需要移向[0,0,0]。它需要从其自身的角度[1,-0.7,5]计算空框的位置。此外,空间不仅与位置有关,而且与旋转有关(和比例,此处不重要)。当T旋转时,空白框的位置将有所不同。
2)使用THREE
s a.distanceTo(b)
检查距离。如果距离大于所需距离,请移动逆戟鲸:
var target = this.el.sceneEl.querySelector('a-camera'); // this should be in the init()
var vec3 = new THREE.Vector3();
var currentPosition = this.el.object3D.position;
// clone the position to operate on a copy
vec3 = this.el.object3D.worldToLocal(target.object3D.position.clone())
var distance = dt*this.data.speed / 1000;
var camFromOrca = currentPosition.distanceTo( target.object3D.position );
// if the distance is more than one meter, move the orca
if (camFromOrca > 1) {
this.el.object3D.translateOnAxis(vec3, distance);
}
提琴here。其实很诡异:)
答案 1 :(得分:1)
var vec3 = new THREE.Vector3();
var obj = this.el.object3D;
var targ = target.object3D;
obj.position.add(vec3.copy(targ.position).sub(obj.position).multiplyScalar(0.1));