Agent following path moves forward indefinitely
Asked Answered
E

0

6

I am trying to make a sprite move towards a point while avoiding some obstacles. The graph that I am using is a GKObstacleGraph, obtained from this scene:

enter image description here

For simplicity I decided not to use a circular physics body for the obstacles, but it's enough to use the sprites' bounds for now. So this is how I created the graph:

lazy var graph:GKObstacleGraph? =
{
    guard let spaceship = self.spaceship else { return nil }

    let obstacles = SKNode.obstacles(fromNodeBounds: self.rocks)
    let graph = GKObstacleGraph(obstacles: obstacles, bufferRadius: Float(spaceship.size.width))
    return graph
}()

When the user taps on any location of the scene, the spaceship should start moving toward that position by avoiding the obstacles:

func tap(locationInScene location:CGPoint)
{
    guard let graph = self.graph else { return }
    guard let spaceship = self.spaceship else { return }

    let startNode = GKGraphNode2D(point: vector_float2(withPoint: spaceship.position))
    let endNode = GKGraphNode2D(point: vector_float2(withPoint: location))

    graph.connectUsingObstacles(node: startNode)
    graph.connectUsingObstacles(node: endNode)

    let path = graph.findPath(from: startNode, to: endNode)
    print(path)
    let goal = GKGoal(toFollow: GKPath(graphNodes: path, radius: Float(spaceship.size.width)) , maxPredictionTime: 1.0, forward: true)
    let behavior = GKBehavior(goal: goal, weight: 1.0)
    let agent = GKAgent2D()
    agent.behavior = behavior

    graph.remove([startNode, endNode])
    spaceship.entity?.addComponent(agent)

    // This is necessary. I don't know why, but if I don't do that, I 
    // end up having a duplicate entity in my scene's entities array.
    self.entities = [spaceship.entity!]
}

But when I tap on a point on the scene, the spaceship starts moving indefinitely upwards. I tried to print the position of the GKAgent at every frame, and this is what I got:

enter image description here

With very low values, the spaceship still keeps moving upwards without stopping, ever.

This is my project on GitHub.

Epigeal answered 2/6, 2017 at 14:4 Comment(10)
What values are you getting as the input (location)?Alvertaalves
@Alvertaalves For example: (x = 68, y = 78.49). The points are already converted to the scene's coordinate system.Epigeal
if you can set up a minimal project on github or somewhere then I will download it and try to solve this for you. You obviously have a lot going on here outside of these two blocks--there is no way for me to reproduce this issue.Complement
I'm getting two errors, both in lazy var graph... it looks like it's not letting you use self.rocks or self.spaceship for some reason. I tried fixing it for a minute but I'm scratching my head myself here.Complement
@Complement Are you using Swift 3?Epigeal
i might need to update xcode, I'm on version 8.2.1 (8C1002). I know they released a new swift update since then.. crap I just remembered i'm on capitan.Complement
The line of code that causes the upward movement is this: rock.physicsBody = SKPhysicsBody(circleOfRadius: rock.size.width) inside your rocks computed property getter. I have no idea why this happens, but to avoid it you can go to GameScene.sks, highlight all the rocks, go to the attributes inspector, and under Physics Definition, select Bounding Circle for Body Type. Make sure you deselect Dynamic, Allows Rotation, and Affected By Gravity. Now your rocks will have physics bodies, and your ship will not move up. Though it looks like your entity behavior still doesn't work...Destitution
did you ever figure this out?Complement
@Complement no, it's still unsolved. The bounty is closed, but if someone solves the problem I will open another bounty and reward him with 50 points.Epigeal
i still have this on my todo list. I will be upgrading to sierra soon and will be able to take a whack at it.Complement

© 2022 - 2024 — McMap. All rights reserved.