Rotation around center point with velocity - iOS - ios

Hi fellow programmers!
What I'm trying to do:
I have an image placed in the center of the screen. When I put my finger on this image, and rotate my finger, the image should rotate as well. When i release the finger, i would like the image to decelerate for x period of time. (Let's say that it should decelerate slowly). Basically I'm trying to develop a Spin-the-bottle application, but I can't figure out the velocity part.
What I've tried:
I've used this to get the one finger rotation, and it works great so far, but it doesn't include the velocity part. And honestly, I have no idea, how to calculate the velocity.
What I'm asking of you:
A "simple" way of doing one-finger-rotation with velocity, of an UIImageView in a normal UIViewController. (Or just using this method to the rotation part, and somehow implement velocity.) Examples are appreciated!
If you have any questions, please comment!
Thanks in advance. :)

Related

Swift/Xcode "Snake Game"

I am a student learning how to code in Xcode using swift. I am currently trying to make an app similar to the snake game that hopefully everyone knows.
I am currently on a windows computer so I can't show my code at the moment. I doubt you will need it for what I'm asking.
I am trying to make my Sprite Node (Just a while square) move with a swipe of the screen in the up, down, left, right fashion just like the original snake game. I have the Gesture Recognizer set up so it prints out what direction your swiping.
How can I make the sprite node move with a swipe? I want it to move infinitely in that direction until you either 1.) hit a will and you respawn/game over or 2.) swipe in a different direction.
I assume you add velocity to the node when you swipe so you just add the code into that swipe snippet but how do I make it apply ONLY to the "snake"?
You can use an SKAction to move the snakeHead toward the wall that corresponds to the direction you want to move. This can be done in the action parameter when you declare your gestures.
I suggest using .move(to: duration:) and to maintain a constant speed toward each wall you can use the distance, speed, time formula to calculate the duration for the .move(to: duration:) action.
Check out https://developer.apple.com/documentation/spritekit/skaction
https://developer.apple.com/documentation/spritekit/skaction/1417768-move

Drag and launch type thing in Swift Sprite Kit?

If I have a sprite node, just a white circle somewhere on the screen, how am I able to make it so when I drag, let's say downwards and slightly to the left, the circle sprite would launch upwards and to the right and then gradually come down, like a golf shot.
Another way of explaining the mechanic is the Angry Birds game, where you launch the birds of the slingshot, the birds move in the opposite direction of your drag and gradually come down.
For another live example of the mechanics of the circle, look at the app, Desert Golfing.
Thanks, and if you don't know what I mean just comment and I'll try to explain it better.
OPTIONAL: If you do know how to do the slingshot type mechanic for the circle, do you also know how to add an arrow to the screen so users know which way the circle will launch?
Ill try to break your problem down into small steps that you could then solve yourself:
Detect the swipe:
use a UIPanGestureRecognizer. You will be able to implement a method that is called whenever a user drags their finger in a certain direction.
Here are some good references:
- Pan Gesture Official Documentation
- A very useful question that can serve you as a guide
Detect the magnitude of the swipe in order to impart an impulse
Check out the second link above. What you have to do is in the method for the gesture recognizer you will detect certain flags such as when the user starts the pan or ends the pan. Then, you can check for the location at those moments. With the Pythagorean theorem you should be able to get the distance and use that as the magnitude.
Apply impulse:
Create a physics body for your sprite and then make sure that you have gravity set inside your physics world. This allows the sprite to move in a parabolic motion. Then, use applyImpulse: on your physics body with your magnitude.
Regarding the arrow, you can easily do some delegation from within your pan gesture handler that gets the magnitude of the swipe and projects a reflection that your arrow will then show. Your question is pretty loaded so going into more detail is impossible, but best of luck. Hope this helps!

Apply an impulse to SKNode to move across screen and detect collision

I am making a basic app that pushes shapes across the screen and detects collision with Sprite Kit. My first attempt was using moveTo on the nodes. The issue I had was with collision, the objects would rotate around each other instead of bounce.
Therefore I found I need to use applyForce OR applyImpulse.
In this situation I have a circle for example that is position off screen at its start of life. We then determine a target exit point, and want to 'flick'/'push' the node in that direction.
I cannot figure out how to applyImpulse towards the target end position I have as a CGPoint. I need to get this to a CGVector but I am not sure what needs to be done. I had a look around and found some Ray tuts but they just show applyForce or moveTo. I am not sure how to calculate this.
I found a site that explains 2D physics well.
http://www.rodedev.com/tutorials/gamephysics/
With this I worked out what the angle needed to be and have a speed that I can control and it works well.
You can move an object by changing manually the x and y position so you can reach your end point. In the update function you change yourObject.position.x and yourObject.position.y if I have understood correctly your question. If not please be more explicit. Hope that helps.

Sprite kit ios game slingshot machanism similar to angry bird game

I am newbie in IOS Gaming, and i need to create a game using Sprite Kit framework for functionality similar to angry bird pulley system, and also wants to find the distance the object is travelled from the pulley to its landing.
Can anyone help me out with this, i would be thankfull for it.
Thanks in advance.
One way you could code a slingshot effect would to use a starting point on the screen at let's say (x=100,y=100). You would display a SpriteNode of a slingshot with the Y centered at the (100,100).
The next step would be to use touchesBegan:withEvent: in the area of the slingshot to let your code know the player is looking to shoot the slingshot.
You would use touchesMoved:withEvent: to track how far back (how much tension) the player is pulling back from the slingshot.
The release would be triggtouchesEnded:withEvent. Based on how far the touch began (x=100) and how far back is was released (for example x=30), you can apply force like this:
float forceUsed = startTouchX - endTouchX;
[_projectile.physicsBody applyForce:CGVectorMake(forceUsed, 0)];
If you are looking to angle the shot you would also have to track Y and use that variable instead of the 0 above.
As for calculating distance between 2 points on the screen, it boils down to x and y coordinates. Subtract objectA.position.x from objectB.position.x
Things can get a lot more complex of course but that all depends on what you want to do in your code. Hope this helps.
P.S. The touches above are all part of the UIResponder Class.

UIView perpetual linear animation with collision detection

I'm trying to make a circular UIView animate in a particular direction until it collides with the borders of the view (which is full screen, so basically the borders of the device), at which point it will reflect off it and continue on its way infinitely. However, I'm not really sure how to pose my question, so I'm having trouble finding any information on it. I already have the view, the direction it will move in, and its velocity. I'm just not sure how to handle an animation like that.
Any advice is much appreciated! Thanks!
Use a CADisplayLink to continuously update the position of the view, synced with the refresh rate of the screen. The update method that that display link triggers will take into account the current x and y velocity of the view and update the frame based on it. If at ever point the x- or y-coordinate goes under or over a limit (0 or screen width/height), reverse the appropriate velocity value and recalculate the position.

Resources