花栗鼠动力学的弹丸运动计算

发布于 2024-08-17 01:53:58 字数 1330 浏览 10 评论 0原文

我需要实现一点人工智能,它可以向刚体施加脉冲以击中目标。就像炮弹从大炮中发射出来一样。我使用 Chipmunk Dynamics 作为物理引擎。

我的数学很糟糕,我的物理也很糟糕,但我一直在阅读,并且在 SO 和以下内容的帮助下 维基百科条目,我得到了这个,

float x = target.x = launchPos.x;
float y = target.y = launchPos.y;
float g = 9.8;
float v = 100;
float angle1, angle2;

float tmp = pow(v, 4) - g * (g * pow(x, 2) + 2 * y * pow(v, 2));

if(tmp < 0){
NSLog(@"No Firing Solution");
}else{
angle1 = atan2(pow(v, 2) + sqrt(tmp), g * x);
angle2 = atan2(pow(v, 2) - sqrt(tmp), g * x);
}

// Split the velocities
float vVel = v * sin(angle2);
//NSLog(@"Vertical Velocity: %f", vVel);

float hVel = v / cos(angle2);
//NSLog(@"Horizontal Velocity: %f", hVel);

CGPoint force = cpv(hVel, vVel);

这应该给我一个角度,从中我可以计算发射射弹所需的水平和垂直速度。

然而,它不起作用,出于多种原因,这并不令我感到惊讶。首先是因为我在数学和物理方面很糟糕,但也因为我对其他一些事情感到困惑。

  1. 这个方法似乎没有考虑质量。应该吗?我本来以为这很重要?但后来,我在大学学的是艺术,所以我的想法可能是错的。

  2. Box2d有PTM_RATIO,但我在chipmunk中找不到类似的东西,那么我的值如何对应于chipmunk中的空间坐标?

  3. 我知道弧度以及它们与度数的区别,以及如何在两者之间进行转换。但我应该在这里使用哪个?我应该将角度1和角度2转换为度数吗?即使我这样做了,它仍然不起作用。

总而言之,关于物理学和花栗鼠,我有很多不明白的地方。所以我来这里寻求帮助。

我可以用花栗鼠中的某些东西来解决这个问题,或者如果有人必须自己解决这个问题,我真的很感激一些帮助。

I need to implement a little bit of Ai that can apply an impulse to a rigid body in order to hit a target. Like a cannon ball being fired from a cannon. I'm using Chipmunk Dynamics for the physics engine.

My maths is terrible, as is my physics, but i've been reading up, and with a little help from SO and the following from this wikipedia entry, I got this

float x = target.x = launchPos.x;
float y = target.y = launchPos.y;
float g = 9.8;
float v = 100;
float angle1, angle2;

float tmp = pow(v, 4) - g * (g * pow(x, 2) + 2 * y * pow(v, 2));

if(tmp < 0){
NSLog(@"No Firing Solution");
}else{
angle1 = atan2(pow(v, 2) + sqrt(tmp), g * x);
angle2 = atan2(pow(v, 2) - sqrt(tmp), g * x);
}

// Split the velocities
float vVel = v * sin(angle2);
//NSLog(@"Vertical Velocity: %f", vVel);

float hVel = v / cos(angle2);
//NSLog(@"Horizontal Velocity: %f", hVel);

CGPoint force = cpv(hVel, vVel);

Which should give me the angle, from which I can calculate the horizontal and vertical velocities needed to launch the projectile.

However, it's not working, which doesn't surprise me at all for a number of reasons. Firstly because I'm terrible at Maths and Physics, but also because I'm confused by a couple of other things.

  1. This method doesn't seem to take mass into account. Should it? I would have thought that was quite important? But then, I studied art at college, so I might be wrong about that.

  2. Box2d has PTM_RATIO, but I can't find anything like that in chipmunk, so how do my values correspond to the space coordinates in chipmunk?

  3. I know of radians and how they differ to degrees, and how to convert between the two. But which should I be using here? should I be converting angle1 and angle2 to degrees? Even if I do, It still doesn't work.

In summary, there's a lot about physics and chipmunk that I don't understand. So I'm here, asking for help.

Is there something in chipmunk that I can use to figure this out, or if anyone has had to figure this out themselves, I'd really appreciate some help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

卸妝后依然美 2024-08-24 01:53:58

首先,我认为水平速度应该是:

float hVel = v * cos(angle2);

而且,你是对的,你需要考虑质量。看起来您是直接从速度得出力矢量,这是不正确的。

我从未使用过 Chipmunk,但我猜你不能直接对物体应用速度?你必须施加一个力,将物体加速到特定的速度。力的方程很简单:

F = ma

由于我们将射弹从零速度加速到 v,因此加速度将等于 v。但是,您需要将此加速度乘以物体的质量才能获得所需的正确力。

CGPoint force = cpv(hVel, vVel) * projectileMass;

For one, I would think that horizontal velocity should be:

float hVel = v * cos(angle2);

And also, you are right that you need to take mass into consideration. It looks like you are deriving the force vector directly from velocity, and that's not correct.

I've never used Chipmunk, but I am guessing that you can't directly apply a velocity to an object? You have to apply a force, which accelerates an object to a particular velocity. The equation for Force is simple:

F = ma

Since we are accelerating this projectile from zero velocity to v, acceleration would be equal to v. However, you need to multiply this acceleration by the object's mass in order to get the correct force required.

CGPoint force = cpv(hVel, vVel) * projectileMass;
枫林﹌晚霞¤ 2024-08-24 01:53:58

好的。问题是(除了 v*cos(angle) 的拼写错误之外)您将力与冲量混淆了。

施加有角度的力并不能使物体的速度具有所需的角度和大小。您必须:

  • 通过使用 cpBodySetVel(cpBody *body, cpFloat Vel) 设置器手动更改速度,将速度设置为所需的速度,或者
  • 假设初始速度为 0,应用脉冲 到对象。

我怎么强调都不为过,力与脉冲不同:

  • 如果你对一个物体施加恒定的力,速度将随时间连续变化,从 0 速度到无限速度......根本不是什么你想要的。
  • 如果对物体施加一次性脉冲,速度将立即从 v0 变为 v0+脉冲/质量。

我认为花栗鼠不允许施加冲动。但是您可以通过将力除以算法的时间步长,在一个时间步上施加力,然后移除力来模拟脉冲。但最好坚持使用设置器设置速度。

如果您确实必须对身体施加恒定的力才能到达目标,那么这些方程就不是您在维基百科中找到的方程。

Ok. The problem is (Besides the typo of v*cos(angle)) that you are confusing force with impulse.

Applying a force with an angle does not make the object's velocity have the required angle and magnitude. You must either:

  • Set the velocity to the required velocity by manually changing the velocity using the cpBodySetVel(cpBody *body, cpFloat Vel) setter, or
  • Assuming the initial velocity is 0, apply an impulse to the object.

I can't stress this enough, a Force is not the same as an Impulse:

  • If you apply a constant force to an object, the velocity will change continuously in time, from 0 velocity to an infinite velocity... not at all what you want.
  • If you apply a one time impulse to an object, the velocity will change immediately from v0 to v0+impulse/mass.

I don't think Chipmunk allows applying impulses. But you can probably simulate an impulse by dividing the force by the time step of the algorithm, applying the force for one time step and removing the force afterwards. But it is probably better to stick to setting the velocity using the setter.

If you really must apply a constant force to the body to arrive to the target, then the equations are not the ones you found in wikipedia.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文