【问题标题】:Projectile trajectory based on angle基于角度的弹丸轨迹
【发布时间】:2020-05-05 14:20:20
【问题描述】:

在我的 3D 游戏(自上而下的射击游戏)中,我尝试这样做: 当您在地图上的特定区域单击鼠标按钮(点上的光线投射) -> 从枪中射出一枪 -> 弹丸以特定路径沿枪管方向飞行,并且与鼠标指针的距离相同(但不是方向)。 我已经有了计算弹丸轨迹的源代码。 帮助使射弹沿枪管的方向飞行,但使用 mouseposition 显示的距离 射线投射,而不是 mouseposition 方向。

```private void FireCannonAtPoint(Vector3 point)
{
    var velocity = BallisticVelocity(point, _angle);

    GameObject projectile = Instantiate(_projectile, _shootPoint.transform.position, Quaternion.identity) as GameObject;
            projectile.GetComponent<Rigidbody>().velocity = velocity;
}

private Vector3 BallisticVelocity(Vector3 destination, float angle)
{
    Vector3 dir = destination - _shootPoint.transform.position; 
    float height = dir.y;
    dir.y = 0; 
    float dist = dir.magnitude; 
    float a = angle * Mathf.Deg2Rad; 
    dir.y = dist * Mathf.Tan(a); 
    dist += height / Mathf.Tan(a); 

    float velocity = Mathf.Sqrt(dist * Physics.gravity.magnitude / Mathf.Sin(2 * a));
    return velocity * dir.normalized; 
}```

【问题讨论】:

  • 鼠标位置光线投射而不是鼠标位置方向是什么意思?
  • 如果你看一下我附上的图片:鼠标位置方向是 - “炮塔”和“raycast mouseclick”之间的线。 mouseposition raycast 是 - 子弹从“炮塔”向前飞(图中红色箭头)。

标签: c# unity3d vector game-physics


【解决方案1】:

枪管:在 3D 中分割[turret_end_1, turret_end2],其中turret_end_1 是炮塔上子弹离开它的点。

屏幕:焦点F和屏幕平面,命名为screen_plane

# From 3D to 2D: projection from 3D space on the screen_plane:
Mouse = [mouse_x, mouse_y] # given on the screen_plane
B1 = project_on_screen_plane(turret_end_1)
B2 = project_on_screen_plane(turret_end_2)

# Calculations in 2D, i.e. on the screen_plane
U = B1-B2
U = ( dot_product(Mouse-B1, U) / dot_product(U, U) ) * U
U = B1 + U # this is the point you are looking for on the screen


# From 2D back to 3D: connect the focal point with the point U on the screen plane
# to form a line and find its intersection point in 3D with the line B1 B2
# aligned with the turret.
# line through F with directing vector Vector_from_F_to(U)
# line through B1 with directing vector B1-B2

V = intersect( F,  Vector_from_F_to(U),  B1,  B1-B2 ) # the point you are looking for in 3D

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-05
    • 1970-01-01
    • 1970-01-01
    • 2021-08-28
    • 2019-07-31
    • 1970-01-01
    相关资源
    最近更新 更多