0
我有一个箭头和一个球。箭头的anchorPoint位于底部。我需要做的就是使精灵能够随着触摸旋转,然后线性冲动施加到球上并沿箭头指向的方向射击。应用线性脉冲旋转Sprite-Cocos2d/Box2D
E.G.
假设您将箭头旋转70度,然后按开火按钮。球然后以70度角射门。
目前,这是我在用,但球不中箭头所指过的方向拍摄。
触摸旋转箭头。
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
//acquire the previous touch location
CGPoint firstLocation = [touch previousLocationInView:[touch view]];
CGPoint location = [touch locationInView:[touch view]];
//preform all the same basic rig on both the current touch and previous touch
CGPoint touchingPoint = [[CCDirector sharedDirector] convertToGL:location];
CGPoint firstTouchingPoint = [[CCDirector sharedDirector] convertToGL:firstLocation];
CGPoint firstVector = ccpSub(firstTouchingPoint, _arrow.position);
CGFloat firstRotateAngle = -ccpToAngle(firstVector);
CGFloat previousTouch = CC_RADIANS_TO_DEGREES(firstRotateAngle);
CGPoint vector = ccpSub(touchingPoint, _arrow.position);
CGFloat rotateAngle = -ccpToAngle(vector);
CGFloat currentTouch = CC_RADIANS_TO_DEGREES(rotateAngle);
//keep adding the difference of the two angles to the dial rotation
arrowRotation += currentTouch - previousTouch;
}
火按钮以箭头指向的方向射击球。
-(void) fireBall: (id)sender
{
[self unscheduleUpdate];
direction = arrowRotation;
b2Vec2 force = b2Vec2(direction, 24.8);
_ballBody->ApplyLinearImpulse(force, _ballBody->GetWorldCenter());
}
任何帮助,非常感谢! 在此先感谢!
如果我这样做,线性冲动不适用于球。 – Jonathan
你是怎么试图用你当前的代码来试用它的。你的旋转在一分钟不是一个向量,并没有一个x,y在我给你的代码中使用 –
我拥有的代码是我知道旋转精灵的唯一方法。我还在学习。你能不能展示一些示例代码? – Jonathan