我正在尝试使用b2PrismaticJoint和b2MouseJoint。我需要沿x轴移动我的射弹以将其定位到目标,并且只想垂直滑动而不移动射弹将其抛向该方向。我正在使用ApplyLinearImpulse(),但无论我向哪个方向滑动,它的方向始终朝向右上方。代码是:
- (void)ccTouchesEnded:(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);
if (_mouseJoint) {
_world->DestroyJoint(_mouseJoint);
_mouseJoint = NULL;
}
if(_primJoint) {
_world->DestroyJoint(_primJoint);
_primJoint = NULL;
}
if (hit) {
_strikerBody->ApplyLinearImpulse(locationWorld, _strikerBody->GetPosition());
}
}
答案 0 :(得分:1)
看起来您的locationWorld
向量每次都指向相同的方向。我想你想要这样的东西:
b2Vec2 impulseDirection = locationWorld - _strikerBody->GetPosition();
impulseDirection.Normalize();
const double Force = 10 * _strikerBody->GetMass(); //or anything you want
_strikerBody->ApplyLinearImpulse( Force*impulseDirection, _strikerBody->GetPosition() );
现在,脉冲将应用于触摸方向_strikerBody
的中心(相对于_strikerBody
)。