[Solved]Get and Set position to fixture/shape

This topic is solved.

Solution code :
Code:
inline void MovePolygonShape(b2PolygonShape* polygon,b2Vec2 move)
{
    for(int32 i=0;i<polygon->m_vertexCount;i++)
    {
        polygon->m_vertices[i]+=move;
    }
    polygon->m_centroid+=move;
}

inline b2Vec2 getShapePosition(const b2Shape *shape)
{
    if(shape->m_type == b2Shape::e_circle)
    {
        return static_cast<const b2CircleShape*>(shape)->m_p;
    }
    else if(shape->m_type == b2Shape::e_polygon)
    {
        return static_cast<const b2PolygonShape*>(shape)->m_centroid;
    }
    return b2Vec2_zero;
}

inline void setShapePosition(b2Shape *shape,b2Vec2 pos)
{
    if(shape->m_type == b2Shape::e_circle)
    {
        static_cast<b2CircleShape*>(shape)->m_p = pos;
    }
    else if(shape->m_type == b2Shape::e_polygon)
    {
        b2PolygonShape* polygon = static_cast<b2PolygonShape*>(shape);
        MovePolygonShape(polygon,pos-polygon->m_centroid);
    }
}

//After call, if mass can be cahge, use body->ResetMassData();
inline void setFixturePosition(b2Fixture *fixture,b2Vec2 pos)
{
    setShapePosition(fixture->GetShape(),pos);
    //It is necessary to recalculate AABB box of fixture
    //Because it is not automatic if body is static.
    bool active = fixture->GetBody()->IsActive();
    fixture->GetBody()->SetActive(!active);
    fixture->GetBody()->SetActive(active);
}

inline b2Vec2 getFixturePosition(const b2Fixture *fixture)
{
    return getShapePosition(fixture->GetShape());
}

inline b2Vec2 getFixtureWorldPosition(const b2Fixture *fixture)
{
    return fixture->GetBody()->GetPosition()+getShapePosition(fixture->GetShape());
}


Original message =>

Suit of viewtopic.php?f=3&t=6365&hilit=b2Fixture+change+position

I want create 2 fixture with same b2FixtureDef, but i need also change second fixture's position.

I think at this bad code :
Code:
inline b2Vec2 getShapePosition(const b2Shape *shape)
{
    if(shape->m_type == b2Shape::e_circle)
    {
        return static_cast<const b2CircleShape*>(shape)->m_p;
    }
    else if(shape->m_type == b2Shape::e_polygon)
    {
        return static_cast<const b2PolygonShape*>(shape)->m_centroid;
    }
    return b2Vec2_zero;
}

inline void setShapePosition(b2Shape *shape,b2Vec2 pos)
{
    if(shape->m_type == b2Shape::e_circle)
    {
        static_cast<b2CircleShape*>(shape)->m_p = pos;
    }
    else if(shape->m_type == b2Shape::e_polygon)
    {
        static_cast<b2PolygonShape*>(shape)->m_centroid = pos;
    }
}


Get work correctely.
But Set dont work because shape's center is calculate with function of shape.
Example :
Code:
void b2PolygonShape::Set(const b2Vec2* vertices, int32 count)
{
...
m_centroid = ComputeCentroid(m_vertices, m_vertexCount);
}

Them i can not move a shape/fixture.

Any solutions folks?
Fixtures don't really have a position, they only have vertices. To move the shape, you will need to move all the vertices.
Maybe it is more efficase destroy and recreate shape??? If both b2Shape have same shape, maybe mass isn't modify ❓

Quote:
Fixtures don't really have a position, they only have vertices. To move the shape, you will need to move all the vertices.

b2CircleShape dont have vertice. Then it is a little more difficult 😕

If Box2D dont provide this fonctionnality, maybe it has good reason and it is not good idea change shape/fixture position in body.
Destroying and recreating the shape is the most fail-safe method, but it may be quite awkward if you are using the b2Fixture* pointers in other parts of the program, for example to compare with in your collision callbacks.

