This topic is solved.
Solution code :
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 :
Get work correctely.
But Set dont work because shape's center is calculate with function of shape.
Example :
Them i can not move a shape/fixture.
Any solutions folks?
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());
}
{
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;
}
}
{
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);
}
{
...
m_centroid = ComputeCentroid(m_vertices, m_vertexCount);
}
Them i can not move a shape/fixture.
Any solutions folks?