我创建了两个b2body
,我想在特定点周围移动一个身体。为此,我创建了两个实体并在这些实体之间创建了b2revoluteJoint
。但它不会产生预期的效果。
以下是我的代码:
-(id) init
{
if( (self=[super init])) {
self.touchEnabled = YES;
CGSize winSize = [[CCDirector sharedDirector] winSize];
b2Vec2 gravity = b2Vec2(0.0f, 0.0f);
bool allowBodiesToSleep = true;
world = new b2World(gravity);
world->SetAllowSleeping(allowBodiesToSleep);
world->SetContinuousPhysics(true);
_debugDraw = new GLESDebugDraw( PTM_RATIO );
world->SetDebugDraw(_debugDraw);
uint32 flags = 0;
flags += b2Draw::e_shapeBit;
flags += b2Draw::e_jointBit;
flags += b2Draw::e_aabbBit;
flags += b2Draw::e_pairBit;
flags += b2Draw::e_centerOfMassBit;
_debugDraw->SetFlags(flags);
// Arm
arm = [CCSprite spriteWithFile:@"arm.png"];
[self addChild:arm];
[arm setPosition:ccp(winSize.width/2, winSize.height/2)];
b2BodyDef spriteBodyDef;
spriteBodyDef.linearDamping = 1;
spriteBodyDef.angularDamping = 1;
spriteBodyDef.type = b2_dynamicBody;
spriteBodyDef.position.Set(arm.position.x/PTM_RATIO, arm.position.y+1/PTM_RATIO);
spriteBodyDef.userData = (__bridge void *)arm;
spriteBody = world->CreateBody(&spriteBodyDef);
b2PolygonShape spriteShape;
spriteShape.SetAsBox(arm.contentSize.width/PTM_RATIO/2, arm.contentSize.height/PTM_RATIO/2);
b2FixtureDef spriteShapeDef;
spriteShapeDef.shape = &spriteShape;
spriteBody->CreateFixture(&spriteShapeDef);
monster = [CCSprite spriteWithFile:@"body.png"];
[monster setPosition:ccp(winSize.width*0.37, winSize.height/2)];
[self addChild:monster];
b2BodyDef monsterBodyDef;
monsterBodyDef.position.Set(monster.position.x/PTM_RATIO, monster.position.y/PTM_RATIO);
monsterBodyDef.userData = (__bridge void *)monster;
monsterBody = world->CreateBody(&monsterBodyDef);
b2PolygonShape monsterShape;
monsterShape.SetAsBox(monster.contentSize.width/PTM_RATIO/2, monster.contentSize.height/PTM_RATIO/2);
b2FixtureDef monsterShapeDef;
monsterShapeDef.shape = &monsterShape;
monsterBody->CreateFixture(&monsterShapeDef);
b2RevoluteJointDef armJointDef;
armJointDef.Initialize(monsterBody, spriteBody, b2Vec2(monster.position.x/PTM_RATIO,monster.position.y/PTM_RATIO));
armJointDef.enableMotor = false;
armJointDef.enableLimit = true;
armJointDef.localAnchorA = b2Vec2(0, 0);
armJointDef.localAnchorB = b2Vec2(1, 0);
armJointDef.motorSpeed = -10;
armJointDef.lowerAngle = CC_DEGREES_TO_RADIANS(9);
armJointDef.upperAngle = CC_DEGREES_TO_RADIANS(75);
armJointDef.maxMotorTorque = 100;
armJoint = (b2RevoluteJoint*)world->CreateJoint(&armJointDef);
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(monster.position.x/PTM_RATIO, monster.position.y/PTM_RATIO);
//or make it static bodyDef.type = b2_staticBody;
b2Body * body = world->CreateBody(&bodyDef);
//create circle shape
b2CircleShape circle;
circle.m_radius = 20.0/PTM_RATIO;
//define fixture
b2FixtureDef fixtureDef;
fixtureDef.shape = &circle;
fixtureDef.density = 1;
fixtureDef.restitution = 0.7;
fixtureDef.friction = 0.4;
body->CreateFixture(&fixtureDef);
}
return self;
}
触摸移动身体的功能:
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (mouseJoint != nil) return;
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
monster.visible = NO;
if (locationWorld.x < spriteBody->GetWorldCenter().x + 50.0/PTM_RATIO)
{
b2MouseJointDef md;
md.bodyA = monsterBody;
md.bodyB = spriteBody;
md.target = locationWorld;
md.maxForce = 2;
mouseJoint = (b2MouseJoint *)world->CreateJoint(&md);
}
}
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
spriteBody->SetTransform(locationWorld, angle);
}
我在Box2d中是全新的,所以如果有人做过这样的话。请解释我。 谢谢:))