You can also poke into the shapes themselves and move the vertices (or center point for a circle) directly: http://stackoverflow.com/questions/6618 ... y-in-box2d
That way you can keep the same fixture pointers.
If you move the vertices/center directly then you should call ResetMassData() on the body afterwards.
With your help, i have provided this code

Code:
#ifndef HEADER_PHYSIC_FONCTION
#define HEADER_PHYSIC_FONCTION

#include <Box2D/Box2D.h>

inline void MovePolygonShape(b2PolygonShape* polygon,b2Vec2 move)
{
    for(int32 i=0;i<polygon->m_vertexCount;i++)
    {
        polygon->m_vertices[i]+=move;
    }
    polygon->m_centroid+=move;
}

inline b2Vec2 getShapePosition(const b2Shape *shape)
{
    if(shape->m_type == b2Shape::e_circle)
    {
        return static_cast<const b2CircleShape*>(shape)->m_p;
    }
    else if(shape->m_type == b2Shape::e_polygon)
    {
        return static_cast<const b2PolygonShape*>(shape)->m_centroid;
    }
    return b2Vec2_zero;
}

inline void setShapePosition(b2Shape *shape,b2Vec2 pos)
{
    if(shape->m_type == b2Shape::e_circle)
    {
        static_cast<b2CircleShape*>(shape)->m_p = pos;
    }
    else if(shape->m_type == b2Shape::e_polygon)
    {
        b2PolygonShape* polygon = static_cast<b2PolygonShape*>(shape);
        MovePolygonShape(polygon,pos-polygon->m_centroid);
    }
}

inline void setFixturePosition(b2Fixture *fixture,b2Vec2 pos)
{
    setShapePosition(fixture->GetShape(),pos);
}

inline b2Vec2 getFixturePosition(const b2Fixture *fixture)
{
    return getShapePosition(fixture->GetShape());
}

inline b2Vec2 getFixtureWorldPosition(const b2Fixture *fixture)
{
    return fixture->GetBody()->GetPosition()+getShapePosition(fixture->GetShape());
}

#endif


It work correctely when i move shape before fixture are created :
Code:
    //Create fixture 1
    b2FixtureDef fixture_def = engine.p_factory.getFixtureDef("Floor");
    fixture_def.userData = this;
    fixture_def.filter.categoryBits = this->type;
    fixture1 = body->CreateFixture(&fixture_def);
    //Create fixture 2
    setShapePosition(const_cast<b2Shape*>(fixture_def.shape),b2Vec2(6,0));
    fixture2 = body->CreateFixture(&fixture_def);
    //setFixturePosition(fixture2,b2Vec2(6,0));
    body->ResetMassData();


But when i move shape after fixture are created :
Code:
    //Create fixture 2
    //setShapePosition(const_cast<b2Shape*>(fixture_def.shape),b2Vec2(6,0));
    fixture2 = body->CreateFixture(&fixture_def);
    setFixturePosition(fixture2,b2Vec2(6,0));
    body->ResetMassData();


DebugDraw draw shape in new position, but collision dont work 😮
Collision dont work also on old position.
Other fixture of body work correctly(no move).

Maybe it is imposible move fixture after created, else Box2D provide this fonctionality.

I can move b2FixtureDef and it is base problem. But i dont undertand why fixture loses collision and shape is correctly draw.
Maybe AABB who are not recalculate 🙁

Other solution, it is save b2FixtureDef, destroy fixture, move b2FixtureDef, recreate fixture. A little more difficult 😆
But b2Fixture::m_shape is const, then it is not good idea maybe.
oh... are these shapes on static bodies?
I seem to recall some problems with the AABB not being recalculated for static bodies.
Maybe this might work:
Code:
    //recalculate AABB - is there a better way?
    bool active = body->IsActive();
    body->SetActive(!active);
    body->SetActive(active);
All right, all conclusions are just. And this code work.

Thank to help. I post final code on the first mesage.
You may experience drift if you don't keep an original copy of the vertices around and always apply an offset to them. Its the physics engine version of the problem of making too many copies of a key or converting music across several file formats back to back.

Original thread: https://box2d.org/forum/viewtopic.php?t=8846