SpriteKit snake - joints and physics - sprite-kit

I'm working on my game - Snake for iOS.
My development has already passed this stage, however I think about doing some refinements to it. After several tries and errors I've totally ignored physics/movement and collision detection offered by SpriteKit and I keep using my own implementation. Perhaps with your help I may refactor this parts of the game.
My snake can turn in all possible directions (360 degree). There is no gravity. How would you set up your physic bodies if you would prefer to offset as much as a code to existing SpriteKit implementation?
There are two important parts I think.
First - joints. Snake is constructed from multiple physic bodies. All bodies simply follow one another - going through all the points that the head was some time ago. I assume the best joint for this is Pin joint? I used Pin joints with anchor point exactly between neighbouring bodies. But this setup caused the rest of the snake's body to change the physics of the head. It shouldn't be like that. I tried changing the weight to zero, putting linear and angular dumping to maximum. The result was never acceptable.
Second - controlling movement. The movement of the snake is constant. User by pressing left and right button starts gentle movement of the snake to the right or left. Do you think I should in the update method reset the snake's velocity each time to my constant value? Or should I play with forces impulses and torques? The turning only takes place when user keeps pressing the screen - it takes few seconds to reverse the snake (like "u-turn") - how about rotating it?
Any suggestion is appreciated. Best regards.

Related

Flutter Flame game - animation speed (update duration)

for those who are familiar with using Flame in Flutter for game development, I'm wondering if you can just advise me whether I'm on the right track, or not - because I'm not sure if what I'm seeing in my testing is what I expected. I started out with Flame because I thought it seemed like a relatively simple way to make the basic game that I'm aiming to make.
I'm making a basic game where there are four boundaries defined on each edge of the screen, and a ball will bounce around the four boundaries. The boundaries are defined as widgets (because I want to control the properties of each - sometimes they'll be "electrified", meaning the ball shouldn't collide with them). And the ball is a widget as well, of course. I've got some basic code done where I can drag a line from the ball to indicate the direction that I want to start bouncing, and then the ball will bounce around the boundaries (just using basic angle of incidence = angle of reflection to determine the direction of movement).
The code to do the movement is in the "update" method of the ball widget - however, what I'm finding is that the time between updates is somewhere around 200-300 milliseconds, so if I want to show the ball moving at any kind of pace, it has to jump a good number of pixels at each "update" tick - and thus the movement looks "jerky".
Am I doing this the right way? Is there a different (better) approach that will make the movement appear smoother? Or, I'm wondering whether the duration of the "update" ticks is a result of running the code via debug in an Android emulator? (I'm using Android Studio for the emulation, and Visual Studio Code to build the project). I know I don't have actual code here in the question, because essentially I don't have an issue with my code not running - I would just like to understand if that duration of "update" ticks is "normal", and if the resulting "jerky" animation is just to be expected - or do I need to look at a different approach? Thanks in advance!
You should preferably not be using widgets for moving game parts, you should be using Flame components. So you could have for example 4 SpriteComponents as the walls and then the ball as another SpriteComponent and then you can use the collision detection system to act upon when the ball touches one of the walls.
https://docs.flame-engine.org/main/collision_detection.html

remove bouncing from PhysicsBody?

How can I completely remove the bounciness from my PhysicsBodies, I already set the restitution of all PhysicsBodies to 0 but when multiple PhysicsBodies collide they still bounce off of each other, how can turn this off?
Here's a video where i swipe up and down which triggers this code on the blue SpriteNodes:
node.physicsBody?.applyImpulse(CGVector(dx: x, dy: y))
https://imgur.com/a/MD2Sc
I want the blue SpriteNodes to just stay up or down and not bounce away
Are you experimenting in a space without any world damping/friction? If so, you will need to zero the velocity of objects the moment they collide in order to get zero bounce. But you'll also need to move them outside the boundary of the objects they collided with.
By way of explanation:
This is, I presume, happening because your objects are achieving a (slight) state of overlap right before the collision is reported to the physics engine. When this happens, the first response of some physics engines is to remove this overlap by moving the object with a corrective amount of force or movement. What's happening looks like a continuation of that movement.
In some physics engines you get the opportunity to determine how strongly the corrective behaviour is, and how frequently it occurs, and even set the physics simulation to much higher rates of solving than the frame rate. In these engines you could find the right balance of settings to prevent this. But not in SpriteKit.

Recreating physics seen in Snake vs Block

