Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (292)
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  
  Help for my enemy chase  (Read 1165 times)
0 Members and 1 Guest are viewing this topic.
Offline Graziano Mesina

Senior Newbie





« Posted 2006-09-27 00:52:50 »

Hi, I've an enemy that chase my player on maze tile game.
I've implemented the bresenham algorithm, it draw a rect from the enemy to the player, if this rect collides with a tile, enemy can't see the player and false is returned, otherwise it can see and true is returned.

The bresenham algorithm work fine, but I've a problem on my chase method:

px and py are player's coordinates
x and y are enemy's coordinates
validLocation return true if my enemy can walk in that direction:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
private void chase(float px, float py)
    {
        if(x < px && validLocation(x + velocità,y))
            EAST;
        else if(x > px && validLocation(x - velocità,y))
            WEST;
        else if(y < py && validLocation(x,y + velocità))
            SOUTH;
        else if(y > py && validLocation(x,y - velocità))
            NOTH;  
       
        super.incremen();
    }


obviously this method isn't correct, for example, when I've this situation:



my ghost enemy goes to EAST, but when x > px ot goes to WEST

how I've to change the controls??

please, any suggestion??!!!!!

-Montanelli-: Ma lei evadeva quasi sempre, no? <br /><br />-Mesina-: Sì, ho la fortuna di avere i polsi più grossi delle mani...
Offline Graziano Mesina

Senior Newbie





« Reply #1 - Posted 2006-09-27 09:59:49 »

I was thinking to give a priority when I check (x < or > player X) & (y < or > playerY)

like this:

1  
2  
3  
4  
if(Math.abs(x-px) > Math.abs(y-py)
than check (x < or > player X)
else
check (y < or > playerY)


but it's still imperfect
any ideas?

-Montanelli-: Ma lei evadeva quasi sempre, no? <br /><br />-Mesina-: Sì, ho la fortuna di avere i polsi più grossi delle mani...
Offline dishmoth
« Reply #2 - Posted 2006-09-27 11:49:56 »

I was thinking to give a priority when I check (x < or > player X) & (y < or > playerY)

like this:

1  
2  
3  
4  
if(Math.abs(x-px) > Math.abs(y-py)
than check (x < or > player X)
else
check (y < or > playerY)


That looks pretty close to the correct answer to me, but I think you need to add some logic so that if abs(x-px) > abs(y-py) then the code first checks (x < or > px), and if it turns out that the enemy cannot move in that direction (east or west) then it falls back to checking (y < or > py) so that the enemy has the chance of moving north or south instead.  (And similarly if abs(x-px) <= abs(y-py).  First check for movement north/south, and if that isn't possible then check for movement east/west.)

As an aside, if you look at maze games like Pacman (which I'm assuming is similar to what you're doing), then the enemies are usually programmed so that once they have chosen which direction to move in, they keep moving in that direction until they reach an intersection in the maze.  (Also, they never stay still, and often they prefer to turn left or right rather than reverse their direction.)  This helps to give the impression that the enemies are behaving intelligently.

Simon


Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (66 views)
2013-05-18 05:29:12

alaslipknot (75 views)
2013-05-17 05:24:48

gouessej (107 views)
2013-05-16 08:53:38

gouessej (103 views)
2013-05-16 08:17:58

theagentd (114 views)
2013-05-15 23:01:13

theagentd (103 views)
2013-05-15 23:00:54

StreetDoggy (148 views)
2013-05-14 23:56:26

kutucuk (171 views)
2013-05-13 01:10:36

kutucuk (169 views)
2013-05-12 23:36:09

UnluckyDevil (178 views)
2013-05-12 13:09:57
Complex number cookbook
by Roquen
2013-04-24 20:47:31

2D Dynamic Lighting
by Oskuro
2013-04-18 00:46:12

2D Dynamic Lighting
by Oskuro
2013-04-18 00:45:57

2D Dynamic Lighting
by Oskuro
2013-04-18 00:23:20

Noise (bandpassed white)
by Roquen
2013-04-06 01:36:01

Noise (bandpassed white)
by Roquen
2013-04-04 00:17:38

Java Data structures
by Roquen
2013-03-29 21:21:12

Topic Request
by kutucuk
2013-03-23 05:42:01
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!
Page created in 0.116 seconds with 20 queries.