An update on Gwen - we have path follow.

Posted on February 19, 2012 | 5 minute read

Today was filled with boxes, furniture, moving, and being squished in a car. It was awesome. Tiring, but awesome. Ellen and Baz were helping me move into the new place, which was amazing of them. After the first load was dropped off at the new house and we were back for the second we took a short break to relax. Naturally, the conversation drifted to games. It always does. Specifically, I expressed my frustration at being stuck on Gwen.

The problem

Just follow the fricking path.

The problem, you see, is that I’m building an array of points for particular entities to move to. The player clicks any number of nodes which get added to an array, then clicks a button to spawn the entities and start them moving. An if statement decides whether the entity should be moving or staying still depending on its position in relation to each node.

Now the issue was that if I set the pos.x and pos.y of the moving entity to that of each node, then used shift() to remove the first item in the array until it got to the end, everything was just dandy. The entity was instantly cycling its position through each node. However when I tried to make the entities move to each node gradually using vel.x and vel.y, they kept jumping back and forth after reaching the node, hovering around it. As a result they never met the condition to shift the first item in the array and move on to the next node.

The solution

Giving it a bit of leeway

I thought that the issue was my noobishness and not understanding something basic. It kind of was, but Baz let me know that this is actually a common problem - he’s seen it before - and that the issue was likely the moving entity overshooting the first node slightly, then jumping back and forth attempting to reach it but never quite falling onto the exact coordinates. One thing I could try was create a range around the node’s pos.x and pos.y, to give the moving entity allowance to hit within that range as opposed to trying to force it to hit the node’s exact coordinates.

So after a bit of fiddling around, Ellen and Baz musing over potential setbacks, I ended up with a working path follow! My overall code structure is still really messy and I’ll need to now restructure lots of things as I realized something I didn’t consider for the future, but right now it works and it looks like this:

[crayon] // If there is at least one node in the path, do this if (ig.game.patharray.length > 0 ) { console.log(ig.game.patharray.length); // Find the angle to the first element in the array var angle = this.angleTo( ig.game.patharray[0] );

 //If the entity is at the first element in the array, stop moving and remove said element
 if (this.pos.x < = ig.game.patharray[0].pos.x + 10 && this.pos.x >= ig.game.patharray[0].pos.x - 10 && 
      this.pos.y < = ig.game.patharray[0].pos.y + 10 && this.pos.y >= ig.game.patharray[0].pos.y - 10){
      ig.game.patharray.shift();
      this.vel.x = 0;
      this.vel.y = 0;
 }

 //If the entity is not at the first element in the array, move toward the first element
 else {
      this.vel.x = Math.cos(angle) * this.speed;
      this.vel.y = Math.sin(angle) * this.speed;
 }

} [/crayon]

As a result, visually the user can barely tell that the path may not be hitting the coordinates of the node exactly (and the actual node entity is larger than 1px, so to the naked eye it is hitting that node), and my entity moves from node to node until it gets to the end of the array. Yay!

Of course upon further discussion about what I’m actually trying to do I’ve realized that I need to do away with the single global array and create individual arrays for various entities instead, but I won’t get into that now. It’ll just require some re-routing.

Reading

I want fiction, but this is what I get

The Art of Readable CodeI haven’t read any fiction in a while because I’ve been too focused on other things. Like the last thing I was making, and now Gwen. I need to learn to manage my time better and make time for both reading and playing games. Both of these things are important. It just seems like now that I’m in learning mode I have to keep going - it’s all I seem to think about in my spare time. I did recently go to Boffins and pick up The Art of Readable Code by Dustin Boswell and Trevor Foucher. As you can likely see above, while I try to keep my code neat there is probably a lot of room for improvement. Hopefully this will help me to keep my code legible and to not end up with a jumble of crap that even I can’t understand a few weeks down the line.

So now

It’s tea time

After the moving the three of us squished into the car again to get some dinner. And on the way back the conversation, as always, drifted to games. We talked about Deus Ex and Modern Warfare and Skyrim and BF and Batman and, again, linear games (which the three of us seemed to be in agreement on - something that I haven’t noticed a lot of when it comes to this particular topic). I am now logging off for the night…I think…to make a cup of tea and then, finally, go to bed.

Liza out.




Categories:game dev dev games
comments powered by Disqus