2

I started programming a game in java. I have Enemys and a Player, a basic 2D game, thats to get experience for the first time.

Now i wanted the Enemys to follow a path, which i can draw in to a "level editor". I have a JPanel, a mouseMoveListener, and on click the Path2D starts saving the mouseMove Points to a Path2D.Double Object.

After that, i implemented the following method to make the enemys following this path:

public void forward(){
    if(!pathIterator.isDone()){
        pathIterator.currentSegment(current);
        x = current[0];
        y = current[1];
        pathIterator.next();
    }
    else {
        dead = true;
    }
}

I think its clear what happens now: The Enemy is following, but the speed is that i moved the mouse with. So if i move to Mouse to fast, the enemy just.. "jumps" from one point to an other. To slow, its "sneaking" over that points. (And because im not an Robot, i cannot move the Mouse with the same speed ^^)

Talking of Robot: Yes, i could let a awt.Robot move my Mouse. But this isnt really possible too, because i have to draw complicated paths, which dont have any visible mathematics behind.

So, i want let this Enemys to move on this path with the same speed. My Problem: I don't know where to implement a "fix". I have 2 Ideas:

  • Maybe i could work on the Path creation: Instead of just adding the Points to the Path2D, maybe i could calculate points between the points where i moved to fast, or deleting points which are to near by each other. But: First, I don't know how to calculate this, (Is there any maths logic to achieve this?) And Second, when i do this i probably would'nt be able to change the speed of the enemys ingame, and that would be bad

  • The second idea i have, is to calculate the points between (oder the points to jump over), this should happen every frame, relative to the actual speed. But here im not experienced enough in maths to.

So, are that ways possible, and if yes, has someone an idea how to calculate this? And, if not, what other possibilitys i have to achieve this?

Thank you, and sorry for the bad english!

T_01
  • 1,256
  • 3
  • 16
  • 35
  • You could always consider using *Splines*, that would mean that you generate a set of control points in your application that creates the paths and you can use those in your game. However it is quite a hard subject in my humble opinion, so I'm unsure if you should go with it now already. – skiwi Jan 21 '14 at 11:44
  • I was at the point to think the same ^^ Thank you for this idea, but Tim B already named the solution. Thank you anyway! – T_01 Jan 21 '14 at 12:05

1 Answers1

5

All you need to do is define the speed of movement of the enemy inside the enemy class.

When it works out the next point to move to then create a direction vector by subtracting the current position from the new position.

Normalize the direction vector (so it is length 1) then multiply it by the speed and the tpf (time per frame).

Move by that amount instead of jumping to the next point.

(Note if the points are very close together or the framerate is low this can cause it to overshoot a bit, it should be fine though).

Tim B
  • 40,716
  • 16
  • 83
  • 128
  • Arghhh, maths :D Thank you for the answer! So, a the direction vector is [x](xCurrent|xNew)[y](yCurrent|yNew), right? I will google how to normalize it ^^ – T_01 Jan 21 '14 at 11:56
  • `(xNew-xCurret), (yNew-yCurrent)` - length is `sqrt(x*x+y*y)`. normalize is `x/length, y/length`. :) – Tim B Jan 21 '14 at 11:57
  • Your library probably has a Vector2f (or similar) type for storing the vector in though and that will most likely have a normalize() method of some description. – Tim B Jan 21 '14 at 11:58
  • Im not using any librarys. Im just working with the default jre :) Wait - so the LENGTH of this vector is sqrt x² + y² ? And then divide the points / length ? – T_01 Jan 21 '14 at 12:03
  • Ou, i found this picture right now: http://www.mathe-lexikon.at/media/formulas/683071954d5ff16d66bf999ba779a6b0.png This is it ^^ Thank you very much, i hope i can make it run!! – T_01 Jan 21 '14 at 12:04
  • Ok, now i have a new problem.... maybe you can help me here? Im trying to check now, if the enemy reached the next point of the path... but that isnt working... the coords from the vector "new" and the actual ones arent matching... how to do this?? – T_01 Jan 21 '14 at 13:14
  • 1
    If you get close to the end of the path just "snap" to the final position and exit. Easiest way is to see if the distance to the end point is < speed*tpf. If it is then move to destination and end. – Tim B Jan 21 '14 at 14:00