so all i want to do is rotate an SKSPriteNode by 90 degrees. just that. It should be simple yet my first approach, assuming it would be degrees, turns the object in completely the wrong diection. so i head to google > stackoverflow. plenty of answers to do with this, okay so ill try using M_PI or some variation. nope. 'Double' is not convertible to 'CGFloat'. google again. "Try using skaction with blah blah" nope.
how difficult can it be to rotate a sprite? or am i insane
This seems to work fine for me. Understanding conversions between radians and degrees is important.
http://en.wikipedia.org/wiki/Radian#Conversion_between_radians_and_degrees
sprite.zRotation = CGFloat(M_PI_2)
I'm assuming you're using Swift based on your "'Double' is not convertible..." comment.
The following rotates a sprite 90 degree counterclockwise:
sprite.runAction(SKAction.rotateByAngle(CGFloat(M_PI_2), duration: 1.0))
The following rotates a sprite 90 degree clockwise:
sprite.runAction(SKAction.rotateByAngle(CGFloat(-M_PI_2), duration: 1.0))
Another option would be to create an extension for Int:
extension Int
{
var deg2Rad : CGFloat
{
return CGFloat(self) * CGFloat(M_PI) / 180.0
}
}
then use it like this:
sprite.runAction(SKAction.rotateByAngle(180.deg2Rad, duration: 1.0))
Makes it more human-readable.
let threesixty:CGFloat = 360 * .pi / 180
Related
I'm trying to move a user position marker in the direction the camera is facing. Kind of like you would control a character in a game.
Since camera in MapKit is aligned north, I thought for moving forward I'd add some latitude degrees and then rotate the resulting point on camera angle.
I have some converters from meters to how many degrees is that:
class Converter {
fileprivate static let earthRadius = 6378000.0 // meter
class func latitudeDegrees(fromMeter meter: Double) -> Double {
return meter / earthRadius * (180 / Double.pi)
}
class func longitudeDegress(fromMeter meter: Double, latitude: Double) -> Double {
return meter / earthRadius * (180 / Double.pi) / cos(latitude * Double.pi / 180)
}
}
So for moving forward, my code looks like this:
let latitudeDelta = Converter.latitudeDegrees(fromMeter: speed)
let y = userLocation.latitude + latitudeDelta
let x = userLocation.longitude
let a = -self.mapView.camera.heading * Double.pi / 180
self.userLocation = CLLocationCoordinate2D(latitude: x*sin(a) + y*cos(a), longitude:x*cos(a) - y*sin(a))
I've tried different approaches, and none seem to work. Is there something fundamentally wrong? Could it be that I also need to consider Earth curvature in the calculations?
After some more struggling, I found out that this problem is called "Direct Geodesic Problem". Here I found all the formulas I needed, but in the end used a great C library.
I have an app which animates a needle on a meter as long as the user is pressing on the screen. When the finger is lifted I need to know the rotation angle of the needle. I remove all animations as soon as the finger is lifted but I can't figure how to get the current rotation angle of the needle.
It is quite simple, this is the full solution:
Sample Setup:
imageView.transform = CGAffineTransform(rotationAngle: CGFloat.pi / 6) // just to test (it is 30 in degrees and 0.523598775598299 in radians)
Code:
let rad: Double = atan2( Double(imageView.transform.b), Double(imageView.transform.a))
let deg: CGFloat = CGFloat(rad) * (CGFloat(180) / CGFloat.pi )
print(deg) // works, printing 30
where deg = degrees and rad = radians
Explanation:
The first line is getting the radians, and the second line is multiplying the radians by the equivalent of a radian in degrees, to get the degrees.
NOTES:
In CGAffineTransform(rotationAngle: someValue), someValue is, in fact, the radians of the angle, it is not measured in degrees. More information about:
radian
degree
PI
The value in degrees of the radian CGFloat.pi is 180, therefore you can test it for any angle depending on this.
Let me know if this helps!
Hi I'm trying to rotate a 3D object in scenekit with no success heres my code:
let rotateAction = SCNAction.rotateByAngle(90, aroundAxis: SCNVector3Make(0, 1, 0), duration: 3)
let moveAction = SCNAction.moveByX(25, y: 0, z: 0, duration: 6)
ship.runAction(rotateAction, completionHandler: {ship.runAction(moveAction)})
I have managed to get it rotating on the correct axis but for some reason its not rotating by the 90 degrees that I've stated it just spins numerous times for the 3 seconds. I appreciate any help thanks.
the angle for rotateByAngle is in radians, so for 90 degrees you'd have to make the angle 1.571 radians.
if you'd like to be more thorough, add a little function to convert degrees to radians. it also will make the code easier to understand in the future.
func degToRadians(degrees:Double) -> Double
{
return degrees * (M_PI / 180);
}
SCNAction Class Reference
I want to give Force to my SKSpriteNode at specific angle.
So, How to treat my CGVector for give force at specific angle?
I had searched for it but unfortunately not getting any good way.
What i wants to achieve :
My SKSpriteNode moving towards the screen. There are buttons on top like 30,45,60.
So if user press button(i.e. that Button contain "30") then i had to move my SKSpriteNode to 30 degree with same speed.
Please help me towards it if any of you can help me regarding this.
First, you will need to convert the angle in degrees to radians by multiplying it by pi / 180
CGFloat angleInRadians = angleInDegrees * M_PI / 180;
You can then determine the vector components in that direction by
CGFloat dx = cosf(angleInRadians);
CGFloat dy = sinf(angleInRadians);
and finally apply a force to the sprite with
[sprite.physicsBody applyForce:CGVectorMake(dx*scale, dy*scale)];
where scale determines how much force is applied.
Optionally, you can rotate the sprite to face in the same direction as its motion by
sprite.zRotation = angleInRadians + offset;
where offset is the difference in angle, in radians, between your sprite's image and zero degrees. For example, if your sprite is facing up when zRotation is zero, offset should be -M_PI_2.
What do I have to do, if I need to rotate a UIImageView? I have a UIImage which I want to rotate by 20 degrees.
The Apple docs talk about a transformation matrix, but that sounds difficult. Are there any helpful methods or functions to achieve that?
If you want to turn right, the value must be greater than 0 if you want to rotate to the left indicates the value with the sign "-". For example -20.
CGFloat degrees = 20.0f; //the value in degrees
CGFloat radians = degrees * M_PI/180;
imageView.transform = CGAffineTransformMakeRotation(radians);
Swift 4:
let degrees: CGFloat = 20.0 //the value in degrees
let radians: CGFloat = degrees * (.pi / 180)
imageView.transform = CGAffineTransform(rotationAngle: radians)
A transformation matrix is not incredibly difficult. It's quite simple, if you use the supplied functions:
imgView.transform = CGAffineTransformMakeRotation(.34906585);
(.34906585 is 20 degrees in radians)
Swift 5:
imgView.transform = CGAffineTransform(rotationAngle: .34906585)
Swift version:
let degrees:CGFloat = 20
myImageView.transform = CGAffineTransformMakeRotation(degrees * CGFloat(M_PI/180) )
Swift 4.0
imageView.transform = CGAffineTransform(rotationAngle: CGFloat(20.0 * Double.pi / 180))
Here's an extension for Swift 3.
extension UIImageView {
func rotate(degrees:CGFloat){
self.transform = CGAffineTransform(rotationAngle: degrees * CGFloat(M_PI/180))
}
}
Usage:
myImageView.rotate(degrees: 20)
_YourImageView.transform = CGAffineTransformMakeRotation(1.57);
where 1.57 is the radian value for 90 degree
This is an easier formatting example (for 20 degrees):
CGAffineTransform(rotationAngle: ((20.0 * CGFloat(M_PI)) / 180.0))
As far as I know, using the matrix in UIAffineTransform is the only way to achieve a rotation without the help of a third-party framework.