How to create a SKPhysicsBody in a shape of a parabola? - swift

I need to create a SKSpriteNode or a SKShapeNode such that it would look like the image below. I can figure out how to use a texture to get the shape of the line but I can't seem to find a way to make the physics body. It needs to be made out of two horizontal lines which can change their y position and the middle parabola-like shape joins the other two lines with a specified maximum point (the maximum would hopefully be a variable).
(Note:- the blue and green lines are just to highlight that the image is compromised of three objects)
Is this possible? Thanks!

Related

Find the Exact Frame of a rotated SKSpriteNode

I really want to know how to find the EXACT frame of an SKSpriteNode if it is rotated. Currently, the frame of an SKSpriteNode looks like this:
This frame is the rect.frame.
However, this frame includes a lot of empty space due to its zRotation. I don't want this empty space and instead want the frame of exactly the SKSpriteNode.
This is what I want:
How can I achieve this? If you have any idea how to find this 'exact' frame of an SKSpriteNode, I would really like to know. Please use SWIFT.
Thank you
This can be done.
Put a dummy node at the bottom left of your sprite that you know the exact size of. Probably use a perfect square. Anchor it's bottom left to the bottom left of your parent. The parent is the one you want the exact size definition of.
From there, two ways:
Scale the dummy sprite to the size of the sprite you're curious about, and use those measurements to determine where the sprite is and what size it is at any point in time.
Put another dummy sprite at the top right of the parent. In this case you can use the midpoint of your two dummy objects, you don't need to use their edges perfectly. Now you can find the position of these two dummies, at any time, and figure out the size/shape/outline of your sprite in world space units.
Here's way 1 animated

SpriteKit - Getting The Weight Of A SKSpriteNode

I have an app with large boxes falling on top of the small red box. I would like to know when the small red block reaches a certain weight (X blocks are resting on top of it). I couldn't find a weight property for the red block. Any suggestions?
EDIT: Just to clarify. The boxes falling from the top will be random sizes, and falling from random positions. So there isn't really a way to keeping track of what landed on top of the red block. I need some way to measure the downward force being applied to the red block
You can calculate the weight for each node the following way and then add them together.
redBox.PhysicsBody?.mass

Sprite Kit Physics Body Complex Shape - Spritekit

I have this situation: http://mokainteractive.com/example.png
I'd like to move the white ball inside the red track and detect wherever the balls touch the limit of the red track.
Which is the best solution? I have to create multiple transparent shape along the borders? Do you have other ideas?
thanks so much
In iOS8 you can create a single physics body for that kind of shape.
Make a texture of your shape with p.e. Adobe Illustrator and use this method:
init(texture texture: SKTexture!,alphaThreshold alphaThreshold: CFloat,size size: CGSize) -> SKPhysicsBody
The SKTexture is your shaped image. The body is defined by the colored pixels.
The alphaThresHold: The minimum alpha value for texels that should be part of the new physics body.
The Size is clear I think.
The texture is analyzed and all invisible pixels around the egg are ignored and only the color pixels are interpreted as the body of the SKPhysicsNode. You should use too many of these because they are very expensive to calculate for the Physics Engine.
Variations of this method are found in the SpriteKit Class Reference.
To your problem. Make an inverse texture of your area which should be transparent and pass it as texture to the physics body. It will be analyzed and a body around the free zone is created.
You cannot create a single physics body for that kind of shape.
Using bodyWithPolygonFromPath: will only allow you to create a convex polygonal path which obviously does not work for your shape.
I think you have 3 options here:
Use a number of bodyWithPolygonFromPath: bodies (probably the hardest to do and time consuming).
Use a number of various size bodyWithRectangleOfSize: bodies (not so hard but time consuming).
Use only straight lines in your image and use bodyWithRectangleOfSize: (the easiest and fastest). If you choose this option remember you are still free to rotate your straight lines to various angles.

How to use a different texture on intersecting part of 2 quads

I'm looking for a way to dynamically change a part of a Quad that has a SpriteRenderer attached to it. Let's say I have a red Quad and a blue Quad, and then I drag one onto the other (fast or slow), the intersecting part should be colored using a green sprite. This illustration shows the scenario I'm trying to solve.
Can someone please help me with this?
You have two options:
First, if your mid color will be the correct mixture of other two color, in this case it would be yellow, you can use Mobile Particle/Additive or Mobile Particle/Multiply Shaders.
In a second way, you can write your own shader that takes the intersection area as parameter and paint your textures according to parameters.

Circle with different colors

I am new to Iphone. I want to draw a circle with different colors in it. And all the colors should cover equal area. Like if I want to have 10 different colors in it. Then each color should cover 1/10th area of the circle. I am not trying to draw a pie chart here. Also not trying to use 10 different colors. Just want 10 equal parts of circle and each part can be filled with colors.
I am trying to build a fortune wheel. Such that a smaller wheel is above the larger wheel. And then I want to drag them separately.
Also is it possible to do this with help of Core Animation?
Ambiguous question. If you draw a piechart with 10 equal areas then each will cover 1/10th of the area, thus fulfilling your request, no?
There are 360° in a circle, so divide that by 10 and each wedge should have a 36°. Now you just have to draw 10 wedges, and this page should help you:
http://www.raywenderlich.com/2106/core-graphics-101-arcs-and-paths
Since you say you don't want pie slices, do you want concentric rings instead?
And are you sure you want equal AREA? That will make the rings different thicknesses. The innermost ring will be fairly thick, and each ring as you go outward will be thinner. Much thinner, on the outer rings.
Our eyes are used to a bulls-eye formation, where each ring is the same thickness.
In any case, you should look at CAShapeLayer objects. You can create a shape layer for each ring that defines a closed path with 2 circles. There is something called the "winding rule" that lets you determine what happens when paths overlap. I think you'd want even-odd path winding (kCAFillRuleEvenOdd).
To make the rings equal area, you could do this:
First calculate the area of the whole circle. Divide by the number of rings. That's the desired area for each ring Let's call that area "a". Start from the center. The radius of that ring (a circle) will be sqrt(pi/a).
For each following ring you'll need to calculate the thickness of the ring based on the area of the outer circle minus the area of the inner circle that makes up the ring. You'll need to write an equation that solves for the outer radius given the desired area and the radius or the previous circle.