I am making a pacman game and the last thing I need to figure out is the code for random ghost movement. Everything else is done except the ghost movement. I don’t care about each ghost having different algorithms. I just want them to move on their own within the maze. The ghost is a pathfinding object and my maze is the obstacle. I am trying to check if the ghost is stopped when it hits the obstacle. But the “if ghost stopped” condition does not work. I tried using “if ghost speed = 0” and that did not work. How can I check if the ghost is stopped so I can raycast the available directions the ghost would be allowed to travel?
Also, I like to put messages on the screen so I can see and test the values of my objects, but I can’t figure out how to display the ghost speed in a text object on the screen. I want to see the built in values of an object and not just my object variables. Any help would be appreciated.
This page has all the “built-in values” you can use:
I don’t think the pathfinding stops when touching an obstacle, it plots a route to the destination, avoiding the obstacles, and stops only when the destination was reached.
Any ideas on how to make the ghosts move randomly through the maze obstacles? I’ve tried pathfinding. No luck. Ghost cut off corners of the maze because of the interpolation values used in pathfinding. You can’t use the built-in values of the ghost object to check angles because the values returned are in decimals which makes it hard to get an accurate direction of travel for the ghost. I’ve tried using raycasting. But the ghosts aren’t allowed to switch directions backwards. They are always supposed to be moving forward. Raycasting worked great for the pacman and that is because I press a key to move him and therefore I know the direction of travel. I can’t find a condition or action that will tell me the direction of travel of a randomly moving object. Sure you have object.Force(), object.Angle(), object.Direction(), but they do not give you what you would think. I figured angle would give me 0, 90, 180, and 270. But it does not. I’ve put over 100 hours in this ghost movement and have 10 different algorithm that I have tried but just can’t make this work in GDevelop. I have made pacman in Java, C++, and GameMaker. So, I know the algorithms. They are not easy to implement in GDevelop. I am open to any help at this point. Out of ideas. Help!!
Considering that the movement options are very limited in pacman, I don’t think you should use pathfinding.
Angle() is the angle of rotation of the image. Unless you spin your ghosts, that’s not what you need.
The ghost algorithm isn’t very complex, from what I remember. Go forward until you face a wall, then it’s either left or right.
Then there’s the mode when they do a 180° to escape from pacman.
Either raycast + force or maybe topdown movement behavior.
The ghost can choose a different direction when they are not facing a wall. There are intersections and the ghost can choose any path from the intersection except for the direction he came from. So, I can’t raycast because the distance can be different at every intersection. Is there anyway to determine the direction of travel of the ghost? Angle() is not it, direction() is not it.
The way i remember it from a video, the ghosts had all ways to follow pacman… One would try to cut in front of him, another would try to follow him and so on…
What you could do is have a timer and every 1 second have them plot a path in front of pacman. That way they would move according to the pacman sprite and they would change behaviors.
If you just want them to move randomly in the maze, just place some invisible markers along the maze and choose one at random and do a pathfinding for that marker. When the ghost reaches the destination have it choose another…
Hope that helps…
Each ghost has a chase algorithm. 3 chase pacman. One goes directly towards him. Another goes 4 grid spaces in front of pacman. And another one uses the distance between Blinky and pacman to plot the course. It is easy to make a ghost chase pacman. I already did that using pathfinding. Then I got stuck trying to make another ghost chase a location 4 grid spaces in front of pacman. Remember pacman is in a grid maze. How do I determine the location in front of pacman if there are no available grid spaces in front of him? That would require way more code to keep track of that location. I would have to count grids, then check for corners and so forth. That’s why I went the random route. I like the random marker idea but pathfinding causes the ghost to overlap the maze blocks when going around corners. I haven’t found a way to fix that either.
I see your problem now…
But if the maze is a pathfinding obstacle, it should not overlap it… If anything it should allow to move at the desired path… Have you checked the grid size of the pathfinder object??
My grid and all ghosts are set at 32. Same with the virtual grid settings. Thanks for the example. I will let you know how it goes and if I can get my game to work.
Then you should try the virtual grid to be set to 16 for the pathfinding. I set mine to 20 when I had 40px sprites…
Also I came up with a solution (more like workaround) for the chasing ahead of pacman problem…
What I did was to set a timer for 1.5 seconds and at that time it would create a rectangle object at the pacman sprite and rotate it towards its movement. Like a viewcone, only rectangular. Then It would pick the farthest marker that it collided with and it would set this as the pathfinding point for the ghost…
This is the example of it…
I almost have my random code working without using targets. The only problem I am having now is that one collision condition will not work. I used the exact code with my player collision and it worked. So, I don’t see why it won’t work on another object. If I put the collision condition further down in the code it will activate. So I know the masks work. I attached a pic of the code. I am not using pathfinding on the ghosts. No behaviors. Just controlling them myself.
Glad to see you got it working without the behavior.
From what I see, you change the direction of the ghost, and you check for collision at the same time.
I’m guessing you expect the ghost to move first.
I have the ghosts moving randomly in the maze. But, they reverse directions. In Pacman, the ghosts do not reverse directions. I wish GDevelop had and else if statement. What I had to do was create a variable and set it so only horizontal movement or vertical movement is completed in one game cycle or frame. But GDevelop still operates too fast and both conditions are met in one frame. I tried adding a timer to stop the ghost until the next game cycle but that isn’t working either. Any ideas on how to keep them from reversing directions?
I tried what Gruk said but GDevelop still runs too fast. What is happening is that I check for open paths every time the ghost is on a pill. If the ghost is moving horizontally, I pick a random open direction and make the ghost go in that direction. Here is where the problem lies. If the new direction is vertical, the game picks a new open path before it finishes going into the previously chosen path. The game frames happen so fast, even with Gruk’s variable idea, that the next open path is being detected before the ghost even starts to move in the new direction. I hope that makes sense. So, what I did to verify this was to add 16 to the ghost position. This moves the ghost before the next open path can be chosen. It works and the ghost will now only go forward and not reverse directions. This is exactly what I wanted. But the problem with adding 16 to the position of the ghost is that it makes it look jerky when moving. 16 is the lowest I can add and have it work. Any lower and it goes back to reversing directions. I’ve attached a video to show you how jerky it is. Any ideas on how to fix this?
PacMan Ghost Movement
You’re adding 16 to its position, so you’re teleporting it. So the result looks quite normal considering your events.
Looks like it was not a good solution to your problem.
I finally figured out a way to truly make the ghost choose a random direction and have it run smoothly. Adding 16 to the position gave me an idea. I created a ghost grid checker and placed it under each pill. I then used the point inside a collision mask condition to check if the ghost’s center point was inside the ghost grid checker. If it is, then I check for a random direction and move accordingly. The ghost grid checker is only 6x6 in size. So, the ghost has to be on top of it to activate the code. Attached is a video of the ghost movement. I’ll clean the code up and hopefully get the last few details finished. Thanks to everyone for all the help.
Pac Man Smooth Ghost Movement Video