I am new to SpriteKit and am trying to learn by creating a game similar to the popular iOS game Snake vs Block
(game screenshot
).
I am not sure if I am setting up the game's physics the most efficiently.
I set the blocks to be affected by gravity, while the ball node is not.
In the touchesMoved method, I set the ball node's position to the touch's x position (y never changes).
Once a collision is detected, gravity is set to zero vector, the leading ball is removed and last ball put in its place.
Once the block is removed, I restore gravity.
However I am not sure how to keep several ball nodes connected to each other as in the game and make them follow the lead ball's position with lag.
Any advice on this?
You can connect them together as joints via SKPhysicsJointPin. You are basically making a rope / chain, and there are a ton of examples for Box2d ropes (SpriteKit physics is Box2d).
Just add the latest ball as a pin-joint to the bottom:
Here are some references for you in addition to my answer:
convert objC to swift (more or less):
https://objectivec2swift.com/#/home/converter/
(Github to project in link ): https://www.youtube.com/watch?v=7jWdcbmnmKQ
http://www.waveworks.de/howto-make-hanging-chains-sprite-kit-physics-joints/
https://developer.apple.com/documentation/spritekit/skphysicsjoint

Spritekit how to not veer off

I'm building a pacman style maze game.
I can apply impulse and limit it to whatever vertical or horizontal direction as desired on the hero.
However, when I add physics body sprites for walls, it gets weird.
I have set the hero physics body and the walls to have no bounce.
Running head on into a wall it stops dead like expected.
But if I then change direction and move along the wall I veer off at a slight angle from the wall.
Any ideas what causes this and how to avoid it?
I want to only move in vertical and horizontal straight lines.
Ok so the solution here is slightly counter intuitive.
Rather than using applyForce: or applyImpulse: family of methods on the physicsBody, use runAction:forKey: on the sprite itself.
For any change in direction, removeAction:forKey: just before calling runAction:forKey: to keep things orthogonal.
(Otherwise there will be a brief angular movement )
The other trick here is to find an appropriate speed for distance. Too fast and it will fly through walls. (Bug?)
Then apply the speed and distance with a multiplier based on the size of your scene.
The total distance should be something greater than possible in any direction.
That keeps the thing moving until it hits a wall or you tell it to move in a different direction.
This works really well.
Using the physics body movement, it seems best suited to a platformer situation where you have a gravity.
In a top down view, there isn't normally gravity.
( though it is imaginable that you could want something that exerts gravity on a sprite to suck it in)

Cocos2d - Creating collidable Sprites?

Hello everyone I an very new to cocos2d, so I apologize if this is a simple question. I would like to create to sprites that collide when they hit each other.
For instance, one sprite, spriteA, is in a fixed position on the screen. And another sprite, spriteB, is moving around the screen. SpriteB will collide with spriteA. If that doesn't make sense or you don't understand it, tell me and I will elaborate further. Any help is appreciated. Thank YOU!
Try using Chipmunk or Box2d physics systems. These are included with Cocos2d and work by having a physics simulation that updates with every time the graphics change on screen.
The physics simulation will tell you if two objects are overlapping, and will provide physical attributes like weight, slipperiness (friction), speed and direction, which creates reactions like bouncing, sliding, realistic loss of speed and changes of direction on impact.
If you are new to physics simulation, here's a 30 second run down:
Create "bodies" in the physics simulation that represent each graphical sprite
Bodies are usually defined more simply than their graphical counterparts, like a circle, square or simple polygon shape
In order to update the graphics on the screen accurately, first you establish a pixels to meters ratio. Meters are notional (i.e. made up) measurement that is used in the physics sim
Each time the physics simulation "ticks", you update the graphics on screen accordingly
So if a body in the physics sim moves 1 meter, you might transform the pixel sprite 32 pixels
Here's a good tute on collision detection using Box2d.
http://www.raywenderlich.com/606/how-to-use-box2d-for-just-collision-detection-with-cocos2d-iphone
enjoy
Its actually very simple:
Just schedule a timer: [self schedule:#selector(checkForCollision:)];
Specify the method: - (void)checkForCollision:(ccTime)dt {}
In the curly braces, make CGRects for each sprite using CGRectMake.
Then in the same method, just call: if (CGRectIntersectsRect) {}
That easy!
Well technically speaking when the 2 sprites interact, or share at least one common point, then they are colliding. I'm a little confused by your question. Are you asking for direction on how to make the sprite move on screen, or are you asking how to handle the actual collision(such as calling a method if they collide)?