Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Simple AI Programming  (Read 1982 times)
0 Members and 2 Guests are viewing this topic.
Offline ghostsoldier23

Jr. Member
**

Posts: 67
Medals: 1



« on: 2012-01-22 10:06:09 »

Could anyone give me a basis in pseudo-code of setting up simple AI?

The AI entities only need to be able to move towards the player on a vector that points to the player location, and avoid obstacles in real time.

I'm just looking for an example algorithm to get me started.
Offline theagentd

JGO Wizard
****

Posts: 1392
Medals: 88



« Reply #1 on: 2012-01-22 11:54:15 »

1  
2  
3  
4  
5  
6  
double dx = player.x - enemy.x, dy = player.y - enemy.y;
double distance = Math.sqrt(dx*dx + dy*dy);

double multiplier = moveSpeedOfEnemy / distance;

double velocityX = dx * multiplier, velocityY = dy * multiplier;

There is no god.
Offline ghostsoldier23

Jr. Member
**

Posts: 67
Medals: 1



« Reply #2 on: 2012-01-22 15:45:00 »

How does velocity help me?  (Thanks for the example though).

I'm more looking for real time path finding (assuming you have the coordinates and size of all obstacles in the area).
Games published by our own members! Go get 'em!
Offline Waterwolf

JGO n00b
*

Posts: 36
Medals: 2



« Reply #3 on: 2012-01-22 15:56:15 »

Position += Velocity

For path finding you should probably look up A*. There is this tutorial (for tile based maps though) on kevglass's site http://cokeandcode.com/index.html?page=tutorials/tilemap2
Offline ghostsoldier23

Jr. Member
**

Posts: 67
Medals: 1



« Reply #4 on: 2012-01-22 19:29:20 »

Oh... I've just been increasing the position by a pace increment with each logic update.  Seems to be working fine....
Offline ReBirth

JGO Wizard
****

Posts: 1279
Medals: 19



« Reply #5 on: 2012-01-23 21:45:55 »

You can also use Point2D.distance() in place of Math.sqrt().

Follow me, your mastah, on TWITTAH!
Offline MunchGamer

JGO n00b
*

Posts: 22



« Reply #6 on: 2012-02-05 02:31:48 »

in 2d space I've been using Math.atan2() to get the angle (from -pi to pi) between 2 points, then multiply the x value by Math.sin(direction) and the y value by Math.sin(direction).  Works like a charm, also for running away from something just use direction + pi.  Ahhhh polar coordinates.
Offline pitbuller

Sr. Member
**

Posts: 340
Medals: 9



« Reply #7 on: 2012-02-05 05:56:17 »

in 2d space I've been using Math.atan2() to get the angle (from -pi to pi) between 2 points, then multiply the x value by Math.sin(direction) and the y value by Math.sin(direction).  Works like a charm, also for running away from something just use direction + pi.  Ahhhh polar coordinates.

Angles are usually not needed. TheAgent method is fastest and simplest. But there is problem if distance = 0 so that need to be taken care of.

Offline ghostsoldier23

Jr. Member
**

Posts: 67
Medals: 1



« Reply #8 on: 2012-02-05 15:03:49 »

Bringing us back on topic here...

I didn't ask for how to calculate the distance between to two entities.  I'm looking for methods of implementing artificial intelligence and path-finding.
Offline ghostsoldier23

Jr. Member
**

Posts: 67
Medals: 1



« Reply #9 on: 2012-02-05 15:08:52 »

I've been considering implementing a concept similar to IR object detection:

1.  Scan 180 degree area (radius is distance from entity to player) for obstacles.
2.  Record where obstacles are and how far away they are, as well as where open spaces are that can be moved through.
3.  Path calculation can be based on open space > far object > near object > very near obstacle (can't move here)

The question is how to write that in code and how to actually lay out the path.  Should it be determined in segments?  One segment=pace perhaps?

Note: the game and its map data is pixel based, not tile based.
Games published by our own members! Go get 'em!
Offline pitbuller

Sr. Member
**

Posts: 340
Medals: 9



« Reply #10 on: 2012-02-05 17:19:53 »

How smart AI should be? allways found the shortest path or just wandering like zombies?

Offline Damocles

Sr. Member
**

Posts: 264
Medals: 14



« Reply #11 on: 2012-02-06 07:16:00 »

True, you need to first know what the AI should do.

If the purpose of the AI is to close to one (or many) Player-positions, then it needs 3 parts:
-recognize when to move (like seeing player)and where to (playerposition, or hidingspot)
-plan how to move (build a pathfinding-path, waypoints or just find a general direction)
-pursue the move according to plan and dynamic obstacles. (Step by step following the path, stop at and avoid dynamic obstacles)

Each part can be developed in a seperate step.
Where each generates the input of another part.

Once this simple "see-follow" works out, you might want to think about a "top level" AI, like a finite statemachine or "general" or script
that controls the AI in more complex situations.

Offline ghostsoldier23

Jr. Member
**

Posts: 67
Medals: 1



« Reply #12 on: 2012-02-06 16:07:37 »

Zombie-like AI, but the AI should always be able to find the shortest path to the player and navigate around obstacles.

That's pretty much all I need.  Shortest path from point A to B and ability to avoid obstacles.
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.094 seconds with 19 queries.