Pathfinding - How To Move Player Along Path

Started by
3 comments, last by frob 7 years, 8 months ago

Hi everyone!

I come with a general question however I've also tried to solve it and failed. Not miserably - it half works....

Anyway, after some sort of path finding algorithm has been executed and we're left with an array containing path nodes, how do we move a player along that path inside a game loop? I've attempted to research this and have fallen short so, instead, I gave it try.

Please see this CODE PEN with my efforts (left click on the canvas to find a path). The path finding is being done with QIAO's pathfinding.js and works really well! I'm quite happy with it.

You'll see the start of the path finding code around line 276 in the code pen JS window. The part where I'm trying to move the player begins at line 313.

There are 2 major problems with it:

  1. The player will move up and/or left but not down and/or right. I have a suspicion that it has something to do with the fact that positions are based off of the top left corner and I haven't taken this into account. I did think of this and tried to fix it by working with the player's center and the node ending center.. to no avail.
  2. For some reason the player's speed is completely ignored. You can set the player's speed on line 122. Doesn't matter if it's 1 or 20, he still moves to the destination (as long as it's up and/or left) at the same speed. This isn't good.

NOTE: If I set the player speed to 20 it actually seems to fix the issue stated in #1...

jackie_chan_meme_by_firefox2014-d8p19a4.

Anyway, if anyone can help, it would be very much appreciated. I tried to strip out as much unnecessary code as possible.

Thanks,

SD

Advertisement
Just grasping at straws here since I didn't dig too deep into your code, but the physics update function looks... wonky.

maxSpeed should be compared to the magnitude of the velocity vector, not the components of the vector, for one thing. But something smells off in that code overall. Try simplifying it by removing acceleration and drag, and just applying a velocity vector to the current position vector every tick. That might reveal something.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Just grasping at straws here since I didn't dig too deep into your code, but the physics update function looks... wonky.

maxSpeed should be compared to the magnitude of the velocity vector, not the components of the vector, for one thing. But something smells off in that code overall. Try simplifying it by removing acceleration and drag, and just applying a velocity vector to the current position vector every tick. That might reveal something.

Ok, I've adjusted my CodePen, however, it remains the same.

You do bring up a good point, though. That ApplyPhysics() function was converted from C# from a sample game that came with XNA (now MonoGame) and I wasn't sure how to multiply 2 vectors in javascript.

i.e.: this.pos * this.velocity;

as opposed to what I had; this.pos.x * this.velocity.x //etc...

************

** SOLVED **

************

OK, I've solved this issue by doing the following (CodePen has been updated).

  1. Commented out the line where I was adjusting the player's position to snap to the grid.
  2. Reverted back to using the player x and y (not the centers) and the path coordinate x and y (not centers)

That's it. Now it can travel at any speed and go all directions.

Apologies if I've wasted anyone's time. I've literally been staring at this for hours.

This is a discussion site, not a Q&A site. Please don't modify titles to "solved".

Thank you for stating what you did to fix it, but we don't mark threads that way since people might have more discussion about the problem or the solution.

This topic is closed to new replies.

Advertisement