GameplayKit Pathfinding with obstacles and agents - sprite-kit

I've been searching for days about this new framework and trying to make usage of some of it's funcionalities,
but... there're some things that are not fitting together for me, the demobot source code isn't helping at some points,
I miss some kind of simple tutorial but here goes my main doubts:
let obstacles = scene["obstacle"]
polygonObstacles = SKNode.obstaclesFromNodePhysicsBodies(obstacles)
graph = GKObstacleGraph(obstacles: polygonObstacles, bufferRadius: 60.0)
func drawGraph() {
for node in graph.nodes as! [GKGraphNode2D] {
for destination in node.connectedNodes as! [GKGraphNode2D] {
let points = [CGPoint(node.position), CGPoint(destination.position)]
let shapeNode = SKShapeNode(points: UnsafeMutablePointer<CGPoint>(points), count: 2)
shapeNode.strokeColor = SKColor(white: 1.0, alpha: 0.5)
shapeNode.lineWidth = 5.0
shapeNode.zPosition = 3
scene.addChild(shapeNode)
}
}
}
So, when I try to draw this graph and see the connections, I get this: http://i.imgur.com/EZ3dx5v.jpg
I find it really weird, anywhere I put my obstacles, even in low numbers, the left-corner portion of the screen have always more connections(the radius don't have influence on that)
I don't use GKComponents on my game, but I tried to run some GKAgents2D to hunt the player, like this:
func calculateBehaviorForAgents(){
let mainCharacterPosition = float2(scene.mainCharacter.position)
let mainCharacterGraphNode = GKGraphNode2D(point: mainCharacterPosition)
graph.connectNodeUsingObstacles(mainCharacterGraphNode)
for i in 0...monsters.count-1{
let monster = monsters[i]
let agent = agents[i]
let behavior = GKBehavior()
let monsterPosition = float2(monster.position)
let monsterGraphNode = GKGraphNode2D(point: monsterPosition)
graph.connectNodeUsingObstacles(monsterGraphNode)
let pathNodes = graph.findPathFromNode(monsterGraphNode, toNode: mainCharacterGraphNode) as! [GKGraphNode2D]
let path = GKPath(graphNodes: pathNodes, radius: 00.0)
let followPathGoal = GKGoal(toFollowPath: path, maxPredictionTime: 1.0, forward: true)
behavior.setWeight(1.0, forGoal: followPathGoal)
let stayOnPathGoal = GKGoal(toStayOnPath: path, maxPredictionTime: 1.0)
behavior.setWeight(1.0, forGoal: stayOnPathGoal)
agent.behavior = behavior
graph.removeNodes([monsterGraphNode])
}
graph.removeNodes([mainCharacterGraphNode])
}
Now when I call the updateWithDeltaTime method, his delegate methods:
func agentWillUpdate(agent: GKAgent){}
func agentDidUpdate(agent: GKAgent){}
give me unexpected values for the agents, it's position doesn't make any sense, with giant numbers that leads to outside of the battlefield
But I saw that the his velocity vector were making sense, so I matched it to my monster and updated the agent to the monster's position
func updateWithDeltaTime(currentTime : CFTimeInterval){
for i in 0...monsters.count-1{
let agent = agents[i]
let monster = monsters[i]
monster.physicsBody?.velocity = CGVectorMake(CGFloat(agent.velocity.x), CGFloat(agent.velocity.y))
agent.updateWithDeltaTime(currentTime)
agent.position = float2(monster.position)
monster.gameSceneUpdate(currentTime)
}
Now I was getting some results, but it's far away from what I want:
The monsters are not following the character to the edges or the right-top portion of screen, I remove their points from the graph but after make a path for them to follow (the image doesn't have this points, but they exists).
Apparently because there was no path leading to there, remember the image?
The question is: how to make this agent system to work?
Maybe I'm totally wrong at the usage of agents, goals and even the graphs! I read the documentation but I still can't make it right
And more...
At first, the monster were not avoid obstacles, even with GKGoals like "avoidObstacles", passing the same PolygonObstacles,
but when I change
graph.connectNodeUsingObstacles(mainCharacterGraphNode)
graph.connectNodeUsingObstacles(monsterGraphNode)
to
graph.connectNodeUsingObstacles(mainCharacterGraphNode, ignoringObstacles: polygonObstacles)
graph.connectNodeUsingObstacles(monsterGraphNode, ignoringObstacles: polygonObstacles)
it worked! o.O
I really need some help, thank you all :D!

Related

How do I get SK sequences to run in swift

I found this pretty useful code online but I'm having trouble getting it to run. The variable names are all correct and I've used print statements to make sure it does make it to this function. It just doesn't seem to run the sequence on the Label nodes. Thanks
func fadeOutInfoText(){
infoLabel1.removeAllActions()
infoLabel2.removeAllActions()
speechIcon.removeAllActions()
let wait:SKAction = SKAction.wait(forDuration: 0.5)
let fade:SKAction = SKAction.fadeAlpha(to: 0, duration: 0.5)
let run:SKAction = SKAction.run {
self.infoLabel1.text = ""
self.infoLabel2.text = ""
self.infoLabel1.alpha = 1
self.infoLabel2.alpha = 1
self.speechIcon.alpha = 1
self.speechIcon.isHidden = true
}
let seq:SKAction = SKAction.sequence([wait,fade,run])
let seq2:SKAction = SKAction.sequence([wait,fade])
infoLabel1.run(seq)
infoLabel2.run(seq2)
speechIcon.run(seq2)
}
NOTE: This would be a comment (not enough reputation to do so yet :)
Executing the above code line-for-line (and adding in the nodes in an empty scene) gives what appears to be the desired result. Presumably you are not calling this function from the scene's update(_:) method, for this keeps the labels and speech icon from doing anything since the actions are removed before the scene performs the actions (see here). Make sure you aren't removing all actions and changing the alpha of the labels before this set of actions can complete elsewhere.
This is a sample code for Sequence.
let sprite = SKSpriteNode(imageNamed:"Spaceship")
let scale = SKAction.scale(to: 0.1, duration: 0.5)
let fade = SKAction.fadeOut(withDuration: 0.5)
let sequence = SKAction.sequence([scale, fade])
sprite.run(sequence)
Let me know it is useful or not.

ARkit SCNMorpher isn't working for me. No errors, just no shape changes

I'm trying a simple example using SCNMorpher to blend between to poly spheres. They are identical in topology except for the position of the points
Each is stored in a .scn file and I get the shapes like:
sphereNode = SCNReferenceNode(named: "sphere")
sphereNode2 = SCNReferenceNode(named: "sphere2")
sphereNode?.morpher = SCNMorpher()
sphereNode!.morpher?.targets = [(sphereNode2?.childNodes.first!.geometry)!]
sphereNode!.name = "EFFECT"
I'm using the faceAnchor blend shapes to drive it
if let effectNode = sceneView?.scene.rootNode.childNode(withName: "EFFECT", recursively: true) {
let v = faceAnchor?.blendShapes[ARFaceAnchor.BlendShapeLocation.jawOpen]
effectNode.morpher?.setWeight(v as! CGFloat, forTargetNamed: "sphere2")
}
I've also tried:
...
effectNode.morpher?.setWeight(v as! CGFloat, forTargetAt: 0)
...
The code runs.. I can print values for v.. they change as I open/close my jaw and that value is passed to the morpher. I see the base sphere shape but it never deforms toward the sphere2 shape. Am I suppose to do anything else to force it to redraw or calc the deformation?
Hmm. looks like I was attaching the morpher to the parent of the shape, not the actual sphere.. funny how asking a question here sometimes creates that "Ah Ha" moment. Reading in my spheres like this fixed it:
sphereNode = SCNReferenceNode(named: "sphere").childNodes.first
sphereNode2 = SCNReferenceNode(named: "sphere2").childNodes.first
sphereNode?.morpher = SCNMorpher()
sphereNode!.morpher?.targets = [(sphereNode2.geometry)!]
sphereNode!.name = "EFFECT"

SKAction with Key changes Sprite Behavior

I've been learning Swift while laid up from back surgery and any help with this is greatly appreciated.
Issue Description:
I have a function that pushes a sprite backwards when hit. When I use
self.runAction(jumpBackSequence)
I get the desired behavior. (The sprite is pushed back a good distance.)
Now when I update this to use "withKey" (because I need to be able to stop the forward action again on the next contact the forward movement is greatly dimished.
self.runAction(jumpBackSequence, withKey: "moveStraightLine")
My function creating this action is below:
func pushBack(){
self.removeActionForKey("moveStraightLine")
let height:CGFloat = 5
let jumpDistanceA:CGFloat = 20
let jumpDistanceB:CGFloat = 20
let duration:NSTimeInterval = 0.15
let jumpUp = SKAction.moveByX(0, y:height, duration:duration)
let moveBack = SKAction.moveByX(jumpDistanceA, y: 0, duration: duration)
let jStep1 = SKAction.group([jumpUp, moveBack])
let jumpDown = SKAction.moveByX(0, y:(height * -1), duration:duration)
let moveBack2 = SKAction.moveByX(jumpDistanceB, y: 0, duration: duration)
let jStep2 = SKAction.group([jumpDown, moveBack2])
let moveZombie1 = SKAction.moveToX(0, duration: spawnSpeed1() )
let removeZombie1 = SKAction.removeFromParent()
let moveAndRemoveZombie1 = SKAction.sequence([moveZombie1, removeZombie1])
let jumpBackSequence = SKAction.sequence([jStep1, jStep2, moveAndRemoveZombie1])
self.runAction(jumpBackSequence)
// self.runAction(jumpBackSequence, withKey: "moveStraightLine")
}
You are using moveTo, not moveBy, so when you add the action back in to move the zombie, you are now saying starting from the closer spot, take X seconds to move to the end, not take X seconds - the time already travelled. You do not want this due to the effect you see. Use moveBy, and the zombie speed will stay consistant
I'm not sure why this behavior happened, but I realized that the runAction withKey was actually moving the code the specified distance where as the example without the key was not. When I updated the code to use a fraction of screen width it behaves the same with and without a key - which makes me think this might actually be an minor bug in Swift.
To fix the issue I updated the distances to
let jumpDistanceA:CGFloat = (self.scene?.size.width)! * 0.15
let jumpDistanceB:CGFloat = (self.scene?.size.width)! * 0.15
I hope this helps someone else.
Best regards,
mpe

Pathfinding no longer works after blocking and unblocking path

I have a map with obstacles that can be placed by the user. And sprites that move from the right of the map to the left.
As the user places obstacles they're added to my game's graph (GKObstacleGraph) and each time I check if a path from the start point to the end point is available. If it's not, it means the user has blocked the path entirely, so I remove the placed obstacle.
That should open the path up again, however it doesn't. It's as if the obstacle is still there.
I have a debug function which loops through all the obstacles in my graph node, showing me visually where they are. It runs each time I add an obstacle. And after I place an obstacle that blocks the path (which is then removed immediately) it shows that there is no obstacle there. The path is clear.
func createObstacle(coordinates: CCCordinates) {
let tX = -game.mapSize.widthHalf + (coordinates.x*game.tileSize.value) + (game.tileSize.valueHalf)
let tY = game.mapSize.heightHalf - (coordinates.y*game.tileSize.value) - (game.tileSize.valueHalf)
let obstacle = CCObstacle(color: color.red, size: game.obstacleSize.size)
obstacle.position = CGPoint(x: tX, y: tY)
let polygonObstacle = getPolygonObstacleForSpriteNode(obstacle)
game.graph.addObstacles([polygonObstacle])
let pathClear = checkPath()
print("Path Clear: \(pathClear)")
if pathClear {
let texture = SKTexture(imageNamed: "obstacle")
obstacle.texture = texture
obstacle.coordinates = coordinates
obstacle.name = "obstacle:\(obstacle.coordinates.convertToString())"
obstacle.zPosition = 5
for var i = 0; i < game.sprites.count; i++ {
let sprite = game.sprites[i]
updatePathForSprite(sprite)
}
panels.map.addChild(obstacle)
} else {
game.graph.removeObstacles([polygonObstacle])
print("removedObstacle")
}
drawAllObstacleOutlines()
}
This code works perfectly until pathClear returns false and the code in else runs. Even though the obstacle is removed from the graph (and the drawAllObstacleOutlines() function confirms that) pathClear always returns false afterwards.
Unless I use:
game.graph.removeAllObstacles()
If I replace the removeObstacles([]) line with that line above. It lets me place obstacles again. (It works even if I add back all the obstacles that were removed, excluding the one that blocked the path.)
The function updatePathForSprite basically calls this one below:
func getPathForNodeToEndPoint(startPoint: CGPoint) -> [GKGraphNode] {
let startNode = GKGraphNode2D(point: float2(Float(startPoint.x), Float(startPoint.y)))
let endNode = GKGraphNode2D(point: float2(Float(game.finishPoint.x), Float(game.finishPoint.y)))
game.graph.connectNodeUsingObstacles(startNode, ignoringBufferRadiusOfObstacles: game.outerTileObstacles)
game.graph.connectNodeUsingObstacles(endNode, ignoringBufferRadiusOfObstacles: game.outerTileObstacles)
let path:[GKGraphNode2D] = game.graph.findPathFromNode(startNode, toNode: endNode) as! [GKGraphNode2D]
game.graph.removeNodes([startNode, endNode])
return path
}
Does anyone know what's going on here?
EDIT: I may have found something weird.
When I add the following lines above the drawAllObstacleOutlines() line:
print("Obstacles \(game.graph.obstacles.count)")
print("Nodes \(game.graph.nodes!.count)")
It increases as I add obstacles... In my case, nodes is 404 and obstacles is 101, however when I place the obstacle that blocks the path, the output is: obstacles 101, Nodes 0
For some reason it's removing all the other nodes in the graph, even though I only removed one obstacle.

Running Stick Figure in Sprite Kit Animation

I am playing around a little bit in SpriteKit, and am trying to make a stick figure run. Due to my lack of experience, I believe I have started this process in an unconventional way.
Instead of drawing out each individual motion frame, I loaded the Separate images of the stick figure (ie the Head, the knee, the Thigh, the body, the arm, etc.) and told them to move accordingly. So far, everything fits together except for the motion and rotation of knee during the running sequence.
My problem is this:
When the thigh pulls back in it's stride, the knee becomes detached, and follows a straight line to the end of the stride while rotating.
My Question is this:
Is there a way to connect the thigh node to the calf node, as if it they were attached at a "knee" per say, while in motion? or is there some more efficient way?
Here is a snippet of my code, It may be a little more descriptive than I have been.
func thigh_run(){
//running config
//DONT CHANGE!
var thigh_rotation = CGFloat(M_PI)*0.8
//Can Change :)
var run_speed = NSTimeInterval(0.25)
// reset the action sequence... basically
let pos_nuetralize = SKAction.rotateByAngle(thigh_rotation/2, duration: run_speed)
let neg_nuetralize = SKAction.rotateByAngle(-thigh_rotation/2, duration: run_speed)
//strides
let thigh_pull = SKAction.rotateByAngle(-thigh_rotation, duration: run_speed*2)
let thigh_stride = SKAction.rotateByAngle(thigh_rotation/2, duration: run_speed)
// for the opposite leg
let b_thigh_pull = SKAction.rotateByAngle(-thigh_rotation/2, duration: run_speed)
let b_thigh_stride = SKAction.rotateByAngle(thigh_rotation, duration: run_speed*2)
//knee movement
let stride = SKAction.sequence([thigh_stride, thigh_pull, pos_nuetralize])
let pull = SKAction.sequence([b_thigh_pull, b_thigh_stride, neg_nuetralize])
r_arm.runAction(SKAction.repeatActionForever(stride))
f_leg_thigh.runAction(SKAction.repeatActionForever(stride))
b_leg_thigh.runAction(SKAction.repeatActionForever(pull))
//knee_run(run_speed)
}
func knee_run(speed: NSTimeInterval){
let knee_stride = SKAction.moveTo(CGPointMake(f_leg_thigh.position.x+f_leg_thigh.size.height, f_leg_thigh.position.y), duration: speed)
let knee_reset = SKAction.moveTo(CGPointMake(f_leg_thigh.position.x, f_leg_thigh.position.y-f_leg_thigh.size.height), duration: speed)
let knee_pull_rotate = SKAction.rotateToAngle(CGFloat(M_PI)/2, duration: speed, shortestUnitArc: true)
let knee_pull_rotate_half = SKAction.rotateToAngle(CGFloat(M_PI)*2, duration: speed, shortestUnitArc: true)
let knee_pull_half = SKAction.moveTo(CGPointMake(f_leg_thigh.position.x-f_leg_thigh.size.height, f_leg_thigh.position.y/2), duration: speed)
let knee_pull = SKAction.moveTo(CGPointMake(f_leg_thigh.position.x-f_leg_thigh.size.height, f_leg_thigh.position.y), duration: speed)
//Grouping of the knee moving and rotating at the same time
let knee_pull_group_half = SKAction.group([knee_pull_half, knee_pull_rotate_half])
let knee_pull_group = SKAction.group([knee_pull, knee_pull_rotate])
let f_knee_stride_seq = SKAction.sequence([knee_stride,knee_reset,knee_pull,knee_reset])
let f_knee_pull_seq = SKAction.sequence([knee_pull_group_half, knee_reset, knee_stride, knee_reset])
f_leg_calf.runAction(SKAction.repeatActionForever(f_knee_stride_seq))
b_leg_calf.runAction(SKAction.repeatActionForever(f_knee_pull_seq))
}
I understand it may be hard to read, I have Documented it for myself pretty well. the f_ prefix is for "front"leg and b is for "back"_leg. think of it as right leg and left leg however.
Anyway! Thanks in advance! I appreciate any help or tips! Comment for any further explanation! :)
The more efficient way is to use SKPhysicsJoint to connect the thigh node to the calf node.