简体   繁体   English

Three.js - 如何计算弹丸的角度和弹道?

[英]Three.js - How to calculate a projectile's angle and ballistics?

I'm using FlyControls.js for my camera view我将FlyControls.js用于我的相机视图

I'm trying to add a projectile to my object我正在尝试将projectile添加到我的 object

The projectile should use the angles from camera position, to crosshair/reticle position, and flow freely through 3D space射弹应使用从相机 position 到十字准线/十字线 position 的角度,并自由流过 3D 空间

I've tried to use camera.position.angleTo(crosshair.position) but it only returns a single float value.我试过使用camera.position.angleTo(crosshair.position)但它只返回一个浮点值。 In 2D I'd use sin and cos , but 3D is different在 2D 中我会使用sincos ,但 3D 是不同的

Also, the projectile is a cylinder , how would I rotate the cylinder to face the direction it's going?另外,射弹是一个cylinder ,我将如何旋转圆柱体以面对它的方向?

//how to get this information?
projectile.angle = {
    x: ?,
    y: ?,
    z: ?,
};

//how to get this information?
projectile.rotation = faceTowardsDestination;

function animate(){
    //projectile movement
    projectile.position.x += projectile.angle.x * projectile.speed;
    projectile.position.y += projectile.angle.y * projectile.speed;
    projectile.position.z += projectile.angle.z * projectile.speed;

    requestAnimationFrame(animate);
}

Projectile (cylinder) should fire from spaceship, towards the crosshair point, and point towards it's destination射弹(圆柱体)应该从飞船发射,朝向十字准线,并指向它的目的地在此处输入图像描述

What you're searching for is the Object3D.lookAt(vec3) method see here for docs .您正在搜索的是Object3D.lookAt(vec3)方法,请参见此处的文档 It rotates the object so it's directly "looking at" the position you tell it to.它旋转 object,因此它直接“查看”您告诉它的 position。

// Establish the coordinages of the target
const target = new THREE.Vector3(x, y, z);

// Make the missile point directly at the position of your target
projectile.lookAt(target);

let speed = 1;

function animate() {
    // Now you move it forward by translating down its own Z-axis
    projectile.translateZ(speed);
    requestAnimationFrame(animate);
}

When your projectile is already rotated to face the crosshairs, you only need to translate it down its local Z-axis (not the global z-axis), so no need to worry about setting the X & Y coordinates.当您的射弹已经旋转到面对十字准线时,您只需将其沿其局部 Z 轴(而不是全局 Z 轴)平移,因此无需担心设置 X 和 Y 坐标。

In order for a projectile to go towards the crosshair, you first have to project the location of the crosshair far in front of the camera.为了将 go 的弹丸射向十字准线,您首先必须将十字准线的位置投射到相机前方很远的位置。

The simplest way I can suggest doing this is creating an infinite plane that's a child of child of the camera, placing it some distance away, and then casting a ray using aRaycaster to find the intersection of the crosshair with this plane.我可以建议这样做的最简单的方法是创建一个无限平面,它是相机子级的子级,将其放置在一定距离之外,然后使用Raycaster投射光线以找到十字准线与该平面的交点。 See an example of intersection between the mouse pointer and a surface here . 在此处查看鼠标指针和表面之间的相交示例。

The vector between your ship's position and this intersection is the direction of travel you'll want to use for your projective.您的船的 position 与此交点之间的矢量是您要用于投影的行进方向。 You probably want to adjust the rotation of the ship accordingly.您可能想相应地调整船的旋转。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM