SpriteKit physicsBody always upright - ios7

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.

Related

How to stop a sprites physicsBody from moving?

In my SpriteKit game i move several sprites using [sprite.physicsBody applyImpulse:vector]; but at a specific point i the game i want them to stop.
I already tried to apply the exact opposite impulse.. but that did not worked very well.
Thanks
You can directly set a physicsBody's velocity via the velocity property. Stop a body by setting it's velocity to (0,0):
sprite.physicsBody.velocity = CGVectorMake(0,0);

Sprite Kit Physics Simulation - bodies never come to rest

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.

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 recognize the touch of a non regular sprite image?

I have a sprite and if it is touched the touch should be recognized. I used the coordinates to do so. I took the coordinates (min x, min y, max x , max y)of the sprite image. But The sprite image is not a rectangular shape. So, even if I touch the coordinates outside the sprite and inside the rectangular bounds the sprite is recognized.
But for my application I need only the sprite to be recognized. So, I have to take only the coordinates of the sprite, but it is not regular shape. I am using CCSprite in my program.
So, what can I do to for only the sprite to be selected ? Which classes should use for this?
Thank You.
You could try one of the following...
Create a bounding box smaller than the absolute extents of the sprite image. Yes it will be smaller than the sprite. This will eliminate the dead space click detection of the sprite the trade off being parts of your sprite which look selectable won't be
Use a circular bounding area to detect if the user has clicked on your sprite. Again you will have the dead space problem in my first suggestion but the sphere may give you some better coverage area over the sprite giving you better results on touch detection
This is a standard problem in physics collision detection systems which often end up using circles or rectangles as their collision bodies. I would go with the either a circle or rectangle smaller than the size of your sprite as your bounding area. Going finer detail than that you could generate bounding area polygons. This would however introduce a whole bunch of new issues and concerns.
I am building a Cocos2D game right now and what I am doing is first I step through my sprites and see which sprites the touch hit (they overlap in my app)
Then, for each sprite hit I use [sprite convertTouchToNodeSpace] to get an X,Y co-ordinate inside the sprite, which I can use (although the Y axis is flipped) to reference the CGImage I created the sprite with.
If the pixel at the touch point is 'clear' ie alpha 0, then the sprite was not really touched, and I check the next sprite in the z-order to see if it has color where it was touched.
Sometimes I think I should be using a two color mask image to go along with each sprite, not the sprite image. But, I am mr. make it work, then make it fast.
I realise this is not super efficient, but I do not have very many sprites and I do this only for touches.