Skip to content

Instantly share code, notes, and snippets.

@pontusarmini
Last active November 10, 2015 04:38
Show Gist options
  • Save pontusarmini/03c2aa6c981cc7c91d93 to your computer and use it in GitHub Desktop.
Save pontusarmini/03c2aa6c981cc7c91d93 to your computer and use it in GitHub Desktop.
A Cocos2d-iPhone/Obj-c/Spritebuilder action that rotates a node around a given point
/*
* Copyright (c) 2015 Pontus Armini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
class ActionRotateAround: CCActionInterval {
private var rotationPoint: CGPoint!
private var relativePoint: CGPoint!
private var sx: CGFloat!
private var sy: CGFloat!
private let relative: Bool
private var radianAngle: CGFloat!
private let angle: Float
private let clockWise: Bool
private var startPosition: CGPoint!
private var previousPosition: CGPoint!
init(absolutePosition point:CGPoint, duration: CCTime, angle: Float, clockWise: Bool) {
relative = false
rotationPoint = point
self.angle = angle
self.clockWise = clockWise
super.init(duration: duration)
setup(angle, clockWise: clockWise)
}
init(positionRelativeToTarget point:CGPoint, duration: CCTime, angle: Float, clockWise: Bool) {
relative = true
relativePoint = point
self.angle = angle
self.clockWise = clockWise
super.init(duration: duration)
setup(angle, clockWise: clockWise)
}
private func setup(angle: Float, clockWise: Bool) {
if(clockWise) {
radianAngle = -(CGFloat(M_PI) * CGFloat(angle) / 180)
} else {
radianAngle = (CGFloat(M_PI) * CGFloat(angle) / 180)
}
}
override func startWithTarget(target: AnyObject!) {
startPosition = (target as! CCNode).position
previousPosition = (target as! CCNode).position
if(relative) {
rotationPoint = ccpAdd(relativePoint, startPosition)
} else {
relativePoint = ccpSub(rotationPoint, startPosition)
}
sx = startPosition.x - rotationPoint.x
sy = startPosition.y - rotationPoint.y
super.startWithTarget(target)
}
override func update(time: CCTime) {
let t = CGFloat(time)
let cosines = cos(radianAngle * t)
let sines = sin(radianAngle * t)
var x:CGFloat, y:CGFloat
if (CC_ENABLE_STACKABLE_ACTIONS == 1) {
let currentPos = (target as! CCNode).position
let diff = ccpSub(currentPos, previousPosition)
startPosition = ccpAdd(startPosition, diff)
rotationPoint = ccpAdd(relativePoint, startPosition)
sx = startPosition.x - rotationPoint.x
sy = startPosition.y - rotationPoint.y
}
x = cosines * sx - sines * sy + rotationPoint.x
y = sines * sx + cosines * sy + rotationPoint.y
let newPosition = CGPoint(x: x, y: y)
(target as! CCNode).position = newPosition
previousPosition = newPosition
}
override func copyWithZone(zone: NSZone) -> AnyObject! {
if(relative) {
return ActionRotateAround(positionRelativeToTarget: relativePoint, duration: duration, angle: angle, clockWise: clockWise)
} else {
return ActionRotateAround(absolutePosition: rotationPoint, duration: duration, angle: angle, clockWise: clockWise)
}
}
override func reverse() -> CCActionInterval! {
if(relative) {
return ActionRotateAround(positionRelativeToTarget: relativePoint, duration: duration, angle: angle, clockWise: !clockWise)
} else {
return ActionRotateAround(absolutePosition: rotationPoint, duration: duration, angle: angle, clockWise: !clockWise)
}
}
}
@pontusarmini
Copy link
Author

Updated to be stackable, so that you can run other move actions simultaneously.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment