I want to search for nodes inside a circle - the circle is a SKShapeNode. The return node (SKSpriteNode) should be the nodes found inside the circle, with the lowest y-value.
For people who are interested, this is the code I use for the search:
SKSpriteNode *currentNode = [SKSpriteNode spriteNodeWithColor:[UIColor clearColor] size:CGSizeMake(0, 0)];
[currentNode setPosition:CGPointMake(0, 99999)];
SKShapeNode *circle = [SKShapeNode shapeNodeWithCircleOfRadius:100];
// adding circle and other nodes to scene
[self enumerateChildNodesWithName://* usingBlock:^(SKSpriteNode *foundNode, BOOL * _Nonnull stop) {
if ([circle containsPoint:currentNode.position]) {
if (currentNode.position.y > foundNode.position.y) {
currentNode = foundNode;
}
}];
// currentNode = node inside circle with lowest y
I don't like this solution, looks like it takes too much effort to find just the one node. I also tried using nodesAtPoint/nodeAtPoint but that does not work in my project - 'big' child nodes.
I am curious: is there an easier way to search for a node with specifics like this?
Checking the Y coordinate is going to be much faster than [circle containsPoint] so you might want to change the order of your condition to first compare the y coordinate of candidate nodes and only check circle containsPoint for nodes that have a y coordinate between the lowest point of your circle (-100) and the currentNode's Y coordinate.
something like :
if ( foundNode.position.y > -100
&& foundNode.position.y < currentNode.position.y )
{
if ([circle containsPoint:foundNode.position])
{ currentNode = foundNode }
}
note: i'm assuming enemy.position was actually meant to be foundNode.position.
Related
I have a following scene:
[Root Node]
|
[Main Container]
| |
[Node A Wrapper] [Node B Wrapper]
| |
[Node A] [Node B]
I've set up pan gesture recognizers in a way that when u pan in open space, the [Main Container] rotates in the selected direction by +/- Double.pi/2 (90deg). When the pan starts on one of the subnodes A, B (i'm hittesting for this on touchesBegan), i want to rotate the subnode along the direction of world axis (again 90deg increments).
I'm rotating the [Main Container] using convertTransform() from rootNode, which works fine, and the rotations are performed along the world axes - the position of main container is (0,0,0) which i believe makes it lot easier.
The reason why i wrapped the subnodes is so they have local positions (0,0,0) inside the wrapper, which should help with the rotation around their origin. But as they are rotated also when i perform rotate on [Main Container] , the direction of their local axes is changed and the rotation is performed around different axis than what i want.
In my (very limited) understanding of transformation matrices, i assume i need to somehow chain and multiply the matrices produced by convertTransform of the parent nodes, or to use the worldTransform property somehow, but anything i tried results in weird rotations. Any help would be appreciated!
I've set up a small sample project based on the SceneKit template, with controls similar as what you described. It's in Objective C but the relevant parts are pretty much the same:
- (void) handlePan:(UIPanGestureRecognizer*)gestureRecognize {
CGPoint delta = [gestureRecognize translationInView:(SCNView *)self.view];
if (gestureRecognize.state == UIGestureRecognizerStateChanged) {
panHorizontal = NO;
if (fabs(delta.x) > fabs(delta.y)) {
panHorizontal = YES;
}
} else if (gestureRecognize.state == UIGestureRecognizerStateEnded) {
SCNMatrix4 rotMat;
int direction = 0;
if (panHorizontal) {
if (delta.x <0) {
direction = -1;
} else if (delta.x >1) {
direction = 1;
}
rotMat= SCNMatrix4Rotate(SCNMatrix4Identity, M_PI_2, 0, direction, 0);
} else {
if (delta.y <0) {
direction = -1;
} else if (delta.y >1) {
direction = 1;
}
rotMat= SCNMatrix4Rotate(SCNMatrix4Identity, M_PI_2, direction, 0, 0);
}
if (selectedNode == mainPlanet) {
selectedNode.transform = SCNMatrix4Mult(selectedNode.transform, rotMat);
} else { //_selectedNode is a child node of mainPlanet, i.e. moons.
//get the translation matrix of the child node
SCNMatrix4 transMat = SCNMatrix4MakeTranslation(selectedNode.position.x, selectedNode.position.y, selectedNode.position.z);
//move the child node the origin of its parent (but keep its local rotation)
selectedNode.transform = SCNMatrix4Mult(selectedNode.transform, SCNMatrix4Invert(transMat));
//apply the "rotation" of the mainPlanet extra (we can use the transform because mainPlanet is at world origin)
selectedNode.transform = SCNMatrix4Mult( selectedNode.transform, mainPlanet.transform);
//perform the rotation based on the pan gesture
selectedNode.transform = SCNMatrix4Mult(selectedNode.transform, rotMat);
//remove the extra "rotation" of the mainPlanet (we can use the transform because mainPlanet is at world origin)
selectedNode.transform = SCNMatrix4Mult(selectedNode.transform,SCNMatrix4Invert(mainPlanet.transform));
//add back the translation mat
selectedNode.transform = SCNMatrix4Mult(selectedNode.transform,transMat);
}
}
}
In handleTap:
selectedNode = result.node;
In viewDidLoad:
mainPlanet = [scene.rootNode childNodeWithName:#"MainPlanet" recursively:YES];
orangeMoon = [scene.rootNode childNodeWithName:#"orangeMoon" recursively:YES];
yellowMoon = [scene.rootNode childNodeWithName:#"yellowMoon" recursively:YES];
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
[gestureRecognizers addObject:panGesture];
And local vars:
SCNNode *mainPlanet;
SCNNode *orangeMoon;
SCNNode *yellowMoon;
SCNNode *selectedNode;
BOOL panHorizontal;
MainPlanet would be your mainContainer and doesn't have to be visible (it does in my example because it has to be tapped to know what to rotate...). The two moons are your node A and B, child nodes of the main node. No wrappers necessary. The key part is obviously the commented portion.
Normally to rotate a child node in local space (IF the parent node is at 0,0,0)
First move it back to the node by multiplying its transform with the inverse of its translation only.
Apply the rotation matrix.
Apply the original translation we removed in step 1.
As you noticed that will rotate the child node on its local pivot point and over its local axis. This works fine until you rotate the parent node. The solution is to apply that same rotation to the child node before rotating it based on the pan gesture (step 2), and then after that remove it again.
So to get the results you desire:
First move it back to the node by multiplying its transform with the inverse of its translation only.
Apply the rotation of the parent node (since it's at 0,0,0 and I assume not scaled, we can use the transform).
Apply the rotation matrix based on the pan gesture.
Remove the rotation of the parent node
Apply the original translation we removed in step 1.
I’m sure there are other possible routes and perhaps instead of step 2 and 4 the rotation matrix could be converted to the main node using convert to/from but this way you can clearly tell what’s going on.
I have managed to draw a line with array of my random CGPoints.
-(void)drawLine
{
SKShapeNode *mainLine = [SKShapeNode node];
CGMutablePathRef pathToDraw = CGPathCreateMutable();
CGPathMoveToPoint(pathToDraw, NULL, 0, 0);
for(int i;i<pointsInMyView.count;i++)
{
CGPoint myPoint = [pointsInMyView[i] CGPointValue];
CGPathAddLineToPoint(pathToDraw, NULL, myPoint.x, myPoint.y);
mainLine.path = pathToDraw;
}
[mainLine setLineWidth:40];
[mainLine setStrokeColor:[SKColor whiteColor]];
mainLine.name = #"mainLine";
[self addChild:mainLine];
}
As you can see I am drawing a SKShapeNode. My goal is to check collision of my SKSpriteNode with my line. But of course, this shape node makes a frame that contains all points of my line, and in this case my ShapeNode is all over my view. My SpriteNode detects collision with this ShapeNode all the time.
I should draw multiple different ShapeNodes I guess, so every node would have its own frame. But if i do it this way, my line is not connected.
Is there some solution to draw this node by node and still get nice line.
Unfortunately nodes in SpriteKit cannot be concave (http://mathworld.wolfram.com/ConcavePolygon.html), with lines that cross over each other.
You will have to create separate nodes for each lines between adjacent points and then possibly join them together. you could do this either by:
Create the 1st segment and then add subsequent segments as children
of that 1st segment.
Create a non-visible node for the whole line and add wll the line
segments as children of that node.
Create physicsbodies for each segment and join them with
SKPhysicsJointFixed.
I currently have a large map that goes off the screen, because of this its coordinate system is very different from my other nodes. This has led me to a problem, because I'm needing to generate a random CGPoint within the bounds of this map, and then if that point is frame/on-screen I place a visible node there. However the check on wether or not the node is on screen continuously fails.
I'm checking if the node is in frame with the following code: CGRectContainsPoint(self.frame, values) (With values being the random CGPoint I generated). Now this is where my problem comes in, the coordinate system of the frame is completely different from the coordinate system of the map.
For example, in the picture below the ball with the arrows pointing to it is at coordinates (479, 402) in the scene's coordinates, but they are actually at (9691, 9753) in the map's coordinates.
I determined the coordinates using the touchesBegan event for those who are wondering. So basically, how do I convert that map coordinate system to one that will work for the frame?
Because as seen below, the dot is obviously in the frame however the CGRectContainsPoint always fails. I've tried doing scene.convertPoint(position, fromNode: map) but it didn't work.
Edit: (to clarify some things)
My view hierarchy looks something like this:
The map node goes off screen and is about 10,000x10,000 for size. (I have it as a scrolling type map). The origin (Or 0,0) for this node is in the bottom left corner, where the map starts, meaning the origin is offscreen. In the picture above, I'm near the top right part of the map. I'm generating a random CGPoint with the following code (Passing it the maps frame) as an extension to CGPoint:
static func randPoint(within: CGRect) -> CGPoint {
var point = within.origin
point.x += CGFloat(arc4random() % UInt32(within.size.width))
point.y += CGFloat(arc4random() % UInt32(within.size.height))
return point;
}
I then have the following code (Called in didMoveToView, note that I'm applying this to nodes I'm generating - I just left that code out). Where values is the random position.
let values = CGPoint.randPoint(map.totalFrame)
if !CGRectContainsPoint(self.frame, convertPointToView(scene!.convertPoint(values, fromNode: map))) {
color = UIColor.clearColor()
}
To make nodes that are off screen be invisible. (Since the user can scroll the map background). This always passes as true, making all nodes invisible, even though nodes are indeed within the frame (As seen in the picture above, where I commented out the clear color code).
If I understand your question correctly, you have an SKScene that contains an SKSpriteNode that is larger than the scene's view, and that you are randomly generating coordinates within that sprite's coordinate system that you want to map to the view.
You're on the right track with SKNode's convertPoint(_:fromNode:) (where your scene is the SKNode and your map is the fromNode). That should get you from the generated map coordinate to the scene coordinate. Next, convert that coordinate to the view's coordinate system using your scene's convertPointToView(_:). The point will be out of bounds if it is not in view.
Using a worldNode which includes a playerNode and having the camera center on this node, you can check on/off with this code:
float left = player.position.x - 700;
float right = player.position.x + 700;
float up = player.position.y + 450;
float down = player.position.y - 450;
if((object.position.x > left) && (object.position.x < right) && (object.position.y > down) && (object.position.y < up)) {
if((object.parent == nil) && (object.dead == false)) {
[worldNode addChild:object];
}
} else {
if(object.parent != nil) {
[object removeFromParent];
}
}
The numbers I used above are static. You can also make them dynamic:
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
Diving the screenWidth by 2 for left and right. Same for screenHeight.
Let's say I have two circles respectively at (0,0) and (0,1).
I have a SKPhysicsJoint between them and it is working good, now I want to separate them with a distance of 2 on runtime, meaning while physics are working in-game. How can I achieve this?
I've tried setting anchor points to (0,0) and (0,2) but something is bugged, although I see the joint it doesn't have any affect.
I want the circles smoothly push each other, as if the length of the spring has increased.
Everything works if I make a circle 'teleport' to a distance of 2 and then anchor the spring to it, but making a physics object teleport cause other bugs as you can guess.
Before adding the joint I 'teleported' the second object to the desired position, then added the joint and then 'teleported' back to the original position.
Here is the code piece:
SKSpriteNode* node1 = [_bodies objectAtIndex:loop];
SKSpriteNode* node2 = [_bodies objectAtIndex:loop-1];
CGPoint prev1 = node1.position;
CGPoint prev2 = node2.position;
node1.position = [((NSValue*)[positions objectAtIndex:loop]) CGPointValue];
node2.position = [((NSValue*)[positions objectAtIndex:loop-1]) CGPointValue];
[self AttachPoint:node1 secondPoint:node2 pos1:node1.position pos2:node2.posiiton] ;
node1.position = prev1;
node2.position = prev2;
it is working as it is, but I'm not sure how efficient this is. I wish SKPhysicsJointSpring had a 'length' parameter that can be changed over time.
I have a 50 X 50 isometric Tiled Map with base tile : 64 X 32.
I am using this function to create a sprite and add to a particular tile dynamically.
-(void)addTile:(NSString *)tileName AtPos:(CGPoint)tilePos onTileMap:(CCTMXTiledMap *)tileMap
{
CCTMXLayer *floorLayer=[tileMap layerNamed:#"FloorLayer"];
NSAssert(floorLayer !=nil, #"Ground layer not found!");
CGPoint tilePositionOnMap = [floorLayer positionAt:tilePos];
CCSprite *addedTile = [[CCSprite alloc] initWithFile:tileName];
addedTile.anchorPoint = CGPointMake(0, 0);
addedTile.position = tilePositionOnMap;
addedTile.vertexZ = [self calculateVertexZ:tilePos tileMap:tileMap];
[tileMap addChild:addedTile];
}
Floor layer is the only layer in my Tiled map and I have added the property cc_vertexz = -1000 to this layer.
I took the calculateVertexZ method from the KnightFight project. Based on the tile coordinates on isometric map it'll calculate the vertexZ and once you see the map it seems to make sense too.
-(float) calculateVertexZ:(CGPoint)tilePos tileMap:(CCTMXTiledMap*)tileMap
{
float lowestZ = -(tileMap.mapSize.width + tileMap.mapSize.height);
float currentZ = tilePos.x + tilePos.y;
return (lowestZ + currentZ + 1);
}
Now this is the code which i'm writing in the -init of my HelloWorldLayer of template cocos2d-2 project. -
self.myTileMap = [CCTMXTiledMap tiledMapWithTMXFile:#"IsometricMap.tmx"];
[self addChild:self.myTileMap z:-100 tag:TileMapNode];
[self addTile:#"walls-02.png" AtPos:CGPointMake(0, 0) onTileMap:self.myTileMap];
[self addTile:#"walls-02.png" AtPos:CGPointMake(0, 1) onTileMap:self.myTileMap];
[self addTile:#"walls-02.png" AtPos:CGPointMake(4, 1) onTileMap:self.myTileMap];
[self addTile:#"walls-02.png" AtPos:CGPointMake(4, 0) onTileMap:self.myTileMap];
Here's the wall image -
And here's the issue -
Case 1
(0,0) should be behind (0,1) according to calculateVertexZ method. And hence the sprite on ( (0,1) is rendered OVER sprite on (0,0).
Case 2
(4,0) should be behind (4,1) according to calculateVertexZ method. But somehow, because I'm adding block on (4,0) AFTER (4,1) its not giving me the desired results.
I had read that when two sprites have same vertexZ ONLY then whichever sprite was added later will be on top. But here the sprites have different vertexZ values, still order of creation is overriding that.
Also, I can't figure out what to do with zorder in this equation. SOMEBODY PLS HELP
I solved this by using the vertexZ property and zOrder property of the base tile.
-(void)addTile:(NSString *)tileName AtPos:(CGPoint)tilePos onTileMap:(CCTMXTiledMap *)tileMap
{
CCTMXLayer *floorLayer=[tileMap layerNamed:#"FloorLayer"];
NSAssert(floorLayer !=nil, #"Ground layer not found!");
CCSprite *baseTile = [floorLayer tileAt:tilePos];
CCSprite *addedTile = [[CCSprite alloc] initWithFile:tileName];
addedTile.anchorPoint = CGPointMake(0, 0);
addedTile.position = baseTile.position;
addedTile.vertexZ = baseTile.vertexZ;
[tileMap addChild:addedTile z:baseTile.zOrder tag:tile.tag];
}