IOS - How to apply a constant velocity - Swift 3 - ios

Im building a game where the node will move either up down left or right on the screen.
To do this im applying a velocity in whichever direction the person swipes. The issue here is that as the node moves along it's speed gradually reduces.
The main thing im trying to do is to have the node move at a constant speed in whichever direction its currently moving. is there a way i can stop the velocity from decreasing over time or possibly another way of moving the node at a constant speed?

You need to set the physics body's linearDamping to 0.
The linear damping is meant to simulate friction, and reduces the velocity of a physics body over time. The default value is 0.1.

Related

SpriteKit - how do I apply a constant velocity and change on touch - swift

How do I make my player move left/right at a constant speed until the user touches the screen again, which will then make the player change direction right/left and run that way at a constant speed etc..
I have tried looking at other answers but can't figure out a working answer.
I've set linearDamping to 0 already.
There are two basic ways to apply velocity.
One is by applying forces to physics bodies, or giving them velocities.
Two is positional transformations, usually done with SKActions.
They're not compatible.
Since you're using physics, you need to either apply force or set a velocity.
I think you should probably take the time to read this entire page:
https://developer.apple.com/documentation/spritekit/skphysicsbody
Here's the setting velocity cherry from it:
First, you can control a physics body’s velocity directly, by setting
its velocity and angularVelocity properties. As with many other
properties, you often set these properties once when the physics body
is first created and then let the physics simulation adjust them as
necessary.
And here's the outline on forces:
You can apply a force to a body in one of three ways: A linear force
that only affects the body’s linear velocity. An angular force that
only affects the body’s angular velocity. A force applied to a point
on the body. The physics simulation calculates separate changes to the
body’s angular and linear velocity, based on the shape of the object
and the point where the force was applied.

How to get Constant Velocity for Spritekit Gravity

I am making a Tetris kind of game, where I want my blocks to fall at a constant velocity. I want to achieve it using SpriteKit Physics Words Gravity.
Problem is that gravity is necessarily acceleration and hence block start to move faster as they fall. I know I can turn gravity off and use actions to move the block but I wish to do it with gravity.
Is there a way to make the gravitational force in SpriteKit world a linear constant velocity?
Using the default physics you will not achieve what you are looking for.
In the definition of Gravity in Spite Kit is clear that is a acceleration, so is no possible to achieve linear speed with it.
A vector that specifies the gravitational acceleration applied to
physics bodies in the physics world.
But, you can have some workarounds to achieve the behavior you want.
Gravity Acceleration 0, and add speed to the falling object.
Limit the maximum Speed of an Object.
Do you own physics
I Think the best way to do it, with gravity working as default, is by limiting the maximum speed.
2 - Limit the maximum Speed
Consulting this post.
In didSimulatePhysics(), you can verify the speed of the object, and limit it.
Like this (Credits to #Andrew, in the original post)
- (void)didSimulatePhysics {
if (node.physicsBody.velocity.x < MAX_SPEED_X) {
node.physicsBody.velocity = CGVectorMake(MAX_SPEED_X, MAX_SPEED_Y);
}
}

SpriteKit collision angle of line and circle is odd

Suppose a circle is enclosed in a diamond, and the circle heads due right. I would expect a series of continual 90 degree angles as the circle draws a square within the diamond.
What I'm seeing, however, is that the circle eventually gets pushed toward the center of the diamond after one or two collisions. What's going on?
The line is set to dynamic -> NO.
The ball gets reflected symmetrically to its angle of approach assuming it's a completely elastic collision. That means, to get the behavior you are looking for, the ball would need to approach exactly from one line mid-point to the next without any loss of energy.
Sprite Kit's physics engine will not be able to handle these exact values. The energy after an elastic collision in Sprite Kit is not guaranteed to be conserved. There will be small inaccuracies in the physics engine's floating point calculations. Also the collision itself may not be resolved as a perfect point-to-point reflection, which means the ball might "slide," which will result in the angle of reflection changing.
However I would still expect to see more collisions then just one or two. Make sure the restitution of both objects is set to 1.0 and make sure the ball's linear damping is set to 0. affectedByGravity should obviously be false. Friction should be set to 0. You can try setting usesPreciseCollisionDetection to true.
If you need the behavior you described in you picture to last indefinitely, you will need to set the position and velocity manually.

SKPhysicsBody ball won't bounce if impulse is too small

I have an infinitely bouncing ball simulation that works properly. The ball bounces around the screen borders forever.
One minor problem though is that if the starting impulse is too small the ball never bounces to begin with.
I experimented and eventually found that a starting impulse of at least 2.1 for my ball is required for it to bounce.
ball.physicsBody!.applyImpulse(CGVectorMake(-2.1, -2.1))
If I set the value to 2 the ball never starts bouncing it just stops at the bottom left corner.
ball.physicsBody!.applyImpulse(CGVectorMake(-2.0, -2.0)) //ball stops
I actually came across a question that answered this, but I forgot how to get to it. I know there is some lower limit that spritekit enforces to make a moving object stop when a collision occurs.
If I could get to that answer that would be great. Also if there is a way to override that, and make an object still bounce with a slower starting impulse that would be great too thanks.
edit: so I re-found the other question, SpriteKit ball loses all energy hitting wall, restitution=1
so my new question is, is there a way to set or lower the velocity threshold in sprite kit? I would like my ball to be able to move slower.
edit: anyone?
Your ball has a weight to it.
It's like in the real world - if you try push a chair with little force, it won't move as it has a weight.
However, if you push harder, the chair will start to move, but it does not start from 0.1 N of force (it really depends on the weight, friction and a few other factors).

SKSpriteKit how to control Impulse or Force speed?

Hi guys i want to apply slow motion in upward direction,
what i am confused, when i applyimpulse
[self.physicsBody applyImpulse:CGVectorMake(0, 60)];
it does applyimpulse with required speed as it measure from its velocity mass and other physics things
what i need to applyImpulse but with controlling speed of impluse that is applied on object on which impulse is being applied.
i tried to set even self.speed but got no success,
how one can achieve desired impluse or Force on an sprite with controlling over speed.
are there some properties that should i set for the SKSpriteNode before applying Impulse,
or some thing else?
Any suggestion or thoughts will be appreciated
Thanks
The speed property of SKNode is to do with SKActions, however the speed property of SKPhysicsWorld is what you probably want to tweak to achieve physics in slow motion.
It says the following in the documentation about the speed property of SKPhysicsWorld
The default value is 1.0, which means the simulation runs at normal speed. A value other than the default changes the rate at which time passes in the physics simulation. For example, a speed value of 2.0 indicates that time in the physics simulation passes twice as fast as the scene’s simulation time. A value of 0.0 pauses the physics simulation.
Access it from within your SKScene subclass like this-
self.physicsWorld.speed = 0.5; // half speed (slow motion)
All forces are applied as actual physics forces on physics bodies.
The resulting movement of the body that had the force applied to it depends on not just the magnitude and direction of the force but also the size and density (i.e. mass) of the body the force is being applied to and where on the body the force is applied.
I'm not entirely sure what you mean when you say you need "controlling speed of impulse" though.
Maybe what you need is not to use forces at all but to move the node manually.
You could use an action [SKAction moveByX:y:] or update the position in the update method. This way you can control the speed of the movement exactly.

Resources