1
\$\begingroup\$

I'm currently involved in a college assignment where we must create a game with our own game engine. In my game, we have an enemy moving around with paths constructed with a A* algorithm. Every frame I update his position but this is too dependent on the computer. Can you guys help to figure out how to solve this?

I tried to make him move only in even frames but, again, it's too time dependent and it's not smooth.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Possible duplicate of How to incorporate delta time into movement? \$\endgroup\$
    – Ocelot
    Commented Jul 7, 2019 at 13:56
  • \$\begingroup\$ Which game engine do you use? \$\endgroup\$ Commented Jul 7, 2019 at 17:13
  • 1
    \$\begingroup\$ @BonecoSinforoso "a game with our own game engine", that implies they are making their own. \$\endgroup\$
    – Ocelot
    Commented Jul 7, 2019 at 17:28
  • 1
    \$\begingroup\$ Welcome to GDSE. Can you eidt to clarify what you mean by 'too dependent'? Are you re-running A* every frame & finding that it's too slow? \$\endgroup\$
    – Pikalek
    Commented Jul 14, 2019 at 12:53

2 Answers 2

2
\$\begingroup\$

If we refer to physics 101 (Motion in a Straight Line: Crash Course Physics #1), we will find that linear motion works as follows:

position = initial_position + speed * elapsed_time

Yes, I am assuming no acceleration.

If we work with delta time from the last frame, then initial_position is just the previous position, so we have:

position = position + speed * delta

speed must be in units of distance over time, of course. And delta must be in units of time. When you multiply distance/time by time, you get the distance that the object moved.

moved_distance = speed * delta
position = position + moved_distance

You may need to do a time unit conversion depending of what units you used for speed and delta. For example, if your speed is in seconds, and your delta is in milliseconds, you would do this:

delta_in_seconds = delta_in_milliseconds / 1000.0 
moved_distance = speed * delta_in_seconds
position = position + moved_distance

Note: If you define speed and position as a vectors, this continues to work. Which remind me, you should call your "speed vector" velocity, and your "moved distance vector" displacement.

So, if you have defined the speed at which an object should move, you can use this approach to figure out how much it moves each frame.

As per getting a delta, you did not specify what language or platform you are using. If you are not using an engine that gives you a delta, refer to How to measure time interval (different languages).

\$\endgroup\$
0
\$\begingroup\$

You can do the following:

  • enemySpeed = speed you set for the enemy.

  • finalSpeed = speed with the delta_time

  • delta_time = time between one frame and another

  • 33333.33333 = interval of time between one frame and another based on game with 30fps

This last value you can set based on the amount of fps you are making your game. It is only divide 1000000 by amount of fps.

Example with a game made based on 30 fps:

1000000 / 30 = 33333.33333...

Formule to set the speed in your game:

finalSpeed = enemySpeed * (delta_time / 33333.33333)

If by chance the pc of the person playing your game is bad and run at 20 fps. The following will happen:

finalSpeed = enemySpeed * (50000 / 33333.33333)

finalSpeed = enemySpeed * (1.5)

  • Assuming the speed you set for the enemy is 10.

finalSpeed = 10 * 1.5

finalSpeed = 15

Notice that there is a compensation, so even on a bad computer, the enemy will walk the same amount.

The opposite will happen if the person has a very good computer that causes the game to run with more fps.

I used my GameMaker knowledge as a reference.


Complementing:

15 * 20 (fps) = 300 units traveled

10 * 30 (fps) = 300 units traveled

\$\endgroup\$

You must log in to answer this question.

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