0
화살표와 공이 있습니다. 화살표의 앵커 포인트는 하단에 있습니다. 필자가해야 할 일은 스프라이트가 터치로 회전 할 수있게 한 다음 선형 충격을 볼에 적용하고 화살표가 가리키는 방향으로 쏜다는 것입니다.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