physicsBody.applyImpulse & collision happening twice instead of once - ios

Heres the deal. My ball drops, hits the floor, and bounces back up with the help of the following code
ball.physicsBody?.applyImpulse(CGVectorMake(0, 25))
However, sometimes the ball recognises two collisions instead of one (on impact) and the ball gets applyImpulse x2. (Due to lag or something?) Causing the ball to fly way to fast. How do i make sure the ball doesn't collide with the floor twice? The ball is 16x16 an the floor is 16x160. I didn't have this problem earlier when the ball and floor were larger. But i really want to solve the problem and it must be possible!

What is happening is the ball is not moving fast enough between updates to be off the paddle before the next update check.
Remember what we did for the boss? Same thing gets applied to the paddle. When the ball hits the paddle, remove the contact check for it. Now you are going to have to add another node in, so that when you pass this node, you reenable the paddle check

Related

Bouncing Ball SKPhysicsBody - Stop Bouncing Perpendicular Forever

So I'm making a brick break style game using "Ray Wenderlich's amazing tutorial" in Spritekit/Swift, just like the tutorial.
I successfully have a ball bouncing around the screen using a SKSpriteNode() and SKPhysicsBody() and I've been tweaking values for the impulses used to begin the ball bouncing around the screen forever.
However, I stumbled upon a problem which I hope to find a solution.
Sometimes, in my game, the Sprite will bounce around and become "Wall Locked" bouncing almost Perpendicular (or straight) between two walls, (give or take a few pixels)! So ends up Zig-Zagging across the whole screen between two said walls for ages.
What I want to do is, should this happen and the ball get "Wall Locked" to introduce a new impulse to get it moving again...
How can this be done? For example, if it has bounced between the top and bottom walls 10 times, then it's time to adjust it's angle/introduce a new impulse.
Could the last 10 positions be stored in an array, then test if their almost perpendicular somehow?
If you inspect the velocity property of your Sprite, you could detect when either of its values is close to zero and then make them something that is not so close to zero. This would stop it getting stuck moving just horizontally or vertically.

How to flick a SKSpriteNode like a soccer ball

If I had a SKSpriteNode on the screen, how would I be able to flick it so that it would simulate the physics of a soccer ball being kicked. how to throw SKSpriteNode? I am guessing that it would be sort of like this question, but instead of the node following your finger first before it is flicked shouldn't happen. It should look like when you flick you are swiping towards the node and then once your finger hits the node it moves it like the physics of a soccer ball. Can anyone give me a place to start in order to achieve this??
I would recommend using the touchesBegan method and touchesMoved method. Basically, keep track of the last position of the touch, along with the timestamp (using NSTimeInterval), and on each touchesMoved check if the soccer ball has the touch location inside it (using [soccerBall containsPoint:location]. Then, calculate the force that you should impart on the ball (compare timestamps, and compare positions), and then run:
[soccerBall.physicsBody applyImpulse:CGVectorMake(dx, dy)];
Replace dx and dy with the results of your calculations. You may wish to add a scale value to make sure that you get the results you are looking for.

didBeginContact not called on fast moving sprite

I have a game where a user is dragging around my main sprite. The main sprite collides with other sprites just fine except when the user drags the main sprite very quickly. Sometimes when the main sprite is moving quickly, the physics bodies just pass right through each other and the two sprites suddenly overlap. I have a breakpoint set that logs the hit count at didBeginContact, and it is not hit.
Is there a limit to how fast a sprite can move and still be covered by didBeginContact? Am I allowing the user to move the sprite faster than the game cycle can handle the collisions?
Again, when the sprite is moving at slow speeds, the physics are working perfectly.
Remember, these things are all calculated frame by frame. You're probably moving the sprite so fast that its ending up on the other side of the screen in too few frames to count as a collision. If someone is spastically moving their finger around it might not catch it. You could put some kind of speed limit on the sprite or something.
try to set physic body with usesPreciseCollisionDetection = YES

Simulating the "Change Directions on Ice" Effect

I really hope someone understands what the effect is that I am asking for. Have you ever played the games where a character is sliding one way and when you try and change the characters direction it is not immediate as they need to slow down in the initial direction before they can start sliding the other way? The new game on the App Store 'Swing Copters' by the maker of Flappy Bird is exactly the effect I am talking about. Can someone please help me create this effect in SpriteKit. I have already tried achieving it by applying different forces but I am either not doing it correctly or the effect isn't possible with forces.
Thanks in advance!
Maybe you want to try working with some acceleration-stuff.
Let's think about the gravity on the earth (a = 9.81 metres/s²). If you throw a ball straight to the sky, the Ball is starting with a velocity of: lets say 5metres per second(positive velocity).
After a short time, the gravity is pulling the velocity of the ball down to 0, so the ball can't get any higher. Right after this, the gravity pulls the ball down until it hits the ground. (Negative velocity)
If we're talking about this in a game, where the ball doesn't move up or down but from left to right, you can use something like this. The moment when you throw the ball, is the moment in the game where you send the command to change the direction. The ball keeps going in the direction where it used to go, gets slower, stops, changes the direction and finally gets faster and faster until you send another command to change the direction again(Or it hits the Wall/the ground). In that case you have to inverse the acceleration you want to use, so the whole thing repeats in the other way.
If the ball should move to the right, positive acceleration,
if the ball should move to the left, negative acceleration.
As formula you can use something like
v = (int) (a * t + v0);
v0 = v;
v is the next velocity, a is the acceleration you want to use, t is the spent time and v0 is the current velocity. t should count the nano-time since the last direction-change. (int) casts the whole thing to an integer, so you can use this directly to move the ball/graphics on the screen.
Repeat this each frame.
For the direction change you can use
t = 0;
a = a * (-1);
t has to be 0 again, otherwise it gets buggy.
I hope this was helpful.

How to avoid collision force in box2D?

i'm creating simple iOS game with Cocos2D and Box2D. In my game user has to create full word by shooting to squares with letters. If shooted letter is correct the square should explode, otherwise the square should fall down. I have created simple contact listener and I can detect collision between bullet and square but the problem is how to avoid collision force when letter is incorrect? I that situation (incorrect letter) i want square to simply fall down without applying collision force to the square.
Maybe I can delete the square and create new one at the position of deleted, but i think it's not the best idea :)
One suggestion I would make would be on collision, if the letter is incorrect, set the bodies x and z velocities to 0. This way when they collide with incorrect squares they will simply fall.
Another thing you could do would be to set the target square's body as fixed ( or rigid, I can't remember what it's called in Box2d off the top of my head). So even if an incorrect letter collides with it, it will not budge but the letter will bounce off it. And it the letter is correct, you can explode it as normal.
The solution I would advice is to set the velocity of the bullet to 0 in both directions in the PreSolve callback of the contact listener. Obviously you do a check in the PreSolve function. This way as the function is called PreSolve, the collision calculation have not happened yet. so setting velocity to 0 will make the bullet to have no effect on the square in terms of force.

Resources