Chipmunk 2D 设置轨道速度功能

发布于 2024-11-13 22:35:10 字数 798 浏览 2 评论 0原文

因此,我使用 Planet.c 演示为我的 2D iPhone 游戏设置了速度函数,该游戏正在开发涉及绕轨道运行的行星的作品。然而,以下代码设置了很好的轨道,但仅围绕屏幕的原点(即屏幕的左下角)进行设置:

// Point mass position
cpVect p = body->p;

cpFloat sqdist = cpvlengthsq(p);
cpVect g = cpvmult(p, -gravityStrength / (sqdist * cpfsqrt(sqdist)));

cpBodyUpdateVelocity(body, g, damping, dt);

我尝试简单地添加中心恒星位置的位移向量(在本例中为屏幕的中心) cpv(160, 240)) 但这只会将其发送到一个奇怪的轨道上。我一直尝试各种尝试让我的身体相对于中心而不是原点的位置向量和径向向量,但我无法让它正常工作。我正在思考以下内容:

// Sun position
cpVect disp = cpv(160, 240);

// Point mass position
cpVect p = body->p;

// Point mass relative to Sun
p = cpvsub(disp, p);

cpFloat sqdist = cpvlengthsq(p);
cpVect g = cpvmult(p, -gravityStrength / (sqdist * cpfsqrt(sqdist)));

cpBodyUpdateVelocity(body, g, damping, dt);

有什么想法吗?谢谢!

So I've used the Planet.c demo to set up a velocity function for my 2D iPhone game that's in the works that involved orbiting planets. However the following code sets up nice orbits but only does so around the origin of the screen i.e. the lower left corner of the screen:

// Point mass position
cpVect p = body->p;

cpFloat sqdist = cpvlengthsq(p);
cpVect g = cpvmult(p, -gravityStrength / (sqdist * cpfsqrt(sqdist)));

cpBodyUpdateVelocity(body, g, damping, dt);

I've tried simply adding a displacement vector of the central star's position (in this case the center of the screen cpv(160, 240)) but that just sends it off in a strange orbit. I've always tried a variety of trying to get my body's position vector and radial vector in relation to the center as opposed to the origin but I can't get it to work quite right. I'm thinking something along the lines of:

// Sun position
cpVect disp = cpv(160, 240);

// Point mass position
cpVect p = body->p;

// Point mass relative to Sun
p = cpvsub(disp, p);

cpFloat sqdist = cpvlengthsq(p);
cpVect g = cpvmult(p, -gravityStrength / (sqdist * cpfsqrt(sqdist)));

cpBodyUpdateVelocity(body, g, damping, dt);

Any ideas? Thanks!

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

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

发布评论

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

评论(1

金兰素衣 2024-11-20 22:35:10

知道了!正如我怀疑的那样,这只是获得正确的向量减法的问题,即中心减去位置向量。干得好!

Got it! As I suspected was just a matter of getting the right vector subtractions namely the center minus the position vector. Well done!

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