Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an object rotating outside a circle

I tried to create an object rotating outside the circle, but it turns it inside the circle.

This is what I want to do (my character):

enter image description here

 func moveClockWise() {

            var dx = Character.position.x - self.frame.width / 2
            var dy = Character.position.y - self.frame.height / 2

            var rad = atan2(dy, dx)

            Path = UIBezierPath(arcCenter: CGPoint(x: self.frame.width / 2, y: self.frame.height / 2), radius: 150, startAngle: rad, endAngle: rad + CGFloat(M_PI * 4), clockwise: true)
            let follow = SKAction.followPath(Path.CGPath, asOffset: false, orientToPath: true, speed: 200)
            Character.runAction(SKAction.repeatActionForever(follow))
    }

but this what happened :

enter image description here

like image 546
omer15 Avatar asked Jul 04 '26 13:07

omer15


1 Answers

I'm not going about it the exact way you are.. but this is what i came up with. just copy paste the code and try it out :)

enter image description here

class GameScene: SKScene {

    let sprite = SKSpriteNode(color: SKColor.redColor(), size: CGSize(width: 20, height: 30))
    let circleRadius = CGFloat(100)

    var angleRelatedToCircle = CGFloat(0)
    let rotationSpeed = CGFloat(0.01)

    override func didMoveToView(view: SKView) {

        let topOfSprite = SKSpriteNode(color: SKColor.blueColor(), size: CGSize(width: 20, height: 2))
        topOfSprite.position.y = sprite.size.height
        sprite.addChild(topOfSprite)
        sprite.anchorPoint.y = 0
        sprite.zRotation = CGFloat(-M_PI_2)

        let circle = SKShapeNode(circleOfRadius: circleRadius)
        circle.fillColor = SKColor.whiteColor()
        circle.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
        self.addChild(circle)
        circle.addChild(sprite)

        sprite.position.y += circleRadius
    }


    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        sprite.zRotation += CGFloat(M_PI)
    }

    override func update(currentTime: NSTimeInterval) {
        angleRelatedToCircle -= rotationSpeed
        sprite.zRotation -= rotationSpeed

        sprite.position.x = circleRadius * cos(angleRelatedToCircle)
        sprite.position.y = circleRadius * sin(angleRelatedToCircle)
    }
}
like image 54
hamobi Avatar answered Jul 06 '26 05:07

hamobi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!