团结:逼真的给定目标点和方向的汽车 [英] Unity: turning a car realistically given target point and direction

查看:88
本文介绍了团结:逼真的给定目标点和方向的汽车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CSV,可以在给定的时间t内以秒为单位给出具有id的汽车的x,z坐标. 以下是具有id=1的汽车的摘录数据,其坐标为几秒钟.在最初的三秒钟内,汽车停了下来.然后,它向左转 并继续直行几秒钟.

I have a CSV that gives me the x,z coordinates of a car with id at a given time t in seconds. Below is an excerpt data for a car with id=1 and it's coordinates over a few seconds. For the first three seconds, the car is stopped. It then turns left and continues straight for a few more seconds.

此刻,我正在根据下一个位置计算方向,并旋转汽车使其朝向该方向,但是转弯处没有曲线",它只是直接移至下一个位置:

At the moment I'm calculating the direction based on the next position and rotating the car to face towards that, but there is no 'curve' to the turn, it simply moves straight to the next position:

Vector3 direction = (nextPosition - car.transform.position).normalized;
if(direction != Vector3.zero)
{
    Quaternion lookRotation = Quaternion.LookRotation(direction);
    float step = speed * Time.deltaTime;
    car.transform.rotation = Quaternion.Slerp(car.transform.rotation, lookRotation, Time.deltaTime * rotationSpeed);
}

仅给出这些点,特别是转弯发生处的958959处的两个点,是否有办法更实际地转弯?我知道Unity的标准资产CarController.cs具有 steering 功能,但是我不确定是否可以利用它来发挥自己的优势.

Given just these points, specifically the two at 958 and 959 where the turn occurs, is there a way to more realistically make the turn? I know that Unity's standard asset CarController.cs has steering functionality, but I'm not sure if I can use that to my advantage.

t,id,x,z
956,1,-1.50,232.39
957,1,-1.50,232.39
958,1,-1.50,232.39
959,1,-4.50,209.72
960,1,-4.50,193.05
961,1,-4.50,176.39

推荐答案

正如我在回答上一个问题时所指出的那样,一个很好的第一近似值是计算两次测量之间间隙中"的平均速度,将汽车指向在平均速度的方向上,就完成了.

As I noted in my answer to your previous question, a good first approximation is to compute the average velocity "in the gap" between the measurements, point the car in the direction of the average velocity, and you're done.

这不会产生逼真的运动,因为汽车当然不会瞬时改变速度.而是在汽车上施加了,并且在特定方向上使汽车加速. (请注意,减速是一种加速度;我没有在加速度和减速度之间进行区分.)

