Sprite Kit different gravity for different sections - sprite-kit

I am building a flappy bird game where half of the level is above water, and the other half is below water.
If your bird is in the air, there is regular gravity and when you tap, an impulse is applied going straight up.
If your bird is in the water, gravity should be in the negative direction (going up) and be less. When you tap, an impulse is applied going straight down.
Can I set a scene's gravity in different places?
I thought of using a timer to apply negative forces if the bird is in the water, but it's all over the place.
Also, I can't just change the entire scene's gravity because there are other SKSprite objects in the scene that should have different gravity applied to them (for instance one bird in the air should be flapping up when tapping, and a bird that dove into the water should be swimming down when tapping all at the same time).

Gravity is the same throughout the world, so you can't have areas of different gravity.
However, just because SpriteKit doesn't offer a higher level abstraction for something doesn't mean it's difficult to do for yourself. SpriteKit's constant gravity does the same thing to a body as if you apply a force to it with the same magnitude and direction every time your scene's update: method runs.
So, you can do different gravity in the upper and lower parts of your scene in your update: method.
Check the y-coordinate of your bird.
If it's below whatever threshold separates water from air, use applyForce: to apply an upward force. If it's above the water, apply a downward force.
Because this happens every frame, the bird will accelerate downward when above the water and upward when below, and you should see a nice loss of momentum effect just after the transition.
You'll need to tweak the magnitude of your forces until it behaves just right for the gameplay you want. When you do, be aware that the units for applyForce: are 150x those of SKPhysicsWorld's gravity (because of what's probably an Apple bug) and depend on the mass of your physics body (because of Newton's second law).

I have an idea for how you can do this:
make two rectangles for the top and bottom half (one for water, one for the air)
create a physics body on each rectangle that simulates the gravity you desire
set those rectangles as the contact delagate
add the other objects to the rectangular world they should be in
To handle main bird:
- When the anchor point (probably set in middle) contacts one of the rectangles,
change the contact delegate of the main player from the first body to the second body in the contact event.
This means that you have two worlds with different gravities with a main character who's gravity switches among contact of a new world(passing through the middle)
Good luck

Related

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.

SpriteKit snake - joints and physics

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.

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)

mouse joint is not working to restrict the ball in the half part of the screen

Hi guys I Am developing the application in cocoas2d using the box 2d frame work but unfortunately
i'm not able to restrict the gray ball in the half screen area of the image shown here
i want that ball not to go opposite part of the screen
I Have Used The b2Mousejoint For to move the ball around the screen
b2PrismaticJointDef restrict on any particular axis
But
i want to restrict on the particular rect area of the screen
You could create your custom distance joint which will restrict global axes of the ball. But it will be hard if you never write your own physics engine.
There are 2 easier ways to implement what you want.
Create 4 static "border" boxes around the area where the ball must stay. Then place the ball and the boxes into one collision group.
However, the response of the "border" boxes will not be instant. Therefore, the ball at high speed will sometimes "sink" into the boxes, then be popped out.
You can correct the position and reset the speed of the ball manually in code when it crosses the bounds of the desired area. But it may lead to the unstable physics simulation.

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)?