79
« on: April 14, 2009, 05:15:23 pm »
I'm using it this way :
if((*shape).GetNbPoints()<=8)
{// box2D can handle shapes with 8 points max
b2PolygonDef shapeDef;
shapeDef.vertexCount = (*shape).GetNbPoints();
// we set the shape at 0,0 so that the global SFML coordinates fit the Box2D coordinates system
(*shape).SetPosition(0,0);
for(unsigned int j=0; j<(unsigned int)shapeDef.vertexCount; ++j)
{// for each point of the shape
shapeDef.vertices[j].Set(ToolBox::toMeter((*shape).TransformToGlobal((*shape).GetPointPosition(j)).x),ToolBox::toMeter((*shape).TransformToGlobal((*shape).GetPointPosition(j)).y));
}
// we set the common shape params
// set the box density
shapeDef.density = density;
// override the default friction
shapeDef.friction = friction;
// finally we add the shape to the body
this->myBody->CreateShape(&(shapeDef));
}
else
{// SFML circle has more than 8 points ... if your shape has more than 8 points it'll be considered as a circle
// circle definition
b2CircleDef circleShape;
float centerPosX, pointPosX, centerPosY, pointPosY;
centerPosX = (*shape).GetPosition().x; // we set the xpos of the center
centerPosY = (*shape).GetPosition().y; // we set the y pos of the center
pointPosX = (*shape).GetPointPosition(1).x; // xpos of a point of the circle
pointPosY = (*shape).GetPointPosition(1).y; // ypos of the same point
// we calculate the radius sqrt((XA-XB)²+(YA-YB)²)
circleShape.radius = ((centerPosX-pointPosX)*(centerPosX-pointPosX));
circleShape.radius += ((centerPosY-pointPosY)*(centerPosY-pointPosY));
circleShape.radius = ToolBox::toMeter(sqrt(circleShape.radius));
// we set the density
circleShape.density = density;
// we set the friction
circleShape.friction = friction;
// finally we create the shape from the circleShape
this->myBody->CreateShape(&(circleShape));
}