Box2D (C++) 重力井
目前,我在我的一个游戏中使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
遗憾的是,Box2D 中并未内置此功能。最简单的事情是获取重力井和刚体之间的角度和距离,并相应地设置刚体的速度。
获取角度:
获取距离:
我使用 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:
To get the distance:
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