Tracking Node Orientation - swift

I'm currently working on an app and I have my Node rotating by angle every time I touch by using the TouchesBegan method. Now I've been trying to figure out if theres a way to tell what way a node is orientated?
For example if you have a square is there a way to give every side a diffrent value (1,2,3,4)? Can you tell what value is faceing down?
I was thinking if I could tell what angle the node has been rotated by
( one touch = 90 degrees / two touches = 180 degrees....)
I could use that value for features ill be needing in the future. However I don't know if that value is ever saved, or how to go about saving it
Thank you for any help!

To get the angle your SKSpriteNode is facing use the zRotation property on you SKSpriteNode. Bear in mind this is measured in radians, if you specifically need it in degrees you can convert from radians to degrees with the following code:
let degrees = sprite.zRotation * 180 / CGFloat(M_PI)
Alternatively, if all you wanted to do was know how many times the user had touched the screen - you could use a variable that you increment every time touchesBegan in called.
Hope that helps!

Related

How to find direction (in radians or degrees) of a physics body

I have a physics body that gets hit and rotates a lot. I want to limit the amount it can rotate (only 45 degrees in each direction or something) and I wanted to try to do that by applying torque if the direction goes past a certain point. I'm ok with the sprite wobbling back and forth (it's a bird).
I'm open to other suggestions on how to limit the rotation as well.
Thanks in advance!
Edit: I've already tried using zRotation (I printed it out and it was always 0.) I also tried using the vector but that gave me the direction the sprite was going.

Unity rotation issues

I'm trying to have a model rotate 90 degrees when a button is pressed - should be simple, right?
Well, the entire system is a buggy mess for some odd reason. I would appreciate some help fixing it
transform.parent.rotation = Quaternion.Euler(transform.parent.rotation.x, transform.parent.rotation.y , transform.parent.rotation.z);
Instead, the model just rotates in random directions that seem like they shouldn't at all be related to my code.
I started up the game to rotate the model while it's in play-mode, but the way it rotates seems like it just suddenly changes out of the blue.
I'm really confused by this & would appreciate some help in fixing it
You code doesnt work like your think.
Quaterinion.Euler expects input in the form of Euler angles, but you are inputing the (x,y,z) of a Quaterinon which consists of (x,y,z,w) which is why you get really funky rotation.
https://docs.unity3d.com/ScriptReference/Quaternion.html
To get the current Euler Angles of your transform, simply use transform.eulerAngles (or in your case, transform.parent.eulerAngles)
var euler = transform.parent.eulerAngles;
transform.parent.rotation = Quaternion.Euler(euler.x, euler.y, euler.z);
However this doesnt change the rotation in anyway.
If you want to rotate 90 degrees around the Y-axis, you could add 90 like this
var euler = transform.parent.eulerAngles;
transform.parent.rotation = Quaternion.Euler(euler.x, euler.y+90, euler.z);
An even simpler way to rotate 90 degrees around Y is ofcourse
transform.Rotate(0, 90, 0);

Unity3D manipulate object orientation with respect to original orientation

I encountered another issue with hand-object interaction using a Leap Motion device. In particular, I use the LM to perform orientation manipulations on virtual objects.
I want to use the difference between the current and last orientation of the hand to manipulate the object:
Quaternion relativeOrientationHands = Quaternion.Inverse(currentHandOrientation) * updatedHand;
transform.rotation = transform.rotation * relativeOrientationHands;
It works fine, however the problem is that, let's say I rotate the object 180 degrees around the x axis (after that, y is pointing downwards). If I release the object afterwards, and grasp it again, the orientation changes get from then on applied to the new orientation of the object which is super confusing to use. (Original: turn hand to the left - object turns counter-clockwise; After: turn hand to the left - object turns clockwise)
Unfortunately, I don't know the math to fix it, but maybe someone can help me out. How can I apply the orientation changes of the hand to my virtual object, with respect to the default object orientation using Quaternions. Maybe, something with normal vectors?
EDIT:
The 3 figures illustrate my problem. The first figure shows the object in its original orientation (x-right, y-upwards). I turn the cube 180 degrees by rotating my hand 180 degrees clockwise around z (blue axis), after I release it, x is pointing to the left and y is pointing downwards (figure 2). If I grasp the object (figure 3) in its current orientation, and let's say I would want to perform a 90 degrees clockwise rotation around the y-axis. In figure one, I could do that by moving my hand 90 degrees clockwise. However, I changed the orientation of the cube with my previous manipulation. Therefore, I would have to perform a 90 degrees counter-clockwise rotation with my hand to move the object in clockwise direction, because the y-axis is flipped (figure 2-3). I always want that turning your hand 90 degrees clockwise results in a 90 degrees clockwise rotation of the object regardless of its current orientation.
Thank you very much
Best
R.Devel

