i finally got aabb collision to work properly
This commit is contained in:
@@ -6,7 +6,6 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
|||||||
project(SFML_game_engine)
|
project(SFML_game_engine)
|
||||||
|
|
||||||
include_directories(include)
|
include_directories(include)
|
||||||
|
|
||||||
set(SOURCES
|
set(SOURCES
|
||||||
src/main.cpp
|
src/main.cpp
|
||||||
src/elements.cpp
|
src/elements.cpp
|
||||||
@@ -23,7 +22,6 @@ FetchContent_Declare(SFML
|
|||||||
FetchContent_MakeAvailable(SFML)
|
FetchContent_MakeAvailable(SFML)
|
||||||
|
|
||||||
add_executable(SFML_game_engine ${SOURCES})
|
add_executable(SFML_game_engine ${SOURCES})
|
||||||
|
|
||||||
target_link_libraries(SFML_game_engine
|
target_link_libraries(SFML_game_engine
|
||||||
PRIVATE
|
PRIVATE
|
||||||
SFML::Graphics
|
SFML::Graphics
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include <SFML/Graphics/Color.hpp>
|
#include <SFML/Graphics/Color.hpp>
|
||||||
#include <SFML/Graphics/RectangleShape.hpp>
|
#include <SFML/Graphics/RectangleShape.hpp>
|
||||||
#include <SFML/System/Vector2.hpp>
|
#include <SFML/System/Vector2.hpp>
|
||||||
|
#include "globals.hpp"
|
||||||
|
|
||||||
enum class CollisionDir { None, Left, Right, Top, Bottom };
|
enum class CollisionDir { None, Left, Right, Top, Bottom };
|
||||||
class Entity {
|
class Entity {
|
||||||
@@ -31,6 +32,10 @@ public:
|
|||||||
bool m_jumping;
|
bool m_jumping;
|
||||||
void handle_movment();
|
void handle_movment();
|
||||||
bool get_jumping();
|
bool get_jumping();
|
||||||
|
void horizontal_no_out_of_bounds(Entity &base);
|
||||||
|
void vertical_no_out_of_bounds(Entity &base);
|
||||||
|
bool vertical_top_collision(Entity *wall_list[MAX_ROWS][MAX_COLS]);
|
||||||
|
bool vertical_bottom_collision(Entity *wall_list[MAX_ROWS][MAX_COLS]);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
#include "elements.hpp"
|
#include "elements.hpp"
|
||||||
|
#include "globals.hpp"
|
||||||
#include <SFML/Graphics/Rect.hpp>
|
#include <SFML/Graphics/Rect.hpp>
|
||||||
#include <SFML/Graphics/Color.hpp>
|
#include <SFML/Graphics/Color.hpp>
|
||||||
#include <SFML/System/Vector2.hpp>
|
#include <SFML/System/Vector2.hpp>
|
||||||
#include <SFML/Window/Keyboard.hpp>
|
#include <SFML/Window/Keyboard.hpp>
|
||||||
|
#include <ranges>
|
||||||
|
|
||||||
Entity::Entity(sf::Vector2<float> size, sf::Vector2<float> pos, sf::Vector2f velocity, float border_thickness, sf::Color inside_color, sf::Color border_color) {
|
Entity::Entity(sf::Vector2<float> size, sf::Vector2<float> pos, sf::Vector2f velocity, float border_thickness, sf::Color inside_color, sf::Color border_color) {
|
||||||
rect.setSize(size);
|
rect.setSize(size);
|
||||||
@@ -73,3 +75,55 @@ void Player::handle_movment() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Player::horizontal_no_out_of_bounds(Entity &base) {
|
||||||
|
float right_border = WIDTH - rect.getSize().x;
|
||||||
|
if(rect.getPosition().x > right_border) {
|
||||||
|
rect.setPosition({ right_border, rect.getPosition().y});
|
||||||
|
}
|
||||||
|
if(rect.getPosition().x < std::abs(rect.getOutlineThickness())) {
|
||||||
|
rect.setPosition({ 0, rect.getPosition().y});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Player::vertical_no_out_of_bounds(Entity &base) {
|
||||||
|
float groundY = base.rect.getPosition().y - base.rect.getSize().y - rect.getSize().y - base.rect.getOutlineThickness() - rect.getOutlineThickness();
|
||||||
|
if (rect.getPosition().y >= groundY) {
|
||||||
|
rect.setPosition({rect.getPosition().x, groundY});
|
||||||
|
set_velocity_y(0);
|
||||||
|
m_jumping = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Player::vertical_top_collision(Entity *wall_list[MAX_ROWS][MAX_COLS]) {
|
||||||
|
for(int i : std::views::iota(0, MAX_ROWS)) {
|
||||||
|
for(int j : std::views::iota(0, MAX_COLS)) {
|
||||||
|
if(wall_list[i][j] == nullptr) continue;
|
||||||
|
auto collision = find_intersection(*wall_list[i][j]);
|
||||||
|
if(collision == CollisionDir::None) continue;
|
||||||
|
if(collision == CollisionDir::Top) {
|
||||||
|
rect.setPosition({ rect.getPosition().x, wall_list[i][j]->rect.getSize().y + wall_list[i][j]->rect.getPosition().y + rect.getOutlineThickness()});
|
||||||
|
set_velocity_y(0);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Player::vertical_bottom_collision(Entity *wall_list[MAX_ROWS][MAX_COLS]) {
|
||||||
|
for(int i : std::views::iota(0, MAX_ROWS)) {
|
||||||
|
for(int j : std::views::iota(0, MAX_COLS)) {
|
||||||
|
if(wall_list[i][j] == nullptr) continue;
|
||||||
|
auto collision = find_intersection(*wall_list[i][j]);
|
||||||
|
if(collision == CollisionDir::None) continue;
|
||||||
|
if(collision == CollisionDir::Bottom && get_velocity().y > 0) {
|
||||||
|
rect.setPosition({ rect.getPosition().x, wall_list[i][j]->rect.getPosition().y - rect.getSize().y });
|
||||||
|
m_jumping = false;
|
||||||
|
set_velocity_y(0);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|||||||
+24
-69
@@ -12,18 +12,12 @@
|
|||||||
#include <SFML/Window/Keyboard.hpp>
|
#include <SFML/Window/Keyboard.hpp>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <ranges>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "globals.hpp"
|
#include "globals.hpp"
|
||||||
#include "elements.hpp"
|
#include "elements.hpp"
|
||||||
#include "maps.hpp"
|
#include "maps.hpp"
|
||||||
|
|
||||||
#ifdef __APPLE__
|
|
||||||
sf::Font font("/Users/andrewhaynes/personal/Projects/CPP/SFML_game_engine/assets/pixel.ttf");
|
|
||||||
#endif
|
|
||||||
#ifdef __linux__
|
|
||||||
sf::Font font("/home/andrew/personal/Projects/CPP/SFML_game_engine/assets/pixel.ttf");
|
|
||||||
#endif
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
Entity *wall_list[MAX_ROWS][MAX_COLS] = {};
|
Entity *wall_list[MAX_ROWS][MAX_COLS] = {};
|
||||||
struct {
|
struct {
|
||||||
@@ -56,21 +50,19 @@ int main() {
|
|||||||
float border_size { -1.0f };
|
float border_size { -1.0f };
|
||||||
} simulated_data;
|
} simulated_data;
|
||||||
|
|
||||||
for(int i = 0; i < MAX_ROWS; i++) {
|
for(int i : std::views::iota(0, MAX_ROWS)) {
|
||||||
for(int j = 0; j < MAX_COLS; j++) {
|
for(int j : std::views::iota(0, MAX_COLS)) {
|
||||||
if(map_data[i][j]) {
|
if(map_data[i][j] != 1) continue;
|
||||||
wall_list[i][j] = new Entity { tile_data.size, tile_data.pos, tile_data.velocity, tile_data.border_size, sf::Color::Black };
|
wall_list[i][j] = new Entity { tile_data.size, tile_data.pos, tile_data.velocity, tile_data.border_size, sf::Color::Black };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for(int i = 0; i < MAX_ROWS; i++) {
|
for(int i : std::views::iota(0, MAX_ROWS)) {
|
||||||
for(int j = 0; j < MAX_COLS; j++) {
|
for(int j : std::views::iota(0, MAX_COLS)) {
|
||||||
if(wall_list[i][j] != nullptr) {
|
if(wall_list[i][j] == nullptr) continue;
|
||||||
wall_list[i][j]->rect.setPosition({ (j * tile_data.size.x), (i * tile_data.size.y) });
|
wall_list[i][j]->rect.setPosition({ (j * tile_data.size.x), (i * tile_data.size.y) });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Player player {
|
Player player {
|
||||||
player_data.size, player_data.pos, player_data.velocity,
|
player_data.size, player_data.pos, player_data.velocity,
|
||||||
@@ -83,6 +75,13 @@ int main() {
|
|||||||
sf::RenderWindow window(sf::VideoMode({WIDTH, HEIGHT}), "SFML window", sf::Style::Default);
|
sf::RenderWindow window(sf::VideoMode({WIDTH, HEIGHT}), "SFML window", sf::Style::Default);
|
||||||
window.setFramerateLimit(60);
|
window.setFramerateLimit(60);
|
||||||
|
|
||||||
|
#ifdef __APPLE__
|
||||||
|
sf::Font font("/Users/andrewhaynes/personal/Projects/CPP/SFML_game_engine/assets/pixel.ttf");
|
||||||
|
#endif
|
||||||
|
#ifdef __linux__
|
||||||
|
sf::Font font("/home/andrew/personal/Projects/CPP/SFML_game_engine/assets/pixel.ttf");
|
||||||
|
#endif
|
||||||
|
|
||||||
sf::Text text(font, "0, 0", 16);
|
sf::Text text(font, "0, 0", 16);
|
||||||
text.setPosition({ WIDTH - (TILESIZE * 11.5), HEIGHT - (int)(TILESIZE / 1.5)});
|
text.setPosition({ WIDTH - (TILESIZE * 11.5), HEIGHT - (int)(TILESIZE / 1.5)});
|
||||||
|
|
||||||
@@ -99,8 +98,8 @@ int main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
player.handle_movment();
|
player.handle_movment();
|
||||||
for(int i = 0; i < MAX_ROWS; i++) {
|
for(int i : std::views::iota(0, MAX_ROWS)) {
|
||||||
for(int j = 0; j < MAX_COLS; j++) {
|
for(int j : std::views::iota(0, MAX_COLS)) {
|
||||||
if(wall_list[i][j] == nullptr) continue;
|
if(wall_list[i][j] == nullptr) continue;
|
||||||
auto collision = wall_list[i][j]->find_intersection(player);
|
auto collision = wall_list[i][j]->find_intersection(player);
|
||||||
if(collision == CollisionDir::None) continue;
|
if(collision == CollisionDir::None) continue;
|
||||||
@@ -117,59 +116,15 @@ int main() {
|
|||||||
}
|
}
|
||||||
player.set_velocity_y(player.get_velocity().y + player.m_acceleration);
|
player.set_velocity_y(player.get_velocity().y + player.m_acceleration);
|
||||||
player.rect.move({0, player.get_velocity().y});
|
player.rect.move({0, player.get_velocity().y});
|
||||||
for(int i = 0; i < MAX_ROWS; i++) {
|
|
||||||
for(int j = 0; j < MAX_COLS; j++) {
|
|
||||||
if(wall_list[i][j] == nullptr) continue;
|
|
||||||
auto collision = player.find_intersection(*wall_list[i][j]);
|
|
||||||
if(collision == CollisionDir::None) continue;
|
|
||||||
switch (collision) {
|
|
||||||
case CollisionDir::Bottom:
|
|
||||||
player.rect.setPosition({ player.rect.getPosition().x, wall_list[i][j]->rect.getPosition().y - player.rect.getSize().y });
|
|
||||||
player.set_velocity_y(0);
|
|
||||||
break;
|
|
||||||
default: break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// for(int i = 0; i < MAX_ROWS; i++) {
|
|
||||||
// for(int j = 0; j < MAX_COLS; j++) {
|
|
||||||
// if(wall_list[i][j] == nullptr) continue;
|
|
||||||
// auto collision = player.find_intersection(*wall_list[i][j]);
|
|
||||||
// // auto collision = wall_list[i][j]->find_intersection(player);
|
|
||||||
// if(collision == CollisionDir::None) continue;
|
|
||||||
// switch (collision) {
|
|
||||||
// case CollisionDir::Bottom:
|
|
||||||
// player.rect.setPosition({ player.rect.getPosition().x, wall_list[i][j]->rect.getPosition().y - player.rect.getSize().y });
|
|
||||||
// player.set_velocity_y(0);
|
|
||||||
// break;
|
|
||||||
// // case CollisionDir::Top:
|
|
||||||
// // player.rect.setPosition({ player.rect.getPosition().x, wall_list[i][j]->rect.getPosition().y + player.rect.getOutlineThickness()});
|
|
||||||
// // player.set_velocity_y(0);
|
|
||||||
// // std::cout << "Collision top\n";
|
|
||||||
// // player.rect.setPosition({ player.rect.getPosition().x, wall_list[i][j]->rect.getPosition().y + player.rect.getSize().y });
|
|
||||||
// // player.set_velocity_y(0);
|
|
||||||
// break;
|
|
||||||
// default: break;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
float groundY = line.rect.getPosition().y - line.rect.getSize().y - player.rect.getSize().y - line_data.border_size - player_data.border_size;
|
if(!player.vertical_bottom_collision(wall_list)) {
|
||||||
if (player.rect.getPosition().y >= groundY) {
|
player.vertical_top_collision(wall_list);
|
||||||
player.rect.setPosition({player.rect.getPosition().x, groundY});
|
|
||||||
player.set_velocity_y(0);
|
|
||||||
player.m_jumping = false;
|
|
||||||
}
|
|
||||||
float right_border = WIDTH - player.rect.getSize().x;
|
|
||||||
if(player.rect.getPosition().x > right_border) {
|
|
||||||
player.rect.setPosition({ right_border, player.rect.getPosition().y});
|
|
||||||
}
|
|
||||||
if(player.rect.getPosition().x < std::abs(player.rect.getOutlineThickness())) {
|
|
||||||
player.rect.setPosition({ 0, player.rect.getPosition().y});
|
|
||||||
}
|
}
|
||||||
|
player.vertical_no_out_of_bounds(line);
|
||||||
|
player.horizontal_no_out_of_bounds(line);
|
||||||
|
|
||||||
for(int i = 0; i < MAX_ROWS; i++) {
|
for(int i : std::views::iota(0, MAX_ROWS)) {
|
||||||
for(int j = 0; j < MAX_COLS; j++) {
|
for(int j : std::views::iota(0, MAX_COLS)) {
|
||||||
if(wall_list[i][j] != nullptr) {
|
if(wall_list[i][j] != nullptr) {
|
||||||
window.draw(wall_list[i][j]->rect);
|
window.draw(wall_list[i][j]->rect);
|
||||||
}
|
}
|
||||||
@@ -180,8 +135,8 @@ int main() {
|
|||||||
window.draw(text);
|
window.draw(text);
|
||||||
window.display();
|
window.display();
|
||||||
}
|
}
|
||||||
for(int i = 0; i < MAX_ROWS; i++) {
|
for(int i : std::views::iota(0, MAX_ROWS)) {
|
||||||
for(int j = 0; j < MAX_COLS; j++) {
|
for(int j : std::views::iota(0, MAX_COLS)) {
|
||||||
delete wall_list[i][j];
|
delete wall_list[i][j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user