Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
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  
  Enemy movement  (Read 3073 times)
0 Members and 1 Guest are viewing this topic.
Offline Graziano Mesina

Senior Newbie





« Posted 2006-07-27 17:44:09 »

Hi, I've a tilemap game.. I've writed an enemy class with a move method like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
public void move()
    {
        switch((int)(4 * Math.random()))
        {
            case 0: setDx(getMaxSpeed());
                    setDy(0);
                    break;
            case 1: setDx(0);
                    setDy(getMaxSpeed());
                    break;
            case 2: setDx(-getMaxSpeed());
                    setDy(0);
                    break;
            case 3: setDx(0);
                    setDy(-getMaxSpeed());
                    break;
            default: setDx(getMaxSpeed());
                     setDy(0);
        }
    }


The enemy can run around randomly and at each intersection, they have to look all four ways.
I've to put this scannig on the collision detection method? and what kind of scanning?
I'm a little confused.. I hope you can help me!

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

Senior Member





« Reply #1 - Posted 2006-07-27 23:17:12 »

could you clarify everything? The 2 questions don't make sense to me, sorry.
Offline Graziano Mesina

Senior Newbie





« Reply #2 - Posted 2006-07-27 23:27:21 »

I'm sorry.. english is not my mother language  Grin

So, I've a maze with tilemap.. the enemy can move on left - right - up & down..
       |   |
___|   |________
___  o ________
       |    |

If we assume that    o    is the sprite, at each intersection it have to call
1  
public void move
?
where I've to implement the intersection control?

I hope you understand me  Huh

-Montanelli-: Ma lei evadeva quasi sempre, no? <br /><br />-Mesina-: Sì, ho la fortuna di avere i polsi più grossi delle mani...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Kova

Senior Member





« Reply #3 - Posted 2006-07-28 00:15:49 »

I didn't write a single tile game, but I would put the control in enemy's move(). First part of move() would call deceideDirection() where you would pass known parametars and deceide on next direction, and second part of move() would actually move to that direction.
I hope this is what you were asking... I try to think OO way, like, the enemy is the one who has to deceide where to move and then move, so that's why I would put those methods in enemy class.

Edit: oh I've figured it out what you're asking now, ... no, you don't have to put it into collision detection. Usually you first move something and then test if it colided or whatever (and change direction in collision method if it collided so next update it will move different way).
Offline Graziano Mesina

Senior Newbie





« Reply #4 - Posted 2006-07-28 10:48:09 »

I've put a
1  
enemyupdate
method in a manager class that update the movement of the enemy sprite..

            x
o    -->    x
            x

so, let assume that   o   is my sprite, it have a movement to the right and  x are my possible directions, I've to check everytime if a tile is blocked or not with a code like this
     
 
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
if (dx > 0)
   if(map.canMove(x,y+1) == false && map.canMove(x,y-1) == false && map.canMove(x+1,y) == false)
      move to left
   else
    if(map.canMove(x,y+1) == true && map.canMove(x,y-1) == false && map.canMove(x+1,y) == false)
      move or left or up
    else
    if(map.canMove(x,y+1) == true && map.canMove(x,y-1) == true && map.canMove(x+1,y) == false)
      move or left or up or down
.....
.....
....

??

I don't like this kind of control!   Lips Sealed



 

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

Senior Member





« Reply #5 - Posted 2006-07-28 11:19:11 »

