# Making simple ai

im trying to make a simple ai where the enemy box-shaped geometry follows the player. what i have right now is not an ai, its just 4 if-statements to check if x is greater or less and according to that increase or decrease enemy’s x and the same thing for its z.

im having trouble with making a real ai with multiple enemies that follow me but have a pre-set movement path that they do (for instance a box shaped movement loop) while coming closer and close to the player.

I was also wondering what lookAt() does, can that be used as a part of the ai code? Thanks for any suggestions.

The topic of AI and pathfinding is quite broad. What I suggest is to start looking at solutions to this problem that the industry uses:

• A-star pathfinding

What lookAt method are you referring to, the one that is part of Camera?

If lookAt() doesn’t work (I am no expert regarding JME) I have a simple method to give you. It involves simple Mathematical procedures. I will tell you the solution in 2-D. That is, the solution can work if the models can move up/down and left/right (top-down). However, you can change it to whatever view you want, even in 3-D.

What you need to do is use Pythagoras’ theorem. You need to create a right-angled(90 degrees) triangle. How? You need to join the two shapes by a straight line and create a vertical and a horizontal line from each one. Where the vertical and horizontal lines form, you have made a right-angled triangle(two actually, but pick one). This is the plan you must do in your head.

To get it to work, you must get the length of the horizontal and vertical line of your triangle and use that along with Sin(), Cos() or Tan() as required to get the angle between the two shapes. Then, rotate your shape with this angle.

I really hope I helped you. This is a more manual way of doing it.

thanks for the help, i figured out my own way of making an ai