Velocity field in SpriteKit - sprite-kit

Anyone got Velocity Field in SpriteKit working?
Granted, I'm not sure I understand how it's supposed to work, but I'm wiring it up inside the Xcode UI, not code. My intention is to have dynamic spritenodes be pulled to a direction if they enter the area of that velocity field. Seeing as the velocity field node does not have a size of its own, I just assume I have to add it as a child of another spritenode (calling that areaofvelocityfield) which is not physical, but has a size, so that other dynamic spritenodes can enter into it and get affected by the force. So selecting the Velocity Field node, I select areaofvelocityfield.
Now, whatever I do, any dynamic node is affected by that force, whether or not they are inside areaofvelocityfield or in any vicinity of it. Whatever distance they are from the node containing the velocity field, and the velocity field itself, they are always affected and pulled in the vector of the field.
iOS 9.2.1. Xcode 7.2.

So it seems that .region must be set to the SKFieldNode. This cannot be done from Xcode. At least it cannot be done like this, which is my current code which works well:
SKSpriteNode* belt = (SKSpriteNode*)[self childNodeWithName:#"belt"];
SKFieldNode* myfield = (SKFieldNode*)[self childNodeWithName:#"myfield"];
myfield.region = [[SKRegion alloc] initWithSize:CGSizeMake(belt.size.width * 0.5, 10)];
Note that I am trying to make something like a "conveyor belt". The belt sprite is just a rectangle. The myfield is positioned at the same position as the belt. All nodes are placed directly onto the world, rootnode, no parenting is needed. Placing a field inside another node doesn't really have any effect as to the region it affects (or it might have, not sure here). For some reason I had to set the force field's width to half of the visible belt, or it would keep affecting whatever dynamic node that fell down onto the belt for too long.
Other values I used for the field are: strength 1, falloff 10, min. radius 1. Not sure what really made a difference, as this cannot be easily tested in the Xcode interface. A lot of trial and error is needed to figure out how this all works.
Regarding the size of a region: as the docs states, the size protrudes from the origin in all directions which means the width and height will actually be twice what you set.

Related

How to find the center in unity 2d?

I want to create 2d game. The game is similar to the Scale Puzzle. I've already created nearly all the functionality. Only the last remains.
.
This is example.
And that's how I draw shapes.
.
I click inside a white square, and after 1 seconds a square is drawn regardless of the position of the ball. (x,y).
Square is created programmatically, and it is added to the parent element "SquaresList" with name New Game Object.
How can i do, so that the violet field becomes larger, and in the middle of the screen.
I made it so that for every 3 clicks, "SquaresList" increases Scale by 0.25f, and get negative position of the ball. Example:
SquareList.transform.position = new Vector2(-ball.pos.x, -ball.pos.y)
This does not work correctly.
What options can be? (intersections, find the max/min point, math formulas) ?
Hi NoName :) Your method
SquareList.transform.position = new Vector2(-ball.pos.x, -ball.pos.y)
would work perfectly if your object does not inherit other objects and there positions. To fix this make sure that you reset the position of all other game objects that you use as containers.

SpriteKit grand-children of views overlap zPosition

I've started implementing an iOS game with swift and SpriteKit.
I have an object called "bubble" which basically is an SKSpriteNode (with zPosition=0, with image) that have a child (which is an SKCropNode of a person image cropped to a circle, with zPozition=1).
That's ok if one bubble covers another bubble as a whole, but somehow it seems like the bubbles are partially covered with the person images.
a demo picture is provided: (my final output should be that bubble1 will be on top of child-of-bubble-2)
Maybe the problem is that SpriteKit ignores child ordering and just set any node with a zPozition to be in it's correct place? That is - all the bubbles are drawn first because they have zPosition=0 and then all the bubble-children are drawn, as they all have zPosition=1?
If this is the case, what can I do to make sure all bubble parts are drawn together (and other bubbles can cover that bubble, I don't care) knowing that I have a dynamic amount of bubbles?
well according to this SO answer, indeed all the zPosition values are calculated before drawing.
I ended up creating a counter for bubbles, adding 1 every time a bubble has been added, and assinging the counter value as its zPosition.
And inside the bubbles, I made sure every child has a zPosition in the range (0, 1)
If you need to draw things in a precise order, then I suggest you to read section Understanding the Drawing Order for a Node Tree of SKNode Apple's documentation. Everything is correctly describe in details.

Swift SKSpriteNode: Chaining Sprites by Image Feature

Context
For the purpose of a MWE we will be using the following image of a stick figure:
with the goal of having a chain of these sprites move, hand-in-hand, across the screen:
It is worthwhile to note that the stick figure image itself is wider than the arm-span of this stick figure. The background is, however, transparent.
Depending on the application, one may make a class that either inherits from SKSpriteNode or encapsulates it, e.g. a class called Person, to store additional information, where there may be an array var people = [Person]().
Questions
1.) Suppose you had two instances of the aforementioned Person class with each sprite taking a stick figure image. How could one position them - programmatically - such that the sprites are touching ''hand in hand'' although the image has a transparent background? Of course one could spend some time fiddling about to get find a spacing parameter to ensure this is achieved, but that parameter would always have to be, via trial-and-error, re-calculated if the sprites were re-scaled.
2.) Given a chain of these sprites, hand in hand, how could one animate them to move across the screen at the same velocity? If one calculates the spacing parameter alluded to in 1.) then an SKAction could be given to each Person such that their end position is offset (but total distance traveled is the same), where the TimeInterval is maintained the same. Is there a way to tell all the sprites to move to the left until off the screen at a rate of $x$ pixels per second?
It looks like you've mostly answered your own questions already, but here are some additional ideas:
Make the spacing value proportional to the size of the sprite.
Yes, there is an SKAction that moves a sprite a given distance over a given period of time (effectively a velocity): let moveAction = SKAction.moveBy(x: 10, y: 0, duration: 2)

.dae model disappears when approaching

When I move towards my .dae imported model, it disappears. I'm not "inside" the mesh yet, visibly at least, so I don't know what the deal is.
It looks like your object is closer than the scene-view camera's "Near Clip Plane", and is not being rendered as a result. The default editor "near clip plane" distance is around 0.3 units, so it shouldn't normally interfere with your objects.
Check that your object scale is correct. If your object is very small, the scene camera's near clip plane will seem much farther in comparison, and will appear to clip objects more aggressively.
You can create a default "Cube" primitive to check the size of your objects. Cubes are 1 unit in all dimensions by default, and most of the time it's a good idea to roughly map one unit to a real-world scale of 1 meter. If your object is considerably smaller than the cube, you may want to try scaling them up and seeing if that helps.
F key is a shortcut key that will automatically zoom and focus to an object. Select the GameObject and press F. This problem should be gone.
If the problem is still there, select the Camera and change the Clipping Planes Near to 0.3 and Far to 50000. You can mess with these values until Object stops disappearing. Although, pressing F should solve it.

How to stop SKSpriteNodes changing zPosition on touch

I am having an issue with the zPosition undesirably changing when a node is touched.
I can't give a simple code example because currently my scene and rendering is quite complex. Everything is drawn correctly when the scene is presented and for the most part besides as described below it works.
So the issue explained - we have a base image node correctly rendered below a tower node (tower defence game). The tower is circular and the base image is square, so the corners stick out. If you manage to touch the corners, for some reason beyond my knowledge, the zPosition changes to above the tower.
There is no particular code on touches to change any zPositions of anything in the game.
I know this is a shot in the dark, but any insight would be great and much appreciated.
If your view's ignoreSiblingOrder property is set, you should set the zPosition of each node, appropriately, to ensure that they are drawn in the correct order.
From Apple's documentation...
The default value (of SKView's ignoreSiblingOrder property) is NO,
which means that when multiple nodes share the same z position, those
nodes are sorted and rendered in a deterministic order. Parents are
rendered before their children, and siblings are rendered from eldest
to youngest. When this property is set to YES, the position of the
nodes in the tree is ignored when determining the rendering order.
The rendering order of nodes at the same z position is arbitrary and may change every time a new frame is rendered [emphasis added].