保存 Box2D 形状

发布于 2024-12-28 15:11:14 字数 251 浏览 0 评论 0原文

作为我的保存/加载游戏代码的一部分,我需要保存世界上所有 Box2D 主体的状态。当我这样做,然后加载并重新创建它们时,会快速弹出一些实体彼此分离的信息。我已经仔细检查了我的保存游戏信息,它是正确的。

对于每个物体,我都会保存世界位置、角度、AngularVelocity 和 LinearVelocity。我还需要保存更多吗?

我只是想知道是否真的不可能完全保存 Box2D 世界的状态。

我在 iOS 中使用 C++ Box2d 代码。

As part of my save/load game code, I need to save the state of all the Box2D bodies that are in the world. When I do this and then load and recreate them there is a quick pop were some of the bodies separate from each other. I've double and triple checked my save game information and it is correct.

For each body, I'm saving the world position, angle, AngularVelocity and LinearVelocity. Is there more I need to save?

I'm just wondering if it's not really possible to fully save the state of the Box2D world.

I am using the C++ Box2d code in iOS.

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

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

发布评论

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

评论(2

捶死心动 2025-01-04 15:11:14

对于每个物体,我都会保存世界位置、角度、AngularVelocity 和 LinearVelocity。我还需要保存更多吗?

来自 Box2D 2.3.2 b2Body 成员变量的回顾(在 b2Body.h),您可能还想保存:

  • 主体的顺序(也许按照顺序保存它们)它们是从 b2World 访问的,如 b2World::Dump() 那样),
  • 线性和角度阻尼值(通过 b2Body::GetLinearDamping() > 和 b2Body::GetAngularDamping()),
  • 重力尺度(通过 b2Body::GetGravityScale()),
  • 类型(通过b2Body::GetType())、
  • is-bullet 值(通过 b2Body::IsBullet())、
  • is-sleeping-allowed 值(通过 b2Body ::IsSleepingAllowed()),
  • is-awake 值(通过 b2Body::IsAwake()),
  • is-active 值(通过b2Body::IsActive())、
  • is-fixed-rotation 值(通过 b2Body::IsFixedRotation())、
  • fixture-list 及其顺序(通过 >b2Body::GetFixtureList())、
  • 用户数据(通过 b2Body::GetUserData())、
  • 力、
  • 扭矩和
  • 睡眠时间。

不幸的是,没有对这最后三个状态值的可读公共访问。

如果你的世界有关节,你可能还想保存那些,但我建议从世界的角度保存那些。

我只是想知道是否真的不可能完全保存 Box2D 世界的状态。

这取决于您在游戏中所做的事情以及您愿意以代码方式执行的操作。

如果您正在施加力或扭矩,并且您的世界未设置为自动清除这些力,那么从纯粹的 b2Body 角度来看:。 OTOH,如果您正在施加力或扭矩,您可以自行保存它们,在这种情况下,力和扭矩可以保存:

睡眠时间,这就是一个对你来说有多重要的问题。如果您希望能够稍后重新加载游戏,使其像从未暂停过一样恢复,那么至少从用户级别的角度来看,您会陷入困境;答案是。 OTOH 是的,如果您愿意修改 Box2D 库源代码的话。

例如,您可以将代码添加到 b2Body.h 文件中,如下所示,提供对睡眠时间状态的读取访问权限:

class b2Body
{
public:
    ...

    /// Get the sleep time.
    float32 GetSleepTime() const;

    ...
};

...

inline float32 b2Body::GetSleepTime() const
{
    return m_sleepTime;
}

当然,如果没有写入访问权限,这也是不完整的。虽然可行,但这可能会更困难,具体取决于您是否希望能够在构建主体时恢复睡眠时间,或者在构建后设置它(通过设置器)是否足够(后者代码较少,但前者可能语义上更有吸引力)。不管怎样,如果您还想查看加载该值的代码,请告诉我。

For each body, I'm saving the world position, angle, AngularVelocity and LinearVelocity. Is there more I need to save?

From a review of the Box2D 2.3.2 b2Body member variables (in b2Body.h), you may also want to save:

  • the ordering of the bodies (perhaps by saving them in the order they're accessed from b2World as b2World::Dump() does),
  • the linear and angular damping values (via b2Body::GetLinearDamping() and b2Body::GetAngularDamping()),
  • the gravity scale (via b2Body::GetGravityScale()),
  • the type (via b2Body::GetType()),
  • the is-bullet value (via b2Body::IsBullet()),
  • the is-sleeping-allowed value (via b2Body::IsSleepingAllowed()),
  • the is-awake value (via b2Body::IsAwake()),
  • the is-active value (via b2Body::IsActive()),
  • the is-fixed-rotation value (via b2Body::IsFixedRotation()),
  • the fixture-list and its order (via b2Body::GetFixtureList()),
  • the user-data (via b2Body::GetUserData()),
  • the force,
  • the torque, and
  • the sleep time.

Unfortunately, there is no readable public access to these last three state values.

If your world has joints, you may also want to save those but those I'd suggest saving from the world's perspective.

I'm just wondering if it's not really possible to fully save the state of the Box2D world.

That depends on what you're doing in your game and what you're willing to do code-wise.

If you're applying forces or torques and your world is not set to auto clear those forces then from a purely b2Body perspective: no. OTOH, if you're applying forces or torques, you could save those on your own in which case forces and torques could be savable: yes.

The sleep time, that'd be a question of how much that matters to you. If you want to be able to reload your game at a later time such that it'd resume exactly like it'd never been paused, then at least from a user-level perspective, you'd be stuck; the answer's no. OTOH yes, if you're willing to modify the Box2D library sources that is.

For example, you can add code to the b2Body.h file like the following that provides read access to the sleep time state:

class b2Body
{
public:
    ...

    /// Get the sleep time.
    float32 GetSleepTime() const;

    ...
};

...

inline float32 b2Body::GetSleepTime() const
{
    return m_sleepTime;
}

Of course, this wouldn't be complete without write access as well. While doable, that could be more difficult depending on whether you'd want to be able to restore the sleep time on construction of the body or if setting it after construction (via a setter) suffices (the latter being less code but the former perhaps being more semantically appealing). Anyways, let me know if you'd also like to see code for loading the value.

时光暖心i 2025-01-04 15:11:14

b2worlddump功能。它将有关世界的所有信息放入日志文件中。因此您可以查看此日志文件并了解必须保存哪些内容。

PS:我自己没有尝试过这个功能

There is dump function of b2world. It put all the information about the world into log file. So you can see this log file and understand what do you have to save.

PS: Did not tried this function myself

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