本文介绍了SpriteKit 使用 applyImpulse 移动旋转的物理体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 applyImpulse 方法在基于 PhysicsBody 旋转的方向上移动一个physicsBody.

I want to move a physicsBody with the applyImpulse method in a direction based on the physicsBody rotation.

例如,physicsBody 是一个正方形,我称之为移动",它会施加一个脉冲使其垂直向上移动.然后我调用一个方法将physicsBody 向右旋转45 度.如果我再次调用move"方法,physicsBody 将沿对角线向右和向上移动.

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 移动旋转的物理体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-24 06:50