So, recently I've started making a Pacman clone in C++ and SFML. Everything was going fine developing the game until I needed to create wall collision. I'm not that good with collision, so I was expecting problems.
What I'm trying to do is to check if a wall is above you, besides you or whatever, and then stopping the player if you collide with a wall.
The problem is that when you hit a wall, you stop way to early. And sometimes you get stuck in them. This is not to be confused with the code that makes Pacman not being able to turn when a wall is over you or something, but when running into a wall to stop.
Here is that code that I just mentioned about turning collision ( it also works, so no need for fixing that):
// All of the movement functions.
if (Keyboard::isKeyPressed(Keyboard::Up) && mapData[xtile + (ytile - 1) * 25] != 1 && mapData[xtile + (ytile - 1) * 25] != 2){
direction = Directions{ Up };
setVelocity(0.0, -2.5);
source.x = 73;
}
if (Keyboard::isKeyPressed(Keyboard::Down) && mapData[xtile + (ytile + 1) * 25] != 1 && mapData[xtile + (ytile + 1) * 25] != 2){
direction = Directions{ Down };
setVelocity(0.0, 2.5);
source.x = 110;
}
if (Keyboard::isKeyPressed(Keyboard::Left) && mapData[(xtile + 1) + ytile * 25] != 1 && mapData[(xtile - 1) + ytile * 25] != 2){
direction = Directions{ Left };
setVelocity(-2.5, 0.0);
source.x = 0;
}
if (Keyboard::isKeyPressed(Keyboard::Right) && mapData[(xtile - 1) + ytile * 25] != 1 && mapData[(xtile + 1) + ytile * 25] != 2){
direction = Directions{ Right };
setVelocity(2.5, 0.0);
source.x = 38;
}
The source.x thingy is for animation, so that has nothing to do with the problem at hand. setVelocity sets the velocity for the player and direction is just the direction of the player. The direction gets set by an enumuration.
Here's my code, if you want to see all of the other files and classes (alltough they're proboably irrelevant to the problem) or some other info, just ask me for the code or explanation and I will post it!
But enough babbling, here we go!
main.cpp
// Headers for the program.
#include <iostream>
#include <vector>
#include <SFML/Graphics.hpp>
// Game headers.
#include "renderer.h"
#include "pacman.h"
#include "pellet.h"
#include "map.h"
using std::cout;
using namespace sf;
// All of theese functions will be used for doing things in the future.
void render();
void logic();
enum Directions{ Up = 1, Down = 2, Left = 3, Right = 4 }; // pacman's directions
// Level data array. For the map.
int mapData[] =
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 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, // 14
1, 3, 1, 1, 1, 3, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 3, 1, 1, 1, 3, 1, // 15
1, 3, 3, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 3, 3, 3, 1, // 16
1, 1, 1, 3, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 3, 1, 3, 1, 1, 1, // 17
1, 3, 3, 3, 3, 3, 1, 3, 3, 3, 3, 3, 1, 3, 3, 3, 3, 3, 1, 3, 3, 3, 3, 3, 1, // 18
1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, // 19
1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, // 20
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // 21
};
// The maze for the game.
Map maze{ mapData };
// Make the renderer.
Renderer renderer(Vector2u(800, 800), "Pac-man");
// Make our yellow friend
Pacman pacman(358, 353);
int main(){
while (renderer.window.isOpen()){ // As long as the window is open, do this loop.
Event event; // A event var for only one thing, closing the window.
while (renderer.window.pollEvent(event)){ // Poll a event...
if (event.type == Event::Closed){ // Check if the event is a close event...
renderer.window.close(); // ... and if it is a close event then close the window.
}
}
logic(); // Doing our logic before the rendering.
render(); // Rendering.
}
}
void render(){
for (int x = 0; x < maze.map.size(); x++){
renderer.Render(maze.map[x]);
}
renderer.Render(pacman.sprite); // Render Pacman.
renderer.window.display(); // Display...
renderer.window.clear(); // ... And then clear the image.
}
void logic(){ // Here we are going to do all of our game logic.
pacman.update(mapData, maze.map);
pacman.sprite.move(pacman.xvel, pacman.yvel); // Move Pacman
// The problem most likely occurs with theese ifs:
if (pacman.direction == Up && !pacman.sprite.getGlobalBounds().intersects(maze.map[pacman.xtile + (pacman.ytile - 1) * 25].getGlobalBounds()) && mapData[
pacman.xtile + (pacman.ytile - 1) * 25] == 1){
pacman.yvel = 0;
}
if (pacman.direction == Down && !pacman.sprite.getGlobalBounds().intersects(maze.map[pacman.xtile + (pacman.ytile + 1) * 25].getGlobalBounds()) && mapData[
pacman.xtile + (pacman.ytile + 1) * 25] == 1){
pacman.yvel = 0;
}
if (pacman.direction == Left && !pacman.sprite.getGlobalBounds().intersects(maze.map[(pacman.xtile - 1) + pacman.ytile * 25].getGlobalBounds()) && mapData[
(pacman.xtile - 1) + pacman.ytile * 25] == 1){
pacman.xvel = 0;
}
if (pacman.direction == Right && !pacman.sprite.getGlobalBounds().intersects(maze.map[(pacman.xtile + 1) + pacman.ytile * 25].getGlobalBounds()) && mapData[
(pacman.xtile + 1) + pacman.ytile * 25] == 1){
pacman.xvel = 0;
}
// pellet collision (fully working)
if (mapData[pacman.xtile + pacman.ytile * 25] == 3){
mapData[pacman.xtile + pacman.ytile * 25] = 0;
maze.map[pacman.xtile + pacman.ytile * 25].setColor(sf::Color(0, 0, 0, 0));
pacman.addPoint();
}
}
There you go. If you have any suggestions on how to solve or go around this problem, feel free to post it to me. And remember, if you need more info, just ask me. Thanks in advance!