This does not produce realistic motion because of course cars do not instantaneously change in velocity. Rather, there are forces applied to the car, and forces accelerate the car in a particular direction. (Note that slowing down is an acceleration; I'm not making a distinction between acceleration and deceleration.)

您拥有每秒的位置,并且您知道如何根据上一个问题计算每秒的平均速度.现在以相同的方式计算平均加速度.我们知道F = m a,所以如果汽车的质量保持不变,那么作用在汽车上的净力就与加速度成比例.

You have the position at every second, and you know how to compute the average velocity at every second from the previous question. Now compute the average acceleration in the same manner. We know that F = m a, so if the mass of the car stays the same, then the net force acting on the car is proportional to the acceleration.

然后您每秒钟要做的是,问自己一个问题:为了在已知的位置和速度到达下一个已知的点,我需要在这个确切的时刻具有什么加速度?"也就是说:如果我从时间t开始沿着加速度矢量平稳地加速,那将是什么矢量使我到达时间t + 1处的已知位置和平均速度?

What you can do then is at every second, ask yourself the question "what acceleration would I need to have at this exact moment in order to get to the next known point at the known position and velocity?" That is: if I accelerated smoothly along the acceleration vector starting from time t, what vector would that be that gets me to my known position and average velocity at time t+1?

一旦您知道,您就可以在秒之间进行速度矢量的插值,并且您会发现曲线更加平滑.然后以相同的方式对位置进行插值.

Once you know that then you can do interpolation of velocity vectors between the seconds, and you will find that the curves are much smoother. Then you do interpolation of the positions in the same manner.

由于混蛋,它们仍然不会完全平滑;即加速度的瞬时变化,人类认为这是猛烈的运动.就像有人将它放在车上或突然转弯时一样.

They still will not be perfectly smooth because of jerk; that is, instantaneous change in acceleration, which humans perceive as a jerking motion. Like when someone floors it in a car, or suddenly turns.

要使运动真正平滑,您需要进一步走一步,并对加速做同样的技巧.找出我每秒需要做些什么,才能在下一秒满足我的目标位置,速度和加速度.

To make the motion really smooth you need to go one step further and do the same trick to acceleration. Figure out what jerk do I need at every second in order to meet my target position, velocity and acceleration at the next second.

然后您可以在几秒钟之间插值位置,速度和加速度,并获得平滑的运动.

You can then interpolate the position, velocity and acceleration between the seconds and get a nice smooth motion.

这不是从一系列点上获得平滑运动的唯一方法,而是这样做的角色建构. 您能想到其他解决此问题的方法吗??

This is not the only way to get smooth motion from a series of points, but it's character building to do so. Can you think of any other techniques to solve this problem?

(此外,我注意到该问题在任何数量的维度上都没有变得更加困难.您可以考虑先解决一维运动的问题,然后将您的解决方案扩展到二维运动.)

(Also, I note that the problem is not significantly harder in any number of dimensions. You might consider solving the problem for one-dimensional motion first, and then extend your solution to two-dimensional motion.)

让我们看一些数字,我们将仅看x数字.

Let's look at some numbers, and we'll look just at the x figure.

我们知道在最初的几秒钟内,位置为-1.5,然后在下一秒内,位置为-4.5.因此,平均速度和加速度一会儿为零,然后一秒钟的平均速度为-3.因此,加速度为零到-3,相差为-3,因此我们每秒以每秒-3米的速度加速度.

We know that for the first couple of seconds, the position is -1.5, and then in the next second the position is -4.5. So the average velocity and acceleration was zero for a while, and then the average velocity was -3 for a second. Therefore the acceleration was zero to -3, that's a difference of -3, so we've accelerated by -3 meters per second per second for a second.

现在让我们考虑一下.

Now let's think about that for a moment.

假设您已停止,并且不断施加力量以每秒每秒-3米的速度将您加速.显然,在一秒钟之后,您将以每秒-3米的速度前进.但是你会在哪里?如果您瞬时从零变到-3 m/s,那么一秒钟后您将处于-3米的距离,但是我们不再将速度建模为瞬时的!您距离起始位置仅-1.5,而不是-3.

Suppose you're stopped, and you continuously apply a force that will accelerate you by -3 meters per second per second. Plainly after one second you'll be going -3 meters per second. But where will you be? If you instantaneously went from zero to -3 m/s then you would be -3 meters away after a second, but we are no longer modeling velocities as instantaneous! You would only be -1.5 away from the starting position, not -3 away.

您需要的实际平均加速度是-6.每秒平均-6的加速度会使您的速度从现在开始变为-6秒钟,但是您从零开始,因此在那一秒内的平均速度为,如我们所愿,-3.这样一秒钟您就可以从-1.5到-4.5,这就是您想要的.

The actual average acceleration you need is -6. An average acceleration of -6 for a second gets your velocity to -6 one second from now, but you started at zero, and so the average velocity over that second is, as we want, -3. And that gets you from -1.5 to -4.5 in one second, which is what you want.

那么:从速度零开始,在恒定加速度为-6 m/s/s的情况下,在时间958.5处的位置和速度是什么?正如我们所期望的那样,速度是-3-平均应该是一半.位置变化是a Δt 2 / 2(您知道为什么吗?)因此是-6 * 0.5 * 0.5/2,即-0.75,是我们-3的四分之一将在第二秒走.再次,这应该是有道理的:我们在上半秒中将放慢,而不是在下半秒中,因此我们在上半秒之前应小于一半

So: what then is the position and velocity at time 958.5, given a constant acceleration of -6 m/s/s, starting from velocity zero? The velocity is -3, as we'd expect -- the average should be about halfway. And the position change is a Δt2/ 2 (do you see why?) So that's -6 * 0.5 * 0.5 / 2, which is -0.75, a quarter of the -3 that we're going to go in this second. Again, this should make sense: we're moving slower in the first half second than the second half second, so we should be less than half way by the first half second.

现在做同样的事情,只是为了混蛋.如果这样做,您会发现您的动作确实变得非常流畅.

Now do the same thing, but for jerk. If you do that you'll find that your motions get very smooth indeed.

我希望一切都说得通.好的入门物理学课本可能对您有很大帮助.

I hope that is all making sense. A good introductory physics text will probably help immensely here.

这篇关于团结:逼真的给定目标点和方向的汽车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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