Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Simple AI Programming  (Read 5388 times)
0 Members and 1 Guest are viewing this topic.
Offline ghostsoldier23

Junior Member


Medals: 1



« Posted 2012-01-22 16: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
« Reply #1 - Posted 2012-01-22 17: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;

Myomyomyo.
Offline ghostsoldier23

Junior Member


Medals: 1



« Reply #2 - Posted 2012-01-22 21: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! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Waterwolf

Junior Member


Medals: 3



« Reply #3 - Posted 2012-01-22 21: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

Junior Member


Medals: 1



« Reply #4 - Posted 2012-01-23 01: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
« Reply #5 - Posted 2012-01-24 03:45:55 »

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

Offline MunchGamer

Senior Newbie


Medals: 1



« Reply #6 - Posted 2012-02-05 08: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
« Reply #7 - Posted 2012-02-05 11: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

Junior Member


Medals: 1



« Reply #8 - Posted 2012-02-05 21: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

Junior Member


Medals: 1



« Reply #9 - Posted 2012-02-05 21: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! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline pitbuller
« Reply #10 - Posted 2012-02-05 23:19:53 »

How smart AI should be? allways found the shortest path or just wandering like zombies?
Offline Damocles
« Reply #11 - Posted 2012-02-06 13: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

Junior Member


Medals: 1



« Reply #12 - Posted 2012-02-06 22: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]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (55 views)
2014-04-15 18:08:23

BurntPizza (53 views)
2014-04-15 03:46:01

UprightPath (66 views)
2014-04-14 17:39:50

UprightPath (49 views)
2014-04-14 17:35:47

Porlus (66 views)
2014-04-14 15:48:38

tom_mai78101 (90 views)
2014-04-10 04:04:31

BurntPizza (151 views)
2014-04-08 23:06:04

tom_mai78101 (246 views)
2014-04-05 13:34:39

trollwarrior1 (204 views)
2014-04-04 12:06:45

CJLetsGame (211 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!