Box2D 获取我身体的形状

发布于 2024-11-05 00:37:03 字数 934 浏览 3 评论 0原文

        Body b;
        while ((b=box2d.physics.PhysicssWorld.world.getBodyList().getNext())!=null) {

                   Shape shape;
                   while ((shape=b.getShapeList().getNext())!=null) {

                        Log.e("name",""+b.getUserData().toString()+" "+shape+" ");

                        opengl.saveMatrix();
                            Meshes.select(b.getUserData().toString())
                                    .translate((b.getPosition().x)*RATIO, (b.getPosition().y)*RATIO)
                                    .rotate((int) ((int) b.getAngle()* (180 / Math.PI)), 0, 0, 1)
                                    .draw(shape, 1,1,1);

                        opengl.loadMatrix();

                   }


        }

我想得到我身体的形状,但我什么也得不到,只有空..为什么?

切勿运行此行: Log.e("name",""+b.getUserData().toString()+" "+shape+" ");

所以 shape=b.getShapeList().getNext()) 总是 null...

        Body b;
        while ((b=box2d.physics.PhysicssWorld.world.getBodyList().getNext())!=null) {

                   Shape shape;
                   while ((shape=b.getShapeList().getNext())!=null) {

                        Log.e("name",""+b.getUserData().toString()+" "+shape+" ");

                        opengl.saveMatrix();
                            Meshes.select(b.getUserData().toString())
                                    .translate((b.getPosition().x)*RATIO, (b.getPosition().y)*RATIO)
                                    .rotate((int) ((int) b.getAngle()* (180 / Math.PI)), 0, 0, 1)
                                    .draw(shape, 1,1,1);

                        opengl.loadMatrix();

                   }


        }

I d like to get my bodies's shape, but i cant get anything, only null.. why?

never run this line: Log.e("name",""+b.getUserData().toString()+" "+shape+" ");

so shape=b.getShapeList().getNext()) always null...

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

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

发布评论

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

评论(1

拔了角的鹿 2024-11-12 00:37:03

我自己刚刚开始使用 Box2D。据我对图书馆的了解,获取人体形状的主要方法是通过它们的固定装置。从夹具中,您可以获得一个 b2Shape 指针 - 但是,因为它的方法是虚拟的,您可能需要将其转换为 b2PolygonShape/b2CircleShape 指针才能使用。以下是一些类似的代码:

void DoStuffWithShapes(b2World *World)
{
    b2Body * B = World->GetBodyList();
    while(B != NULL)
    {
        b2Fixture* F = B->GetFixtureList();             
        while(F != NULL)
        {
            switch (F->GetType())
            {
                case b2Shape::e_circle:
                {
                    b2CircleShape* circle = (b2CircleShape*) F->GetShape();                     
                    /* Do stuff with a circle shape */
                }
                break;

                case b2Shape::e_polygon:
                {
                    b2PolygonShape* poly = (b2PolygonShape*) F->GetShape();
                    /* Do stuff with a polygon shape */
                }
                break;
            }
            F = F->GetNext();
        }

        B = B->GetNext();
    }       
}

其他一些需要注意的事项: b2Body 的 .getNext() 函数返回一个指针 - 这是链表的实现。 b2Fixture::GetNext() 也是如此。您的代码中有一些(对我来说)不熟悉的东西,所以我不能肯定地说,但如果您简单地检查并确保您的变量与 Box2D 函数的返回类型匹配,它可能会工作得很好。

I'm just starting out with Box2D myself. So far as I understand the library, the primary means of getting the shapes of bodies is through their fixtures. From the fixture you get a b2Shape pointer - but, because its methods are virtual, you'll probably need to cast it as a b2PolygonShape/b2CircleShape pointer for it to be useful. Here's some code along those lines:

void DoStuffWithShapes(b2World *World)
{
    b2Body * B = World->GetBodyList();
    while(B != NULL)
    {
        b2Fixture* F = B->GetFixtureList();             
        while(F != NULL)
        {
            switch (F->GetType())
            {
                case b2Shape::e_circle:
                {
                    b2CircleShape* circle = (b2CircleShape*) F->GetShape();                     
                    /* Do stuff with a circle shape */
                }
                break;

                case b2Shape::e_polygon:
                {
                    b2PolygonShape* poly = (b2PolygonShape*) F->GetShape();
                    /* Do stuff with a polygon shape */
                }
                break;
            }
            F = F->GetNext();
        }

        B = B->GetNext();
    }       
}

Some other things to note: the .getNext() function of b2Body returns a pointer - this is an implementation of a linked list. The same is true for b2Fixture::GetNext(). There's some unfamiliar stuff (to me) in your code, so I can't say for sure, but it might work fine if you simply go through and make sure your variables match up with the return types of the Box2D functions.

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