如何围绕圆圈旋转三角形是否正确?

时间:2016-02-20 20:35:35

标签: ios swift sprite-kit

我在圆圈周围有三角形,我创建了如果我的角色与砖碰撞,那么圆圈上的三角形将替换圆圈上的另一个位置。

但我有问题,三角形在发生时会旋转。

当我点击屏幕(那很好)但旋转时会发生这种情况。

它需要看起来像这样:

enter image description here

但它看起来像那样:

enter image description here

AddTriangleToCircle func:

func AddTriangleToCircle(Circle: SKShapeNode, Location: CGFloat, Inside: Bool) {

        Triangle = SKSpriteNode(imageNamed: "Triangle.png")

        Triangle.size = CGSize(width: 30, height: 30)
        Triangle.anchorPoint.y = 0



        let path = CGPathCreateMutable()

        CGPathMoveToPoint(path, nil,298,2)
        CGPathAddLineToPoint(path, nil,149,298)
        CGPathAddLineToPoint(path, nil,0,0)
        CGPathAddLineToPoint(path, nil,298,2)

        CGPathCloseSubpath(path)

        Triangle.physicsBody = SKPhysicsBody(polygonFromPath: path)


        if Inside == true {
            // Inside Triangle
            Triangle.zRotation = CGFloat(M_PI_2)
        } else {
            // Outside Triangle
            Triangle.zRotation = CGFloat(-M_PI_2)
        }

        Triangle.position = CGPoint(x:0.5, y:circleRadius)

        let rotationSpeed1 = rotationSpeed + Location;
        var angleRelatedToCircle1 = angleRelatedToCircle;

        angleRelatedToCircle1 -= rotationSpeed1
        Triangle.zRotation -= rotationSpeed1

        Triangle.position.x = circleRadius * cos(angleRelatedToCircle1)
        Triangle.position.y = circleRadius * sin(angleRelatedToCircle1)


        //Triangle.physicsBody = SKPhysicsBody(texture:TriangelSKT, size: CGSize(width: 30, height:30))
        let centerPoint = CGPointMake(Triangle.size.width / 2 - (Triangle.size.width * Triangle.anchorPoint.x), Triangle.size.height / 2 - (Triangle.size.height * Triangle.anchorPoint.y))
        Triangle.physicsBody = SKPhysicsBody(rectangleOfSize: Triangle.size , center: centerPoint)
        Triangle.physicsBody?.categoryBitMask = triangleCategory
        Triangle.physicsBody?.contactTestBitMask = heroCategory
        Triangle.physicsBody?.collisionBitMask = heroCategory
        Triangle.physicsBody?.allowsRotation = false
        // Set Dynamic to false
        Triangle.physicsBody?.dynamic = false
        Circle.addChild(Triangle);
    }

AddCircle func:

    func AddCircle() {

            Circle = SKShapeNode(circleOfRadius: circleRadius)
            Circle.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
            Circle.strokeColor = UIColor.whiteColor()
            Circle.lineWidth = 9.5
            Circle.fillColor = UIColor(red:0.98, green:0.82, blue:0.32, alpha:1.0)
            self.addChild(Circle)
            Circle.addChild(BooCharacter)



            self.AddTriangleToCircle(Circle, Location: CGFloat(random(1...100)), Inside: false)
            self.AddBrickToCircle(Circle, Location: CGFloat(random(1...200)), inside: true)
            self.AddTriangleToCircle(Circle, Location: CGFloat(random(1...400)), Inside: false)


}

touchsbegan func:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

            BooCharacter.zRotation += CGFloat(M_PI)
            TapToStart.removeFromParent()

             for touch in touches {
             let location = touch.locationInNode(self)



            }

          initializeValues()    
    }

initializeValues func:

func initializeValues() {
    self.removeAllChildren()
    AddCircle()  
    }

1 个答案:

答案 0 :(得分:0)

所以你需要的是根据角度在圆上找到两个点。

圆圈上的一个点就是这样 -

func pointOnCircle(radius:CGFloat, angleInRadians:CGFloat, circleCenter:CGPoint) -> CGPoint {

      return CGPoint(
                 x:circleCenter.x - radius * cos(angleInRadians), 
                 y:circleCenter.y - radius * sin(angleInRadians)
      )

}

如果需要,不要忘记将度数转换为弧度。

您需要定义一个角度(定义三角形的大小)和圆上的起点。

使用该角度在公式中找到圆上三角形的另一个基点,并且只使用较小的角度(原始角度的一半可能会很好)和较大的半径再次使用它,因为第三个可以认为三角形的点位于一个较大的圆圈(红色边框圆圈)上,它与原始圆圈(绿色圆圈)共享相同的中心点

enter image description here