Skip to content

Instantly share code, notes, and snippets.

@t-kashima
Created June 30, 2013 13:25
Show Gist options
  • Save t-kashima/5895135 to your computer and use it in GitHub Desktop.
Save t-kashima/5895135 to your computer and use it in GitHub Desktop.
I add this method in GB2ShapeCache-x.cpp. Because it don't think a sprite scale.
void GB2ShapeCache::addFixturesToBodyForSprite(b2Body *body, const std::string &shape, CCSprite *sprite) {
std::map<std::string, BodyDef *>::iterator pos = shapeObjects.find(shape);
BodyDef *so = (*pos).second;
FixtureDef *fix = so->fixtures;
if ((sprite->getScaleX() == 1.0f) && (sprite->getScaleY() == 1.0f)) {
// simple case - so do not waste any energy on this
while(fix) {
body->CreateFixture(&fix->fixture);
fix = fix->next;
}
} else {
b2Vec2 vertices[b2_maxPolygonVertices];
while(fix) {
// make local copy of the fixture def
b2FixtureDef fix2 = fix->fixture;
// get the shape
const b2Shape *s = fix2.shape;
// clone & scale polygon
const b2PolygonShape *p = dynamic_cast<const b2PolygonShape*>(s);
if(p)
{
b2PolygonShape p2;
for(int i=0; i<p->m_vertexCount; i++)
{
vertices[i].x = p->m_vertices[i].x * sprite->getScaleX();
vertices[i].y = p->m_vertices[i].y * sprite->getScaleY();
}
p2.Set(vertices, p->m_vertexCount);
fix2.shape = &p2;
}
// clone & scale circle
const b2CircleShape *c = dynamic_cast<const b2CircleShape *>(s);
if(c) {
b2CircleShape c2;
c2.m_radius = c->m_radius * sprite->getScale();
c2.m_p.x = c->m_p.x * sprite->getScaleX();
c2.m_p.y = c->m_p.y * sprite->getScaleY();
fix2.shape = &c2;
}
// add to body
body->CreateFixture(&fix2);
fix = fix->next;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment