Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (753) Games in Android Showcase (228) games submitted by our members Games in WIP (842) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Simple AI Programming  (Read 14346 times) 0 Members and 1 Guest are viewing this topic.
ghostsoldier23

Junior Devvie

Medals: 1

 « Posted 2012-01-22 15: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.
theagentd
 « Reply #1 - Posted 2012-01-22 16: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.
ghostsoldier23

Junior Devvie

Medals: 1

 « Reply #2 - Posted 2012-01-22 20: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).
Waterwolf

Junior Devvie

Medals: 3

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

Junior Devvie

Medals: 1

 « Reply #4 - Posted 2012-01-23 00:29:20 »

Oh... I've just been increasing the position by a pace increment with each logic update.  Seems to be working fine....
ReBirth
 « Reply #5 - Posted 2012-01-24 02:45:55 »

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

MunchGamer

Senior Newbie

Medals: 1

 « Reply #6 - Posted 2012-02-05 07: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.
pitbuller
 « Reply #7 - Posted 2012-02-05 10: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.
ghostsoldier23

Junior Devvie

Medals: 1

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

Junior Devvie

Medals: 1

 « Reply #9 - Posted 2012-02-05 20: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.
pitbuller
 « Reply #10 - Posted 2012-02-05 22:19:53 »

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

Junior Devvie

Medals: 1

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

 ivj94 (582 views) 2018-03-24 14:47:39 ivj94 (47 views) 2018-03-24 14:46:31 ivj94 (374 views) 2018-03-24 14:43:53 Solater (61 views) 2018-03-17 05:04:08 nelsongames (108 views) 2018-03-05 17:56:34 Gornova (150 views) 2018-03-02 22:15:33 buddyBro (693 views) 2018-02-28 16:59:18 buddyBro (91 views) 2018-02-28 16:45:17 xxMrPHDxx (493 views) 2017-12-31 17:17:51 xxMrPHDxx (730 views) 2017-12-31 17:15:51
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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