rotate object to touched coordinates - objective-c

I'm doing a simple game in SpriteKit, and have little problem. I would like my character/player(SKSpriteNode) to rotate himself to position where I touched the screen, so he always face touched to coordinates. I know there something like zRoatation, but I don't know how to calculate an angle. I will be really grateful for any tips.

I found a answer thanks to Antoine Lecaille.
float newAngle = -atan2(newPosition.x - currentPosition.x, newPosition.y -currentPosition.y) + [self degToRad:180];

Related

Detect eyes in real time video with Objective-C?

I was checking this Apple sample code, Squarecam. there are also some examples out there written in Swift.
In this example, a red square is drawn when a face is detected. My question is: How can circles be drawn at the eyes?
I still don't know how to detect the eyes in a similar way they did it in the example with the face.
Or how the position of the eyes (faceFeature.leftEyePosition) can be used for drawing the circle.
Any ideas?
The face feature gives you a point of the feature inside the Image bounds.
open var leftEyePosition: CGPoint { get }
you can build a rectangle around the point. Once you have the rectangle you can create a layer and composite over the face image.
if let overlay = CIImage(color: overlayColor).cropping(to: faceImage.extent).applyingPerspectiveTransformFilter(onRect: eyeRect)
{
let eyeMarkedImage = overlay.compositingOverImage(faceImage)
}
The "applyingPerspectiveTransformFilter" and "cropping" are the CIFilters.

Changing the hitbox of an image

I've been looking for a solution for quite a while now, but I can't help finding one. The situation is as following: I created an SKSpriteNode with an image, by the method touchesBegan I want to interact with it, like if one touches the image, something should change. The problem is that the hitbox around this image is square-shaped and not adjusted to the shape of the image. Has anyone got any clue how I can solve this problem? I tried doing it by changing the physicsBody like this
CGFloat offsetX = node.frame.size.width * node.anchorPoint.x;
CGFloat offsetY = node.frame.size.height * node.anchorPoint.y;
CGMutablePathRef path = CGPathCreateMutable();
CGPathCloseSubpath(path);
node.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];
Seems like the touchesBegan method doesn't use the physicsBody shape but the actual shape, I tried my best explaining it, sorry if I confused you.
Thanks in advance!
Julian
In the Xcode level editor, under the physics definition of your SKSpriteNode, change the body type to alpha mask (see image). This makes the physics boundary conform to the actual shape of your sprite rather than the rectangular bounding box.
Sadly, this solution only applies if your sprites are managed in an .sks file since level editor seems to have logic that calculates your sprite's outline and turns it into a path used to initialize the physics body with a body type of 'alpha mask'. (Too bad there's currently no 'bodyType' property on SKPhysicsBody because it would be really convenient to be able to set this programmatically instead of only being able to do so in the .sks file... Apple?)
The way you're attempting to create and initialize an SKPhysicsBody with a CGpath is on the right track I think... But since it's currently an empty path (having no points or lines) I'm guessing it won't have any effect.
Is there any way you could approximate some points & lines to represent a rough outline path for your sprite? Then, you could use those points to create a CGPath and then use that to initialize an SKPhysicsBody.
If not, a short-term compromise could be to initialize your SKPhysicsBody using init(circleOfRadius:) and use a circle as the body type which could seem less clunky than a box. And you could maybe play around with the circle radius to get a feel for the most natural radius for your collisions.

How would I move a SKSpriteNode in a certain direction?

Is there a way to make a skspritenode move in a certain direction? As in degrees. EG: Face 70˚ then move in that direction. I am trying to make it so you can shoot a bullet from my player node but I cant figure out how to make the bullet go past where my mouse is.
if you're using physics, then you can applyImpulse using a CGVector. If you're not using physics then you would use an SKAction, but it doesn't solve your specific use. If you wanted to rotate the object first, then you'd change its zRotation. Obviously you'd have to setup the sprite and the physicsBody, but I've left those steps out.
SKSpriteNode *mySpriteToMove = [SKSpriteNode node];
[mySpriteToMove.physicsBody applyImpulse:CGVectorMake(floatDX, floatDY)];
[mySpriteToMove runAction:[SKAction moveBy:CGVectorMake(floatDX, floatDY) duration:amountOfTime]];
mySpriteToMove.zRotation = someValueInRadians;
CODE FOR COMMENT RESPONSE:
[mySpriteToMove runAction:[SKAction moveTo:CGPointMake(floatX, floatY) duration:amountOfTime]];

Direction of Webkit rotation-transform?

is there a way to change the direction of the webkit rotation-transform? Currently it rotates an object to the right, but I would need to rotate it to the left.
edit: the angle it should rotate, i parse out of a document, so it's fixed!
Any ideas? Thanks!
You can rotate by a negative angle.

finding the coordinates of an NSImageView

Im not too used to cocoa yet, so please bear with me
I am writing a game for mac in Objective-C on cocoa, and I have one problem:
I have two NSImageViews, and i need to set it up so that if I move one, the other one follows it ON THE Y AXIS ONLY. How do I find out the coordinates of the first image so that i can apply the x value to the second one?
Any help is greatly appreciated
I'm not sure if this works with NSImageView, but it works with UIImageView.
yourImage.frame.origin.x;
I hope that helps