Box2D (C++) 重力井

发布于 2024-10-06 01:13:36 字数 204 浏览 4 评论 0原文

目前,我在我的一个游戏中使用 Box2D 物理引擎 - 我想知道是否可以创建一个重力井,其中所有物体都被吸引到一个任意点。有没有某种方法可以做到这一点,或者我必须对每个身体施加某种自定义的力量? (我尝试制作静态超密体,但 Box2D 并未在体对体的基础上应用牛顿万有引力定律)

另外,有没有办法制作反重力井?我可以制作一个以任意点为中心的更密集的球体并使用浮力来实现这一点吗?

Currently I am using the Box2D physics engine in a game of mine - and I was wondering if I could create a gravity well of sorts, in which all the bodies are attracted to a single arbitrary point. Is there a certain way to do this, or will i have to apply a custom force of sorts to each body? (I tried making a static superdense body, but Box2D doesn't apply Newton's Law of Universal Gravitation on a body-to-body basis)

Also, is there a way to make an anti-gravity well? Could I make a denser sphere centered around an arbitrary point and use buoyancy to achieve this?

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

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

发布评论

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

评论(1

夏末染殇 2024-10-13 01:13:37

遗憾的是,Box2D 中并未内置此功能。最简单的事情是获取重力井和刚体之间的角度和距离,并相应地设置刚体的速度。

获取角度:

double dx = rigidBodyX - gravityWellX;
double dy = rigidBodyY - gravityWellY;
double angle = atan2(dy, dx);
// angle is in radians, use atan2(dy, dx) / PI * 180 if 
// you need degrees

获取距离:

double dx = rigidBodyX - gravityWellX;
double dy = rigidBodyY - gravityWellY;
double dist = sqrt(dx * dx + dy * dy);

我使用 flash 和我编写的名为 QuickBox2D 的库快速编写了一个示例。它可能没有那么有用,因为语法与 C++ Box2D 库非常不同,但基本原理是相同的。它也不是一个完美的示例,但它可能会帮助您入门。

查看 Flash 示例

This functionality isn't built into Box2D unfortunately. The easiest thing to do is get the angle and the distance between the gravity well and the rigid body and set the bodies velocity accordingly.

To get the angle:

double dx = rigidBodyX - gravityWellX;
double dy = rigidBodyY - gravityWellY;
double angle = atan2(dy, dx);
// angle is in radians, use atan2(dy, dx) / PI * 180 if 
// you need degrees

To get the distance:

double dx = rigidBodyX - gravityWellX;
double dy = rigidBodyY - gravityWellY;
double dist = sqrt(dx * dx + dy * dy);

I whipped up a quick example using flash and a library I wrote called QuickBox2D. It might not be that helpful since the syntax is very different from the C++ Box2D library, but the basic principal is the same. It's also not a perfect example, but it might get you started.

See The Flash Example

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