花栗鼠动力学的弹丸运动计算
我需要实现一点人工智能,它可以向刚体施加脉冲以击中目标。就像炮弹从大炮中发射出来一样。我使用 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);
这应该给我一个角度,从中我可以计算发射射弹所需的水平和垂直速度。
然而,它不起作用,出于多种原因,这并不令我感到惊讶。首先是因为我在数学和物理方面很糟糕,但也因为我对其他一些事情感到困惑。
这个方法似乎没有考虑质量。应该吗?我本来以为这很重要?但后来,我在大学学的是艺术,所以我的想法可能是错的。
Box2d有PTM_RATIO,但我在chipmunk中找不到类似的东西,那么我的值如何对应于chipmunk中的空间坐标?
我知道弧度以及它们与度数的区别,以及如何在两者之间进行转换。但我应该在这里使用哪个?我应该将角度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.
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.
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?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,我认为水平速度应该是:
而且,你是对的,你需要考虑质量。看起来您是直接从速度得出力矢量,这是不正确的。
我从未使用过 Chipmunk,但我猜你不能直接对物体应用速度?你必须施加一个力,将物体加速到特定的速度。力的方程很简单:
由于我们将射弹从零速度加速到 v,因此加速度将等于 v。但是,您需要将此加速度乘以物体的质量才能获得所需的正确力。
For one, I would think that horizontal velocity should be:
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:
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.
好的。问题是(除了 v*cos(angle) 的拼写错误之外)您将力与冲量混淆了。
施加有角度的力并不能使物体的速度具有所需的角度和大小。您必须:
我怎么强调都不为过,力与脉冲不同:
我认为花栗鼠不允许施加冲动。但是您可以通过将力除以算法的时间步长,在一个时间步上施加力,然后移除力来模拟脉冲。但最好坚持使用设置器设置速度。
如果您确实必须对身体施加恒定的力才能到达目标,那么这些方程就不是您在维基百科中找到的方程。
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:
cpBodySetVel(cpBody *body, cpFloat Vel)
setter, orI can't stress this enough, a Force is not the same as an Impulse:
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.