开发者

change centre of rotation box2d

I have a b2PolygonShape body and apply a force to it.

开发者_如何学C

_recbody->ApplyForce( b2Vec2(6.0, -6.0), _recbody->GetWorldCenter() );

I want to change the centre of rotation of the body to be more in the front of the body. So that applyForce and also collisions make the body not turn in the centre but more in the front.


I know this is quite an old post, but building from the first answer, instead of using SetAsBox(), you should use Set and define the vertices of the box. I found that SetAtBox creates from the center, towards all 4 sides using width and height, so no matter what you do, the center of gravity will be the center of the shape, but on the other hand, if you use Set, even if you're just drawing a box, you can limit it to one side only.

For example:

int32 count = 4;
b2Vec2 vertices[] = {b2Vec2(sprite.contentSize.width/PTM_RATIO, 0.0f / PTM_RATIO),
b2Vec2(0.0f / PTM_RATIO,0.0f / PTM_RATIO),
b2Vec2(0.0f / PTM_RATIO, -sprite.contentSize.height/PTM_RATIO),
b2Vec2(sprite.contentSize.width/PTM_RATIO, -sprite.contentSize.height/PTM_RATIO),
  };
spriteShape.Set(vertices,count);

This will make a shape with the center of gravity shifted towards the upper left corner because we only drew the polygon on the right side going down (Nothing past the [0,0] point).


Found this in this forum "The center of mass of a body will be set from the bodyDef.position.Set() coordinates, regardless of where you create the actual shape. If you offset the boxDef.SetAsBox() coordinates by the amount required, this should get the effect you're after."

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