Moving b2Body in given angle
I have b2Body of which i set angle using setTransform. But I want b2Body to move in particular angle. My CC开发者_如何学JAVASprite is moving in that angle using ccMoveBy. But my b2Body is not moving with that CCSprite. So I thought of using setTransform. I set the angle with the same value by which I'm moving CCSprite. But still b2Body moves somewhere else.
I have used it like
b->setTransform(b->getPosition(),30*DEG_TO_RED);
b->setLinearVelocity(b2Vec2(120,180));
Any help is grealy appreciated.
Thank you,.
If you want to move b2Body by yourself then it is a good idea to make it a kinematic body (you can change body's type every time you want). Then just set the velocity:
body->SetType(b2_kinematikBody);
float angle = 30*M_PI/180.0f;
float spd = 50;
b2Vec2 velocity = spd*b2Vec2(cos(angle), sin(angle));
body->SetLinearVelocity(velocity);
#define CC_DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) * 0.01745329252f) // PI / 180
this is for 30degree rotation of body and not change in its position.
b->setTransform(b->getPosition(),CC_DEGREES_TO_RADIANS(30));
b->setLinearVelocity(b2Vec2(120,180));
or
b->setTransform(b2Vec(100/PTMRATIO,200/PTMRATIO),CC_DEGREES_TO_RADIANS(30));
here i changed to 100 X 200Y POSITION
import "CGPointExtension.h"
b->setTransform(ccpAdd(b2Vec(100/PTMRATIO,200/PTMRATIO), b2Vec(50/PTMRATIO,50/PTMRATIO)),CC_DEGREES_TO_RADIANS(30));
精彩评论