as I said I didn't write a single tile game so I don't feel comfortable trying to help you anymore... there's a lot of people who wrote and know about tile games, it's weird nobody replyed yet (c'mon guys!)... try searching the forum, as I said, after initial space invaders tile games are most popular newbie work.

Edit: I couldn't help myself, I'll say a few things Smiley
of course you don't like the code above, you don't have to check every option. As I see it, you only need to check to what sides can you move and randomly choose one you can. This is my first time I think about movement in tile games and within 5 mins, so take this with some reserve.
Offline Ask_Hjorth_Larsen

Junior Member




Java games rock!


« Reply #6 - Posted 2006-07-28 12:26:08 »

They have to ``look all four ways''? I assume you want them to move in any of those directions.

Yes, put it in the move method. I don't understand very much of what you want, so that's probably why there are not a lot of replies.
Offline Graziano Mesina

Senior Newbie





« Reply #7 - Posted 2006-07-28 12:42:02 »

My game is like pacman but I use a TileMap..

So, "ghost" have to deceide the next directions at any intersection of the path
is this  the correct way to check the path?

1  
2  
3  
4  
5  
6  
7  
8  
9  
if (dx > 0)
   if(map.canMove(x,y+1) == false && map.canMove(x,y-1) == false && map.canMove(x+1,y) == false)
      move to left
   else
    if(map.canMove(x,y+1) == true && map.canMove(x,y-1) == false && map.canMove(x+1,y) == false)
      move or left or up
    else
    if(map.canMove(x,y+1) == true && map.canMove(x,y-1) == true && map.canMove(x+1,y) == false)
      move or left or up or down


I put this code on the enemyupdate method of the manager class.

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

Senior Member





« Reply #8 - Posted 2006-07-28 15:38:05 »

hmm, you're gonna need to add little ai to your ghosts, like that they don't turn in way they came from and that they mostly move towards player (or if they see him in the distance). But first I suggest you try just random movement at intersection, equal chance of going all ways.

For all random, I would do it something like this, pseudo:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
up = 0; down = 1; left = 2; right = 3;
can_move[up] = can he move up? true : false;
can_move[down] = can he move up? true : false;
can_move[left] = can he move up? true : false;
can_move[right] = can he move up? true : false;
deceided = false
while(!deceided) {
    direction = (int) math.random() * 4;
    if (can_move[direction])
        deceided = true;
}
move(direction);
Offline Graziano Mesina

Senior Newbie





« Reply #9 - Posted 2006-07-28 18:54:39 »

Ok, but if the enemy is moving, for example, to the right, on the next step it can move:

- only right
- only up
- only down
- only up or right
- only up or down
- only right or down
- only up or right or down

What is the best way to check and then deceide the direction?

-Montanelli-: Ma lei evadeva quasi sempre, no? <br /><br />-Mesina-: Sì, ho la fortuna di avere i polsi più grossi delle mani...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Kova

Senior Member





« Reply #10 - Posted 2006-07-28 20:37:50 »

what do you mean? The code I gave works in all cases but with same chance of going towards avaliable positions.
So he moves right and next time it's clear it isn't good he moves left... so you need to return false in your:

1  
can_move[left] = can he move left? true : false;


like:

1  
2  
3  
4  
5  
6  
public boolean canHeMoveLeft() {
    if (something is blocking the way) return false;
    // if last direction was right, he can't move left:
   if (direction == right) return false;
    return true;
}


you'll only need to add few lines of code in move() to test if it's a dead end steet becouse like this it would reach the end and get stuck or go into wall.
Offline Ask_Hjorth_Larsen

Junior Member




Java games rock!


« Reply #11 - Posted 2006-07-28 23:39:49 »

Quote
What is the best way to check and then deceide the direction?

You had a suggestion already in the first post. That's why it is difficult to understand your question. You are looking for a solution, but you already have a solution. In which way does your current solution not satisfy your needs?

Let's go through the update routine. The player movement is updated and then the ghosts. Presumably the updating of a ghost involves the move() method. In this method you could check whether the ghost should go up, down, left or right by invoking the code you quoted in the beginning (you just have to ask whether there is free space in the particular directions).

I see no problem.
Offline Anon666

Junior Member




aka Abuse/AbU5e/TehJumpingJawa


« Reply #12 - Posted 2006-07-31 14:16:52 »

hmm, you're gonna need to add little ai to your ghosts, like that they don't turn in way they came from and that they mostly move towards player (or if they see him in the distance). But first I suggest you try just random movement at intersection, equal chance of going all ways.

For all random, I would do it something like this, pseudo:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
up = 0; down = 1; left = 2; right = 3;
can_move[up] = can he move up? true : false;
can_move[down] = can he move up? true : false;
can_move[left] = can he move up? true : false;
can_move[right] = can he move up? true : false;
deceided = false
while(!deceided) {
    direction = (int) math.random() * 4;
    if (can_move[direction])
        deceided = true;
}
move(direction);


Bad bad code.

If he cannot move in any direction, your game locks up in an infinite loop.
Offline Kova

Senior Member





« Reply #13 - Posted 2006-07-31 20:07:08 »

I mentioned this is short, illustrative algorithm...  also (as mentioned) dosen't work for dead end ways. He'll need to adjust it of course.
Offline Graziano Mesina

Senior Newbie





« Reply #14 - Posted 2006-08-01 11:11:28 »

1  
2  
3  
4  
5  
....
......
.........
}
move(direction);


Bad bad code.

If he cannot move in any direction, your game locks up in an infinite loop.


