【问题标题】:How to get direction of a PhysicsBody SKNode in spritekit如何在 spritekit 中获取 PhysicsBody SKNode 的方向
【发布时间】:2015-08-10 20:54:31
【问题描述】:

我有一个施加了脉冲的 SKNode。有没有办法每次都知道它的方向?

它与许多物体发生碰撞,我想实时知道他的方向。

【问题讨论】:

    标签: swift sprite-kit skphysicsbody


    【解决方案1】:

    试试这个:

     yourPhysicsBody.velocity
    

    这应该返回物理体在任何给定时间的速度 CGVector。

    【讨论】:

    • 我要做的是保存方向和速度以应用于我的 SKScene 中的另一个 SKNode。
    • 应该这样做:anotherPhysicsBody.velocity = yourPhysicsBody.velocity
    【解决方案2】:

    首先,获取精灵速度的 x 和 y 分量

    let dx = sprite.physicsBody!.velocity.dx
    let dy = sprite.physicsBody!.velocity.dy
    

    然后计算精灵的方向(以弧度为单位)

    let angle = atan2(dy, dx)
    

    或者,你可以改变精灵的方向来面对它的运动

    sprite.zRotation = angle
    

    【讨论】:

    • 不知道你的回答对不对,我已经用别人的回答解决了;)还是谢谢你的支持!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-20
    相关资源
    最近更新 更多