开发者

Box2D get shape of my bodies

开发者 https://www.devze.com 2023-03-02 05:07 出处:网络
Body b; while ((b=box2d.physics.PhysicssWorld.world.getBodyList().getNext())!=null) { Shape shape; while ((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().开发者_Go百科getNext()) always null...


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消