So, why anybody don't post others code's examples?

I've a class TileMap that contains the data for a tile-based map, including Sprites..
with methods that gets width and height of the map,
getTile, setTile, addSprite, getSprite and
public boolean canMove(int x, int y) where I check if there is a blocked tile or not.

In the class ResourceManager I've the
void updateCreature(Creature creature, long elapsedTime)
where I calculate new value of x and y position

In the class Enemy I've the move method.

So, the control if a tile is blocked or not (with public boolean canMove(int x, int y) method)
it must have done in the enemy class or in the ResourceManager class?

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

Senior Newbie





« Reply #15 - Posted 2006-08-01 12:12:00 »

I would love to help you out as I have made a tilemapped pacman game myself but maybe my brain is a little slow at the moment. I would suggest that you read Kevs tutorial at http://www.cokeandcode.com/collisiontilemaps and see if that helps you out . That tutorial was the basis of all movement and collision detection in my game.


Offline Graziano Mesina

Senior Newbie





« Reply #16 - Posted 2006-08-02 12:45:20 »

I know how move the player
I'm interested on the movement of enemy  Sad
I want to move it on all possible directions.

nobody has done a maze game with tilemap??

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

Senior Member





« Reply #17 - Posted 2006-08-02 13:36:08 »

I remember some topic on pacman and enemy movement, it was about optimizing where in what direction should enemy look at intersection to try to see the player in distance... try to find that topic.

Can't you do it yourself? Movement isn't really rocket scienece... you just need to test in what direction enemy can move and randomly pick one of them. My algoritham is not good as we concluded but I'm sure you can write something similar that works... like put all directions he can move to in an array, shuffle the array and pick first element in it as direction...  before that you'll only need to test if he can move at all (other enemys blocking his sides) and set speed to 0 if he can't.
Offline SmittyTheSmit

Senior Newbie





« Reply #18 - Posted 2006-08-04 11:14:50 »

I have done what you ask but I will have to look at my code and see if there is anything useful there. will try to post something later
Offline Graziano Mesina

Senior Newbie





« Reply #19 - Posted 2006-08-04 18:09:37 »

ok, thanks

-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 #20 - Posted 2006-08-06 19:13:48 »

Hi, I've founded this code from the game Taleban vs Robot

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
class Fish extends SJGSprite
{
   
   
    public void move()
        {
            if(count == 0)
            {
                int i = d;
                do
                    switch((int)(4D * Math.random()))
                    {
                    case 0: // '\0'
                        dx = 1;
                        dy = 0;
                        d = 0;
                        break;

                    case 1: // '\001'
                        dx = 0;
                        dy = 1;
                        d = 1;
                        break;

                    case 2: // '\002'
                        dx = -1;
                        dy = 0;
                        d = 2;
                        break;

                    case 3: // '\003'
                        dx = 0;
                        dy = -1;
                        d = 3;
                        break;
                    }
                while(!maze.canMove(this, getX() + dx, getY() + dy) || Math.random() > 0.10000000000000001D && d != i);
            }


the maze.canMove method is:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
public boolean canMove(SJGSprite sjgsprite, double d1, double d2)
    {
        double d3 = sjgsprite.getWidth();
        double d4 = sjgsprite.getHeight();
        double d5 = d1 - d3 / 2D;
        double d6 = d2 - d4 / 2D;
        for(int i = Math.max((int)Math.floor(d5 / (double)blockWidth), 0); i <= Math.min((int)Math.floor(((d5 + d3) - 1.0D) / (double)blockWidth), width - 1); i++)
        {
            for(int j = Math.max((int)Math.floor(d6 / (double)blockHeight), 0); j <= Math.min((int)Math.floor(((d6 + d4) - 1.0D) / (double)blockHeight), height - 1); j++)
                if(maze[i][j].isSolid())
                    return false;

        }

        return true;
    }

 
what does mean this line code?Huh?

1  
for(int j = Math.max((int)Math.floor(d6 / (double)blockHeight), 0); j <= Math.min((int)Math.floor(((d6 + d4) - 1.0D) / (double)blockHeight), height - 1); j++)



What do you think about this code??
Anyone know something better?

-Montanelli-: Ma lei evadeva quasi sempre, no? <br /><br />-Mesina-: Sì, ho la fortuna di avere i polsi più grossi delle mani...
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 (25 views)
2014-04-15 18:08:23

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

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

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

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

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

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

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

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

CJLetsGame (192 views)
2014-04-01 02:16:10
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

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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!