如何基于 b2Joint 实例化提取/创建 JointDef

发布于 2024-11-27 19:00:03 字数 1863 浏览 0 评论 0原文

我正在尝试序列化 b2World,由于 Box2D 有许多内部使用、创建和删除的私有对象,问题变得更加复杂。我决定应该使用这些内部对象 get() 函数来获取我需要的数据,并在保存时基于它创建一个“b2Definition”对象,并使用全局工厂“create”方法的定义来重新创建对象。

我遇到了一些需要帮助的问题:在下面的代码中是我需要的所有 JointDef 数据,但无法弄清楚如何从指针获取正确类型的对象。

我还想知道我是否应该尝试保存联系人数据......它是在工厂创建方法中自动创建的吗?

        b2WeldJointDef JointDef; //QUESTION: how do i get this
        //JointDef.referenceAngle=      Joint-> ?????       

        b2GearJointDef JointDef; //QUESTION: how do i get these
        //JointDef.joint1=              Joint-> ??????  
        //JointDef.joint2=              Joint-> ??????

        b2LineJointDef JointDef; //QUESTION: how do i get these??
        //JointDef.localAxisA=          Joint-> ????
        //JointDef.lowerTranslation=    Joint-> ????    
        //JointDef.upperTranslation=    Joint-> ????

        b2MouseJointDef JointDef; //No problems

        b2PrismaticJointDef JointDef; //QUESTION: how do i get these??
        //JointDef.referenceAngle=      Joint-> ????
        //JointDef.localAxis1=          Joint-> ????  
        //JointDef.lowerTranslation=    Joint-> ????                    //JointDef.upperTranslation=    Joint-> ????  
        //JointDef.maxMotorForce=       Joint-> ????

        b2PulleyJointDef JointDef; //QUESTION: how do i get these?
        //JointDef.maxLengthA=          Joint-> ????
        //JointDef.maxLengthB=          Joint-> ????    

        b2RevoluteJointDef JointDef; //QUESTION: how do i get these?
        //JointDef.maxMotorTorque=      Joint-> ????
        //JointDef.referenceAngle       Joint-> ????
        //JointDef.lowerAngle=          Joint-> ????    
        //JointDef.upperAngle=          Joint-> ????

        b2JointDef JointDef;
        //JointDef.collideConnected= ????   

我需要上面的数据吗?有办法得到吗?

I'm trying to serialize a b2World, and due to Box2D's many private objects that are used, made, and deleted internally, the issue becomes much more complicated. I decided that i should use these internal objects get() functions to get the data i need, and create a "b2Definition" object based off it at save time, and use the definition with the global factories "create" methods to recreate the objects.

I have encountered a few problems i need help on however: In the following code is all the JointDef data i need, but can't figure out how to get from the pointer to the correct type of object.

I also am wondering if i should even attempt to save contact data.... Is it automatically made in the factory create metods?

        b2WeldJointDef JointDef; //QUESTION: how do i get this
        //JointDef.referenceAngle=      Joint-> ?????       

        b2GearJointDef JointDef; //QUESTION: how do i get these
        //JointDef.joint1=              Joint-> ??????  
        //JointDef.joint2=              Joint-> ??????

        b2LineJointDef JointDef; //QUESTION: how do i get these??
        //JointDef.localAxisA=          Joint-> ????
        //JointDef.lowerTranslation=    Joint-> ????    
        //JointDef.upperTranslation=    Joint-> ????

        b2MouseJointDef JointDef; //No problems

        b2PrismaticJointDef JointDef; //QUESTION: how do i get these??
        //JointDef.referenceAngle=      Joint-> ????
        //JointDef.localAxis1=          Joint-> ????  
        //JointDef.lowerTranslation=    Joint-> ????                    //JointDef.upperTranslation=    Joint-> ????  
        //JointDef.maxMotorForce=       Joint-> ????

        b2PulleyJointDef JointDef; //QUESTION: how do i get these?
        //JointDef.maxLengthA=          Joint-> ????
        //JointDef.maxLengthB=          Joint-> ????    

        b2RevoluteJointDef JointDef; //QUESTION: how do i get these?
        //JointDef.maxMotorTorque=      Joint-> ????
        //JointDef.referenceAngle       Joint-> ????
        //JointDef.lowerAngle=          Joint-> ????    
        //JointDef.upperAngle=          Joint-> ????

        b2JointDef JointDef;
        //JointDef.collideConnected= ????   

Do i need the data above? Is there a way to get it?

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

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

发布评论

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

评论(1

养猫人 2024-12-04 19:00:03

我最近做了很多这样的事情来制作一个导出/导入实用程序,将 Box2D 世界序列化为 JSON,然后再次加载它。您可能会发现源代码很有用 - 查看 http://www.iforce2d.net/b2djson 滚动至最下面可以看到源代码,看一下函数 b2dJson::b2j(b2Joint* joint)

并不复杂,只需要检查joint类型并转换为该类型的指针即可访问内容:

switch ( joint->GetType() )
{
    case e_revoluteJoint:
    {
        b2RevoluteJoint* revoluteJoint = (b2RevoluteJoint*)joint;
        ...
    }
}

但需要注意一些事项:

  • 您需要更改 Box2D 代码本身,以向关节添加一些额外的 GetXXX() 函数来获取所有必要的信息。我也在该页面上放置了它们的列表,这样您就可以检查这些对于我的情况来说是什么(我使用的是最新的 svn)
  • 关节持有一个指向它们加入的主体的内存指针,这对于序列化来说是无用的,所以您首先需要要序列化世界中的物体,然后在该列表中给关节一个索引,
  • 关节定义会采用参考角度,但关节在创建后不会保留该角度,因此您需要使用身体角度和关节角度来计算它的值 齿轮接头连接
  • 另外两个接头并且这些接头被固定作为内存指针,因此要重新创建它们,您需要首先序列化所有非齿轮接头,然后在该列表中为齿轮接头提供索引,
  • 最近齿轮接头已更改,以便它们不再保留指向它们控制的其他两个接头的指针,因此如果您想使用最新的 svn 支持齿轮接头,则需要将它们添加回来

I did a lot of this recently to make an export/import utility to serialize a Box2D world to JSON and then load it again. You may find the source code useful - check out http://www.iforce2d.net/b2djson Scroll towards the bottom and you can see the source code, look at the function b2dJson::b2j(b2Joint* joint)

It's not complicated, you just need to check the joint type and cast to a pointer of that type to access the contents:

switch ( joint->GetType() )
{
    case e_revoluteJoint:
    {
        b2RevoluteJoint* revoluteJoint = (b2RevoluteJoint*)joint;
        ...
    }
}

A few things to note though:

  • you will need to change the Box2D code itself to add some extra GetXXX() functions to the joints to get all the necessary info. I put a list of them on that page too so you can check what these were for my case (I was using the most recent svn)
  • joints hold a memory pointer to the bodies they join which is useless to serialize, so you will first need to serialize the bodies in the world, then give the joints an index in that list
  • joint defs take a reference angle but joints do not hold this after they are created, so you need to use the body angles and the joint angle to calculate what it was
  • gear joints join two other joints and these are held as memory pointers, so to recreate these you will need to first serialize all non-gear joints, then give the gear joints an index in that list
  • recently gear joints were changed so that they no longer keep the pointers to the two other joints they control, so you will need to add them back if you want to support gear joints with the most recent svn
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文