0
\$\begingroup\$

I'm new to game development. Specifically server side. Currently I am trying to implement movement of monsters. From point A to point B.

I have used an a* pathfinding library to calculate the path to be taken but I do not know the logic on how to update the movement where the monster moves in 30 frames.

I am using javascript/node.js and currently I am doing something like:

// in game.update()
if (!monster.isMoving) {
  var steps = char.getPathTo(83, 213);
  steps.forEach(function (cell) {
    char.move(cell[0], cell[1]);
  });
}

Which of course is very wrong because it will evaluate all the movements first. So what are the possible approaches for this?

I followed this game loop:

var gameLoop = function () {
  var now = Date.now()

  actualTicks++
  if (previousTick + tickLengthMs <= now) {
    var delta = (now - previousTick) / 1000
    previousTick = now

    game.update(delta)

    actualTicks = 0
  }

  if (Date.now() - previousTick < tickLengthMs - 16) {
    setTimeout(gameLoop)
  } else {
    setImmediate(gameLoop)
  }
\$\endgroup\$
2
  • \$\begingroup\$ I guess behaviour trees could be an answer? \$\endgroup\$
    – majidarif
    Feb 8, 2015 at 0:24
  • \$\begingroup\$ Behavior trees are overkill for what you're trying to do. You'll want to look more into update loops. \$\endgroup\$
    – House
    Feb 8, 2015 at 2:26

1 Answer 1

1
\$\begingroup\$

Simple, give the monster a hspeed, vspeed speed components, after that find the direction to the next point in the path with the tangent, which if I recall correctly would be y2-y1/x2-x1, being x2 and y2 the point of the next position and x1 and y1 the monster one. After that use the cosinespeed to find the x component or hspeed and -sinespeed for the y component or vspeed (negative because mathematical cartesian graphs and functions have the y oppositive to the computer's).

In a summary, you create a "follow point", get the direction of it, get the components for each by finding first the direction, then multiply each component by your desired speed. After reaching the current point increment it so it starts following the next one.

And one trick more. For checking wether it has reached or not do this: if distance_to_next_point < speed { x = follow_point.x y = follow_point.y follow_point++ } Basically if the speed is bigger than the distance let's jump directly onto it, that way you avoid overlapping it and making your monster return back to it.

Hope this helps or gives you ideas :)

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .