Here is an attached video of my code in action.
https://imgur.com/a/qKK60EABelow is the attached code:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <math.h>
#include <vector>
int screenSizeX = 600;
int screenSizeY = 600;
bool inRange(float low, float hi, float test) {
return (test > low && test < hi);
}
class genericSquare {
public:
float xSize{};
float ySize{};
float defaultX{};
float defaultY{};
float xDelta{};
float yDelta{};
bool moveable;
sf::Vector2f position;
sf::RectangleShape square;
genericSquare(float xS, float yS, sf::Color color, float posX, float posY, float xD, float yD, bool moveabl) {
xSize = xS;
ySize = yS;
defaultX = posX;
defaultY = posY;
xDelta = xD;
yDelta = yD;
moveable = moveabl;
square.setSize(sf::Vector2f(xSize, ySize));
square.setFillColor(color);
square.setPosition(sf::Vector2f(posX, posY));
}
void playerInput();
};
std::vector<genericSquare> objectList;
int zz = 0;
bool checkCollisionAuxilary(float size, sf::Vector2f contains, char u) {
for (int i = 1; i < objectList.size(); i++) {
if (!objectList[i].moveable) {
sf::FloatRect bounds = objectList[i].square.getGlobalBounds();
for (zz = 0; zz < size+1; zz += size/size) {
if (bounds.contains(contains)) {
std::cout << u << std::endl;
return true;
}
}
}
}
return false;
}
bool checkCollision(genericSquare testee, char side, sf::Vector2f) {
float xSize = testee.xSize;
float ySize = testee.ySize;
sf::Vector2f position = testee.square.getPosition();
switch(side) {
case 'l':
return checkCollisionAuxilary(ySize, sf::Vector2f(position.x - 1, position.y + zz), 'l');
break;
case 'r':
return checkCollisionAuxilary(ySize, sf::Vector2f(position.x + xSize + 1, position.y + zz), 'r');
break;
case 'u':
return checkCollisionAuxilary(xSize, sf::Vector2f(position.x + zz, position.y - 1), 'u');
break;
case 'd':
return checkCollisionAuxilary(xSize, sf::Vector2f(position.x + zz, position.y + ySize + 1), 'd');
break;
default:
return false;
break;
}
}
void genericSquare::playerInput() {
position = square.getPosition();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && position.x > 0 && !checkCollision(*this, 'l', position)) {
square.move(sf::Vector2f(-xDelta, 0));
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && position.x < screenSizeX - xSize && !checkCollision(*this, 'r', position)) {
square.move(sf::Vector2f(xDelta, 0));
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && position.y > 0 && !checkCollision(*this, 'u', position)) {
square.move(sf::Vector2f(0, -yDelta));
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && position.y < screenSizeY - ySize && !checkCollision(*this, 'd', position)) {
square.move(sf::Vector2f(0, yDelta));
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::R)) {
square.setPosition(defaultX, defaultY);
}
}
int main() {
sf::RenderWindow window(sf::VideoMode(screenSizeX, screenSizeY), "test");
sf::Clock clock;
float accumulator = 0;
const float timestep = 1.0f / 60.0f; //60hz update frequency
float xDelta = 8.f;
float yDelta = 8.f;
genericSquare player(40.f, 40.f, sf::Color::Red, 40.f, 40.f, xDelta, yDelta, true);
objectList.push_back(player);
genericSquare obstacle(200.f, 200.f, sf::Color::Yellow, 200.f, 200.f, 0.f, 0.f, false);
objectList.push_back(obstacle);
while (window.isOpen()) {
sf::Event e;
while (window.pollEvent(e)) {
if (e.type == sf::Event::Closed)
window.close(); }
accumulator += clock.restart().asSeconds();
while (accumulator >= timestep) {
accumulator -= timestep;
//Put time-reliant code here
//Updates at nearly every 1/60th of a second
player.playerInput();
}
window.clear();
window.draw(obstacle.square);
window.draw(player.square);
window.display();
}
}
Basically, what it is doing when player.playerInput(); is called, is checking every pixel above the player when it is trying to go up, and if any object in
vector<genericSquare> objectList;
bounds contains the pixels above the player, it won't do up, and likewise for the 3 other directions. As you can see in the video, it only works when the player is fully contacting the yellow obstacle, which is weird hence it checks every pixel around the player, and if even one is in the globalBounds of the obstacle, it shouldn't move.
Would anyone mind helping me?
SOLVED:
Basically I tried to experiment with the checkCollisionAuxillary so I wouldn't have to copy and paste the same for loop in the checkCollision function, but that was the reason it was messing up. Probably something to do with the goofy zz variable setup.