问题描述
我想在一个基于physicsBody旋转的方向上使用applyImpulse方法移动一个physicsBody。
I want to move a physicsBody with the applyImpulse method in a direction based on the physicsBody rotation.
例如,physicsBody是一个方形的形状,我打电话一个移动,它将施加一个冲动,使其垂直向上移动。然后我调用一个方法将physicsBody旋转45度。如果我再次调用移动方法,物理对象将向右对角移动。
Foe example, the physicsBody is a square in shape, I call a "move" which will apply an impulse to make it move up vertically. I then call a method to rotate the physicsBody 45 degrees right. If I call the "move" method again, the physicsBody will move diagonally right and up.
任何人都可以帮忙吗?
推荐答案
我建议您遵循Sprite Kit的坐标和旋转约定。具体来说,您的精灵图像应该面向零度(默认值),正值是逆时针旋转。也就是说,这是在精灵面临的方向上施加冲动的一种方法:
I suggest that you follow Sprite Kit’s coordinate and rotation conventions. Specifically, your sprite image should be facing right at zero degrees (the default value), and a positive value is a counter-clockwise rotation. That said, here's one way to apply an impulse in the direction a sprite is facing:
// Specify the force to apply to the SKPhysicsBody
CGFloat r = 5;
// Create a vector in the direction the sprite is facing
CGFloat dx = r * cos (sprite.zRotation);
CGFloat dy = r * sin (sprite.zRotation);
// Apply impulse to physics body
[sprite.physicsBody applyImpulse:CGVectorMake(dx,dy)];
这篇关于SpriteKit使用applyImpulse移动了physicsBody的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!