Sprite Kit Physics Simulation - bodies never come to rest - objective-c

With Sprite Kit physics I have a simple setup with a ball bouncing inside a rectangular edge shape.
It all works fine, however the ball never comes to rest. The resting property is always NO and I can see that the ball animation keeps making a little bit of movement when it should be resting.
Sprite Kit is based on Box2D and here there is a doSleep option on the physics world, but I cannot find something similar with Sprite Kit.
What am I missing?

You should use restitution. A property for setting the bounciness of the physics body in sprite kit. Actually, it is a property describing how much energy a body retains when it bounces off of another body. Dot operator can be used with the ball's sprite name to access the restitution property.

Related

Getting direction of object moved by the physics engine

i am using spritekit to learn about physics behaviour in games, when a sprite node ball bounces off a surface, the physics engine will automatically calculate its new direction.
how do i get this new direction in code?
i am using swift, xcode spritekit. but object c spritekit (cocos2d) is fine too
(the aim is to get a fire trail behind the bouncing ball, but i dont know how to rotate the particle emitter based on ball's vector of movement. the emitter is currently added as a child of the ball node)
To get the "direction" you should read the velocity property of the physicsBody of your node.
To rotate your particle emitter, you should modify the zRotation property of the node.
Well, the most best idea for getting a rotation of a node is getting eulerAngles. But since it showed radians, I converted to degrees. This is the code that I got when getting the rotation of a camera Node
print(round(GLKMathRadiansToDegrees(Float(gameController.camera.eulerAngles.z))))
Hope This helps

How to make a PhysicsBody based on Alpha Values

Suppose there there is a scene as follows:
There is a scene with the same size as the frame of the device. The scene has a red ball, which is able to move throughout the 'world'. This world is defined by black and white areas, where the ball is ONLY able to move in the area that is white. Here is a picture to help explain:
Parts of the black area can be erased, as if the user is drawing with white color over the scene. This would mean that the area in which the ball can be moved is constantly changing. Now, how would one go about implementing a physicsBody for the an edge between the white and black areas?
I tried redefining the physicsBody every time it is changed, but once the shape becomes complex enough, this isn't a viable solution at all. I tried creating a two-dimensional array of 'boxes' that are invisible and specify whether most of the area within each box is white or black, and if the ball touched a box that was black, it would be pushed back. However, this required heavy rendering and iterating over the array too much. Since my original array contained boxes a little bigger than a pixel, I tried making these boxes bigger to smooth the motion a little, but this eventually caused part of the ball to be stopped by white areas and appear to be inside the black area. This was undesired, since the user could feel invisible barriers that they seemed to be hitting.
I tried searching for other methods to implement this 'destructible terrain' type scene, but the solutions that I found and tried were using other game engines. To further clarify, I am using Objective-C and Apple's SpriteKit framework; and I am not looking for a detailed class full of code, but rather some pseudo-code or implementation ideas that would lead me to a solution.
Thank you.
If your deployment target is iOS 8, this may be what you're looking for...
+ bodyWithTexture:alphaThreshold:size:
Here's a description from Apple's documentation
Creates a physics body from the contents of a texture. Only texels
that exceed a certain transparency value are included in the physics
body.
where a texel is a texture element. You will need to convert an image to the texture before creating the SKPhysicsBody.
I'm not sure if it will allow for a hole in the middle like your drawing. If not, I suspect you can connect two physics bodies, a left half and a right half, to form the hole.

How to clip part of a sprite based on its position?

I'm designing a game in Cocos2d, and at one point I have coins shooting out on a platform from a zelda-ish perspective. I'd like to display the coin's shadow sprite (a different sprite from the coin) on the platform, but mask or clip the shadow sprite on the edge of the platform. The coin can continue off the edge of the platform, but the shadow should stop at the edge. The platform also moves, so I need the shadow sprite to track with the platform's movement.
I thought it could work to use a CCClippingNode for this, but I can't add it as a child of anything in a spriteBatchNode which is how I'm making my platform. Without having the shadow as a child of the platform, I'll mess up z-order and the shadow movement won't track correctly. I also checked out Ray Wenderlich's tutorial on masking a sprite but I don't think that'll work since it looks like it masks an individual sprite texture and not an area of the view where the sprite shouldn't be displayed. Any ideas on how to solve this?

Static circle rotates when I move kinematic rectangle in Box2d

I am writing 2d scroller with Box2D in Xcode. Scrolling is organized by moving kinematic bodies (which all together make ground). Kinematic bodies are moved with setlinearvelocity function.
When dynamic body falls on this ground and stops near the wall it starts to rotate.
Here is an image:
Black circle is dynamic, blue blocks are movable kinematic bodies. When circle falls in such pit it starts to rotate. If I stop moving blue ground it does not.
How can I change this?
Stop moving the ground and move The ball instead. I don't see the reason why you are moving the whole terrain instead of only moving a ball. Making the terrain a static body will make the physics simulation more efficient too.

SpriteKit physicsBody always upright

I have a spritenode with a physics body that is affected by gravity and dynamic, however, when it hits things it rotates.
I am only using the physicsbody for the Y axis gravity, and nothing else. When it hits something horizontally, I don't want it to be affected by that thing..
How can I accomplish this?
Straight from the Sprite kit programming guide:
The allowsRotation property determines whether forces can impart angular velocity on the body.