本文介绍了沿其运动方向旋转对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种相对简单的方法来旋转 SKSpriteNode 使其始终面向移动的方向?我的班级 Gamescene 有一个对象系统,它们具有相对的物理体,当它们碰撞时,它们会相互施加脉冲.这使得准确跟踪物体移动的方向变得相对困难.一些物体的形状也不规则,这使得事情变得更加混乱.我尝试过使用三角函数(例如反正切),但它只能在非常特殊的情况下按预期工作.是否有办法找出 SKSpriteNode 轨迹的角度,以便我可以将对象旋转到该角度?感谢您的任何帮助,您可以提供!

Is there a relatively simple way to rotate SKSpriteNode so that it is always facing the direction it is moving? My class Gamescene has a system of objects with their relative physicsbodies that apply impulses on each other when they collide. This makes it relatively difficult to keep track of exactly what direction an object is moving in. Some of the objects are also not regularly shaped, which makes things a bit more chaotic. I have tried using trigonometry (e.g. arctangent), but it only works as intended in very special circumstances. Might there be a way to find out the angle of an SKSpriteNode's trajectory, so that I can rotate the object to that angle? Thanks for any help you can provide!

我为 CGVector 创建了一个扩展,并尝试在两个对象发生碰撞时沿其速度方向旋转 SKSpriteNode(感谢 0x141E 的想法),但它似乎无法正常工作

I created an extension for CGVector and tried to rotate the SKSpriteNode in the direction of its velocity (Thanks to 0x141E for the idea) once two objects collided, but it doesn't seem to be working properly

func didBeginContact(contact: SKPhysicsContact) {
    var firstBody : SKPhysicsBody = contact.bodyA
    var secondBody : SKPhysicsBody = contact.bodyB
    if ((firstBody.categoryBitMask == PhysicsCatagory.Blade) && (secondBody.categoryBitMask == PhysicsCatagory.Laser))
    {
        if (secondBody.node != nil)
        {
            if (saberAngle <= 0 && LCR == 1) // Blade pointing right
            {
                let rebound = CGVectorMake(-reboundStrength*sin(1.5707 + saberAngle), reboundStrength*cos(1.5707 + saberAngle))
                secondBody.applyImpulse(rebound)
            }
            else if (saberAngle > 0 && LCR == -1) // Blade pointing left
            {
                let rebound = CGVectorMake(reboundStrength*sin(1.5707 - saberAngle), reboundStrength*cos(1.5707 - saberAngle))
                secondBody.applyImpulse(rebound)
            }
            if (secondBody.velocity.speed() > 0.01){
                (secondBody.node as! SKSpriteNode).zRotation = secondBody.velocity.angle() - offset
            }
        }
    }
}

我没有在 didSimulatePhysics() 方法中执行此操作,因为有时我尝试旋转的节点不存在,并且我会收到诸如使用未解析的标识符‘sprite’"之类的错误

I didn't do it in the didSimulatePhysics() method because there would be times when the node I was trying to rotate would not exist, and I would get errors like "Use of unresolved identifier 'sprite'"

我通过简单地创建和运行一个名为 rotate 的动作来实现它,该动作将精灵旋转到 secondBody.velocity.angle() - 偏移量,但我仍然很好奇为什么我的代码在此之前没有工作^^

I got it to work by simply creating and running an action called rotate that rotates the sprite to secondBody.velocity.angle() - offset, but I'm still curious as to why my code wasn't working before ^^

推荐答案

沿运动方向旋转精灵相当简单.您可以使用 atan2 函数将精灵速度的 xy 分量转换为角度.您应该仅在精灵的速度大于某个标称值时旋转精灵,以防止精灵在精灵的速度(几乎)为零时重置为零度.

It's fairly straightforward to rotate a sprite in the direction of its motion. You can do this by converting the x and y components of the sprite's velocity into an angle with the atan2 function. You should rotate the sprite only when its speed is greater than some nominal value to prevent the sprite from resetting to zero degrees when the sprite's speed is (nearly) zero.

如果我们扩展 CGVector 以通过速度的分量计算速度和角度

If we extend CGVector to compute speed and angle from the velocity's components by

extension CGVector {
    func speed() -> CGFloat {
        return sqrt(dx*dx+dy*dy)
    }
    func angle() -> CGFloat {
        return atan2(dy, dx)
    }
}

我们可以使用

override func didSimulatePhysics() {
    if let body = sprite.physicsBody {
        if (body.velocity.speed() > 0.01) {
            sprite.zRotation = body.velocity.angle() - offset
        }
    }
}

其中 offset = CGFloat(M_PI_2) 如果您的精灵在 zRotation 为零时朝上,如果您的精灵面向zRotation 为零时的右侧.

where offset = CGFloat(M_PI_2) if your sprite faces up when zRotation is zero, and offset = 0 if your sprite faces to the right when zRotation is zero.

这篇关于沿其运动方向旋转对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 08:39
查看更多