以统一的初始脉冲在抛物线路径上移动 2D 物理体 [英] Moving a 2D physics body on a parabolic path with initial impulse in unity

查看:21
本文介绍了以统一的初始脉冲在抛物线路径上移动 2D 物理体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有什么:

Unity 5 2D 中的一个射弹,受重力影响,我想在抛物线路径上以初始脉冲从 A 点移动到 B 点

A projectile in Unity 5 2D, affected by gravity, that I want to move from point A to point B with initial impulse on a parabolic path

我所知道的:

  • 随机起始位置的二维坐标 (A)
  • 随机目标位置的二维坐标 (B)
  • 我希望身体到达目标位置的时间(X)

我想知道的:

  • 为了让它在 X 时间后到达所需位置 B,我必须一次性向身体施加的初始脉冲(如在 unity applyforce 和 forcemode 脉冲中一样).

注意事项:

  • 这是一个使用 Unity 2D 物理的 2D 世界
  • 两个位置 A 和 B 是完全随机的,可能处于也可能不处于相同的高度(y 坐标)
  • 我只想在生成弹丸时应用一次脉冲
  • 抛射物除了重力之外没有任何其他力影响他(对于当前的问题,假设它总是指向下方的 9.8 级)

这是我想要的(坏)图:D

Here is a (bad) drawing of what I want :D

谢谢前面的人!

推荐答案

让我们解决一些更简单的问题.我们的目标是找到粒子的初始速度,而不是脉冲",因为这样可以更好地控制.

Let's solve a few simpler problems. Our goal is to find an initial velocity for the particle, instead of an "impulse", because we'll achieve better control this way.

首先,考虑我们如何在没有 Unity 物理的情况下解决这个问题.作为时间函数的粒子的位置取决于其初始位置、速度和加速度.无论您有多少维度,相同的方程式都适用.这个等式是:

First, consider how we would solve this without Unity physics. A particle's position as a function of time depends on its initial position, velocity and acceleration. The same equations apply regardless of how many dimensions you have. This equation is:

current_position = 1/2 * 加速度 * 时间^2 + 速度 * 时间 + 起始位置

current_position 成为我们想要的目的地,time 是您选择的旅行持续时间,而 starting_position 是目的地的起始位置粒子.

Let current_position be our desired destination, time be the duration of travel you choose, and the starting_position to be the starting position of the particle.

在 x 维度上,没有加速度.让 X_0 是我们的起始 X 位置,X_1 是我们的目的地,T 是我们想要花费的时间到达那个目的地.我们想要找到 v_x,或者 x 维度的速度.为便于解释,我将所有常量标记为大写,变量标记为小写.

In the x-dimension, there is no acceleration. Let X_0 be our starting X position, X_1 be our destination, and T be the duration of time we want to spend getting to that destination. We want to find v_x, or the velocity in the x-dimension. For ease of interpretation, I've marked all the constants in uppercase and the variable in lowercase.

  1. X_1 = v_x*T + X_0
  2. v_x = (X_1-X_0)/T

在 y 维度上,我们有某种重力.因此,让我们将其合并为我们的加速:

In the y-dimension, we have gravity of some kind. So let's incorporate that as our acceleration:

  1. Y_1 = 0.5*G*T*T + v_y*T + Y_0
  2. v_y = (Y_1 - Y_0 - 0.5*G*T*T)/T

既然你有 v_xv_y,只需设置速度:

Now that you have v_x and v_y, just set the velocity:

rigidbody2d.velocity = new Vector2(v_x, v_y);

由于不同的物体具有不同的质量,因此您不想尝试计算冲量.只需在生成对象时将速度设置为您想要的帧速度即可.

Since different objects have different mass, you don't want to try to calculate an impulse. Just set the velocity to what you want in the frame when the object is spawned.

这与您可能会在 Internet 上的其他地方找到的建议相反,但先尝试,然后再做更复杂的事情.

This runs contrary to advice you might find elsewhere on the Internet, but try it first before doing something more complicated.

一些重要的注意事项:

  • Unity 中的物理处理不稳定:您可能会在不同平台上获得不同的结果,尤其是在不同的移动设备之间.如果有表现压力,可能会有更少的物理评估,从而导致不正确的结果.这通常不是 2D 物理的问题.
  • 您可能会遇到重力大小问题,具体取决于您的版本和其他设置.

这篇关于以统一的初始脉冲在抛物线路径上移动 2D 物理体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