gameobject that rotate away from the facing direction of another game object

Trying to figure this out, but without success.
I have a AI that get to point X; facing that object, since I use transform.lookAt().
Now, I would like to turn the AI away, 180 degree, so it can face the same direction that the other agent is facing. I did try to add 180 to the transform once the AI get to destination but it doesn't work, sice the AI may arrive from any position, so the 180 degree rotation is not always the same as the direction in which the other agent is facing.
Is there a way to know or set, in which direction an object/AI/GameObject, is facing? Math wise, I believe it should be the vector3 related to when the object is imported in game, at 0.0.0 coordinates; although I can't really keep track of the orientation of every GO I have in the application; so I was hoping that there is some way to either set a direction, to which a GO is pointing at (even if it is not moving), or retrieve the orientation.
Have you tried looking at Transform.forward?
Edited for completeness:
Since transform.lookAt() is working for you currently, you can try this to face the target from any direction:
var lookPos = target.position - transform.position;
lookPos.y = 0;
var rotation = Quaternion.LookRotation(lookPos);
and then add 180 degrees to the rotation

UIImageView rotation affecting position?

I have a UIImageView that is set to move up and down the screen with the value of the accelerometer, using the following code:
ship.center = CGPointMake(ship.center.x, ship.center.y+shipPosition.y);
Where shipPosition is a CGPoint set in the accelerometerDidAccelerate method using:
shipPosition.y = acceleration.x*60;
Obviously this works fine, it is very simple. I run into trouble when I try to something equally simple, vary the rotation of the image depending on its acceleration. I do this using:
ship.transform = CGAffineTransformMakeRotation(shipPosition.y);
For some reason this causes a very strange thing to happen, in that the image snaps back to its origin every time the main method is called. I can see frames where the image moves to where it should be, but then instantly snaps back.
This problem only happens when I have the rotation line in, commented out it works fine. I have no idea what is going on here, I have done this many times for different apps and i never had such a problem. In fact I copied my code from a different app I created where it works fine.
EDIT:
What really confuses me is when I change the angle of the rotation from the acceleration to the position of the ship using:
ship.transform = CGAffineTransformMakeRotation(ship.center.y/10);
When I do this, the ship actually rotates based on the accelerometer but does not move, which is crazy because a changing ship.center.y means the position of the ship is changing, but it's not!!
You should set the transform of you view back to CGAffineTransformIdentity before you set his center coordinates or frame and after that apply the new transformation.
The frame property returns the transformed coordinates of a view if it is transformed and not the true (well actually the transformed are true) coordinates.
Quote from the docs:
Warning: If the transform property is not the identity transform, the value of this property is undefined and therefore should be ignored.
Update/Actual Answer:
Well the actual problem is
shipPosition.y = acceleration.x*60;
Since you set the y pos in accelerometerDidAccelerate.
The acceleration won't remember it's old value. So if you move your device it will get a peak and as you slow down it will decelerate again.
Your ship will be +/-60 at the highest acceleration speed but will be 0 when you stop moving your device and shipPosition.y will be 0.
CGAffineTransformMakeRotation expects angle in radians, not in degrees.
1 radian = M_PI / 180.0 degrees