我试图在 Touches Ended 方法上从左到右方向用力射箭。我向 B2Body 施加了线性脉冲,但它没有朝正确的方向射击。这是我的代码
(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"ended");
//Add a new body/atlas sprite at the touched location
CGPoint location;
for( UITouch *touch in touches )
{
location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL: location];
}
if (fabs(ccpDistance(startTouchPosition, location)) < 3) {
return;
}
CGPoint diff = ccpSub(startTouchPosition, location);
diff = CGPointMake(-diff.x, -diff.y);
//CGFloat distanceX = bird.position.x - 50.0;
// CGFloat distanceY = bird.position.y - 160.0;
CGFloat velocityX = diff.x*-1/5;
CGFloat velocityY = diff.y*-1/5;
b2Vec2 birdVelocity = b2Vec2(velocityX,velocityY);
//make b2Body
CGFloat sphereX = arrow.position.x/PTM_RATIO;
CGFloat sphereY = arrow.position.y/PTM_RATIO;
b2BodyDef bodyDef;
bodyDef.position.Set(sphereX,sphereY);
bodyDef.type = b2_dynamicBody;
int num = 6;
b2Vec2 verts[] = {
b2Vec2(-56.5f / PTM_RATIO, 0.8f / PTM_RATIO),
b2Vec2(-51.9f / PTM_RATIO, -2.6f / PTM_RATIO),
b2Vec2(53.7f / PTM_RATIO, -2.2f / PTM_RATIO),
b2Vec2(58.7f / PTM_RATIO, 0.7f / PTM_RATIO),
b2Vec2(52.5f / PTM_RATIO, 3.0f / PTM_RATIO),
b2Vec2(-55.0f / PTM_RATIO, 1.2f / PTM_RATIO)
};
b2PolygonShape polyShape;
polyShape.Set(verts, num);
b2FixtureDef fixtureDef;
fixtureDef.shape= &polyShape;
fixtureDef.density=4;
fixtureDef.restitution=0.1;
fixtureDef.friction=0.5;
b2Body * physicsBird = world->CreateBody(&bodyDef);
physicsBird->CreateFixture(&fixtureDef);
physicsBird->GetUserData();
arrow.rotation=atan2f(arrow.position.x-startTouchPosition.x,arrow.position.y-startTouchPosition.y)*180/M_PI +90;
direction = arrow.rotation;
b2Vec2 force = b2Vec2(direction, 24.8);
physicsBird->ApplyLinearImpulse(force, physicsBird->GetWorldCenter());
physicsBird->SetLinearDamping(0.4);
}
但它不起作用。请帮忙!