Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Enemies path following  (Read 1795 times)

0 Members and 1 Guest are viewing this topic.

Kousen

  • Newbie
  • *
  • Posts: 6
    • View Profile
Enemies path following
« on: July 04, 2020, 11:34:03 am »
Hello guys,

I am recently working with SFML libraries and I am trying to do a Space Shooter game from scratch. After some time working on it I get something that works fine but I am facing one issue and I do not know exactly how to proceed, so I hope your wisdom can lead me to a good solution. I will try to explain it the best I can:

Enemies following a path: currently in my game, I have enemies that can follow linear paths doing the following:

      float vx = m_wayPoints_v[m_wayPointsIndex_ui8].x - m_pos_v.x;
      float vy = m_wayPoints_v[m_wayPointsIndex_ui8].y - m_pos_v.y;

      float len = sqrt(vx * vx + vy * vy);
      //cout << len << endl;
      if (len < 2.0f)
      {
         // Close enough, entity has arrived
         //cout << "Has arrived" << endl;
         m_wayPointsIndex_ui8++;
         if (m_wayPointsIndex_ui8 >= m_wayPoints_v.size())
         {
            m_wayPointsIndex_ui8 = 0;
         }
      }
      else
      {
         vx /= len;
         vy /= len;

         m_pos_v.x += vx * float(m_moveSpeed_ui16) * time;
         m_pos_v.y += vy * float(m_moveSpeed_ui16) * time;
      }
*m_wayPoints_v is a vector<Vector2f> that basically holds the 2d points to be followed.

Related to this small piece of code, I have to say that is sometimes given me problems because getting closer to the next point becomes difficult as the higher the speed of the enemies is.

Is there any other way to be more accurate on path following independtly of the enemy speed? And also related to path following, if I would like to do an introduction of the enemies before each wave movement pattern starts (doing circles, spirals, ellipses or whatever before reaching the final point), for example:

For example, in the picture below:

The black line is the path I want a spaceship to follow before starting the IA pattern (move from left to right and from right to left) which is the red circle.

Is it done hardcoding all and each of the movements or is there any other better solution?

I hope I made myself clear on this...in case I did not, please let me know and I will give more details. Thank you very much in advance!
« Last Edit: July 05, 2020, 04:00:29 pm by Kousen »

Kousen

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Enemies path following
« Reply #1 on: July 05, 2020, 04:56:08 pm »
Well, I think I found one of the problems but I am not sure what the solution can be.

When using the piece of code I posted before, I found that there is a problem when reaching the destination point due to the speed value. Currently to move a space ship fluently, I need to set the speed to 200...which means that in these formulas:

         m_pos_v.x += vx * float(m_moveSpeed_ui16) * time;
         m_pos_v.y += vy * float(m_moveSpeed_ui16) * time;

The new position might exceed  the "2.0f" tolerance so the space ship cannot find the destination point and it gets stuck because the minimum movement that can be done per frame (assuming 60fps) 200 * 1 / 60 = 3.33px. Is there any way this behavior can be avoided?
« Last Edit: July 05, 2020, 06:02:37 pm by Kousen »

Kousen

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Enemies path following
« Reply #2 on: July 09, 2020, 11:21:23 am »
Anyone, please? :(

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Enemies path following
« Reply #3 on: July 09, 2020, 10:23:03 pm »
Honestly, I'm not sure what you're having a problem with.

Related to this small piece of code, I have to say that is sometimes given me problems because getting closer to the next point becomes difficult as the higher the speed of the enemies is.
Which problems? What is difficult?

Is there any other way to be more accurate on path following independtly of the enemy speed?
What do you mean by "accurate" here?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

fallahn

  • Sr. Member
  • ****
  • Posts: 492
  • Buns.
    • View Profile
    • Trederia
Re: Enemies path following
« Reply #4 on: July 10, 2020, 08:49:11 pm »
To tell if you've overshot your target position you can take the dot product of your velocity vector with the vector from your position to the target. If both vectors are pointing in the same direction (ie the target is still in front) the dot product will be greater than 0. If it is less than 0 then you have passed the target point, and can update to the next point on your path.

I've created a sample gist, with comments, here: https://gist.github.com/fallahn/8a1dfe8c7a480dbdb9c314c90f8ea919

Kousen

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Enemies path following
« Reply #5 on: July 10, 2020, 10:19:04 pm »
Hello guys,

Thank you for your replies. I have managed to solve it with a bit of help from another user, so the problem is gone.

I will check your code anyways, fallah. It will help me in the future for sure! Thank you once again :)

 

anything