0
\$\begingroup\$

I have this code to move the enemy: (for x, same for y)

  let a = force / mass

  let v0_x = x - x0
  let new_vx = v0_x * air_friction * dt / dt0 + a * dt * (dt + dt0) / 2

  let new_x0 = x
  let new_x = x + new_vx

  x0 = new_x0
  x = new_x

So basically force controls how much to accelerate and air_friction causes old velocity to drop to zero.

This is what I use to move enemy, I don't know if there is a better way, any drawbacks with this method, I haven't tested it very much.

Currently I give it a little bit of force for a little bit of time, and after which I set the force to zero, to make it stop. Like this:

setInterval(() => {  // run every 1 second
  let res = 0
  setInterval(() => { // run 3 times each update frame
    if (res++ < 3) {
      force = 0.5 
    } else {
      force = 0
    }
  }, 16);
}, 1000);

(Of course I don't use setInterval, it's just like pseudo code, I have library code that achieves something like this).

This causes enemy to move a little bit stop and move again. With this method I can go from A to B.

So my question is, I want to make an enemy behavior where enemy avoids player's facing direction and tries to sneak from back.

I imagined a rectangle in front of the player, when enemy is within this rectangle, it tries to get out of the rectangle from rectangle's farther side to the player. Otherwise it somehow approaches to the back, and gets closer to player from the back. One other behavior I thought is when enemy wants to go to B, it doesn't go in straight line but chooses a direction slightly with an additional angle added to target direction. (Of course this decision is repeated on every interval along the way) Like this

    / or this path
   /
  /
 /
A ------ Target direction  . B Target
 \
  \
   \
    \ Enemy uses this path .C decision is repeated at C

Now I couldn't test this approach, because I had some trouble setting up the force and resetting it to zero, something to do with library getting in the way.

I thought maybe there is a better way to approach this problem, what do you think. My actual question is How do I move an object that avoids a facing direction and tries to sneak from the back, while maintaining a smooth juicy natural movement.

\$\endgroup\$
1

0

You must log in to answer this question.

Browse other questions tagged .