added collisions

This commit is contained in:
DarthMooMancer
2025-10-17 16:42:36 -04:00
parent 371fecc44b
commit 7f33d0d348
4 changed files with 137 additions and 85 deletions
+10 -12
View File
@@ -1,36 +1,34 @@
#ifndef PLAYER_HPP #ifndef PLAYER_HPP
#define PLAYER_HPP #define PLAYER_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>
enum class CollisionDir { None, Left, Right, Top, Bottom };
class Entity { class Entity {
public: public:
Entity(sf::Vector2<float> size, sf::Vector2<float> starting_position, float thickness, sf::Color inside_color, sf::Color border_color); Entity(sf::Vector2<float> size, sf::Vector2<float> starting_position, sf::Vector2f initial_velocity, float thickness, sf::Color inside_color, sf::Color border_color = sf::Color::White);
bool find_intersection(Entity &entity); CollisionDir find_intersection(Entity &entity);
sf::Vector2f velocity;
sf::RectangleShape rect; sf::RectangleShape rect;
protected: protected:
sf::Color inside_color; sf::Color _inside_color;
sf::Color border_color; sf::Color _border_color;
float top_left; float _border_thickness;
float bottom_left;
float top_right;
float bottom_right;
float border_thickness;
}; };
class Player : public Entity { class Player : public Entity {
public: public:
Player(sf::Vector2<float> size, sf::Vector2<float> starting_position, float acceleration, float initial_acceleration, float thickness, float velocity_x, sf::Color inside_color, sf::Color border_color); Player(sf::Vector2<float> size, sf::Vector2<float> starting_position, float acceleration, float initial_acceleration, float thickness, sf::Vector2f initial_velocity, sf::Color inside_color, sf::Color border_color = sf::Color::White);
sf::Vector2<float> m_velocity;
float m_acceleration; float m_acceleration;
float m_jump_strength; float m_jump_strength;
bool m_jumping; bool m_jumping;
void handle_movment(); void handle_movment();
sf::Vector2<float> get_velocity(); sf::Vector2<float> get_velocity();
bool get_jumping(); bool get_jumping();
}; };
#endif #endif
+2 -2
View File
@@ -1,5 +1,5 @@
#ifndef GLOBALS_HPP #ifndef GLOBALS_HPP
#define GLOBALS_HPP #define GLOBALS_HPP
#define WIDTH 600 #define WIDTH 608
#define HEIGHT 400 #define HEIGHT 416
#endif #endif
+37 -7
View File
@@ -1,4 +1,6 @@
#include "elements.hpp" #include "elements.hpp"
#include <SFML/Graphics/Rect.hpp>
#include <iostream>
#include <SFML/Graphics/Color.hpp> #include <SFML/Graphics/Color.hpp>
#include <SFML/System/Vector2.hpp> #include <SFML/System/Vector2.hpp>
@@ -13,26 +15,54 @@
* */ * */
Entity::Entity(sf::Vector2<float> size, sf::Vector2<float> starting_position, float thickness, sf::Color inside_color, sf::Color border_color) { // CollisionDir Entity::getCollisionDirection(const sf::FloatRect& a, const sf::FloatRect& b) {
Entity::Entity(sf::Vector2<float> size, sf::Vector2<float> starting_position, sf::Vector2f initial_velocity, float thickness, sf::Color inside_color, sf::Color border_color) {
rect.setSize(size); rect.setSize(size);
rect.setPosition(starting_position); rect.setPosition(starting_position);
rect.setOutlineThickness(thickness); // Needs to be the same as floor height to be properly on the line; else it will just show up above the line or in the line rect.setOutlineThickness(thickness); // Needs to be the same as floor height to be properly on the line; else it will just show up above the line or in the line
rect.setFillColor(inside_color); rect.setFillColor(inside_color);
rect.setOutlineColor(border_color); rect.setOutlineColor(border_color);
velocity = initial_velocity;
} }
Player::Player(sf::Vector2<float> size, sf::Vector2<float> starting_position, float acceleration, float initial_acceleration, float thickness, float velocity_x, sf::Color inside_color, sf::Color border_color) : Entity(size, starting_position, thickness, inside_color, border_color) { CollisionDir Entity::find_intersection(Entity &entity) {
sf::FloatRect a = rect.getGlobalBounds();
sf::FloatRect b = entity.rect.getGlobalBounds();
auto inter = a.findIntersection(b);
if (!inter) {
return CollisionDir::None;
}
const auto overlap = *inter;
float overlapW = overlap.size.x;
float overlapH = overlap.size.y;
// Compare which axis overlap is smaller
if (overlapW < overlapH) {
// Horizontal collision
return (a.position.x < b.position.x)
? CollisionDir::Right // a hit b from the left side
: CollisionDir::Left; // a hit b from the right side
} else {
// Vertical collision
return (a.position.y < b.position.y)
? CollisionDir::Bottom // a hit b from above
: CollisionDir::Top; // a hit b from below
}
}
Player::Player(sf::Vector2<float> size, sf::Vector2<float> starting_position, float acceleration, float initial_acceleration, float thickness, sf::Vector2f initial_velocity, sf::Color inside_color, sf::Color border_color) : Entity(size, starting_position, initial_velocity, thickness, inside_color, border_color) {
m_acceleration = acceleration; m_acceleration = acceleration;
m_jump_strength = initial_acceleration; m_jump_strength = initial_acceleration;
m_velocity.x = velocity_x;
} }
void Player::handle_movment() { void Player::handle_movment() {
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) { if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) {
rect.move({m_velocity.x, 0}); rect.move({velocity.x, 0});
} if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) { } if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) {
rect.move({-m_velocity.x, 0}); rect.move({-velocity.x, 0});
} }
// if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) { // if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) {
// rect.move({0, -2.5}); // rect.move({0, -2.5});
@@ -41,7 +71,7 @@ void Player::handle_movment() {
// } // }
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space) && !m_jumping) { if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space) && !m_jumping) {
m_jumping = true; m_jumping = true;
m_velocity.y = -m_jump_strength; // push up velocity.y = -m_jump_strength; // push up
} }
} }
+88 -64
View File
@@ -8,39 +8,54 @@
#include <SFML/System/Vector2.hpp> #include <SFML/System/Vector2.hpp>
#include <SFML/Window/Keyboard.hpp> #include <SFML/Window/Keyboard.hpp>
#include <iostream> #include <iostream>
#include <vector>
#include "globals.hpp" #include "globals.hpp"
#include "elements.hpp" #include "elements.hpp"
int main() { int main() {
int map = [[]];
struct {
sf::Vector2<float> size { 16.0f, 16.0f };
sf::Vector2<float> pos { 40, 40 };
float acceleration { 1.0f };
float max_velocity_y { 20.0f };
float border_size { 1.0f };
sf::Vector2f velocity { 4.0f, 0 };
} player_data;
struct {
sf::Vector2f size { 32.0f, 32.0f };
sf::Vector2f pos { 0, 0 };
sf::Vector2f velocity { 0, 0 };
float border_size { 2.0f };
} tile_data;
struct {
} simulated_object_data;
std::vector<Entity> wall_list = {};
for(int i = 0; i < 19; i++) {
wall_list.push_back(Entity { tile_data.size, tile_data.pos, tile_data.velocity, tile_data.border_size, sf::Color::Black });
}
for(int i = 0; i < wall_list.size(); i++) {
wall_list[i].rect.setPosition({ (i * tile_data.size.x), tile_data.border_size });
}
float floor_height = 2.0f; float floor_height = 2.0f;
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);
struct {
sf::Vector2<float> size = { 16.0f, 16.0f };
sf::Vector2<float> pos = { 40, 40 };
float acceleration = 1.0f;
float max_velocity_y = 20.0f;
float border_size = 2.0f;
float velocity_x = 4.0f;
} player_data;
Player player { Player player {
player_data.size, player_data.pos, player_data.acceleration, player_data.size, player_data.pos, player_data.acceleration,
player_data.max_velocity_y, player_data.border_size, player_data.velocity_x, player_data.max_velocity_y, player_data.border_size, player_data.velocity,
sf::Color::Black, sf::Color::White sf::Color::Black
}; };
sf::RectangleShape line({ WIDTH, floor_height }); Entity line { { WIDTH, floor_height }, { 0, HEIGHT - 50 }, { 0, 0 }, 1.0f, sf::Color::White };
line.setPosition({ 0, HEIGHT - 50}); // sf::RectangleShape line({ WIDTH, floor_height });
// line.setPosition({ 0, HEIGHT - 50});
sf::RectangleShape rect({ 4, 4 }); Entity test_square { { 8, 8 }, { 100, 40 }, { 0, 0 }, 1.0f, sf::Color::Black };
rect.setFillColor(sf::Color::Black);
rect.setOutlineColor(sf::Color::White);
rect.setOutlineThickness(2.0f);
rect.setPosition({ 40, 40 });
// float vel_x = 3.0f;
float vel_y = -20.0f;
while (window.isOpen()) { while (window.isOpen()) {
window.clear(sf::Color::Black); window.clear(sf::Color::Black);
@@ -48,23 +63,35 @@ int main() {
if (event->is<sf::Event::Closed>()) if (event->is<sf::Event::Closed>())
window.close(); window.close();
} }
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space)) {
vel_y = -10;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) { if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) {
window.close(); window.close();
} }
// FOR TESTING
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space)) {
if(test_square.rect.getPosition().x < 30) {
test_square.velocity = { 20, 0 };
}
if(test_square.rect.getPosition().x > WIDTH - 30) {
test_square.velocity = { -20, 0 };
}
}
player.handle_movment(); player.handle_movment();
player.m_velocity.y += player.m_acceleration; // always apply gravity player.velocity.y += player.m_acceleration;
player.rect.move({0, player.m_velocity.y}); player.rect.move({0, player.velocity.y});
vel_y += 1.0f; test_square.velocity.y++;
rect.move({ 0, vel_y }); if(test_square.velocity.x > 0) {
test_square.velocity.x--;
}
if(test_square.velocity.x < 0) {
test_square.velocity.x++;
}
test_square.rect.move({ test_square.velocity.x, test_square.velocity.y });
float groundY = line.getPosition().y - floor_height - player.rect.getSize().x; float groundY = line.rect.getPosition().y - floor_height - player.rect.getSize().x;
if (player.rect.getPosition().y >= groundY) { if (player.rect.getPosition().y >= groundY) {
player.rect.setPosition({player.rect.getPosition().x, groundY}); player.rect.setPosition({player.rect.getPosition().x, groundY});
player.m_velocity.y = 0; player.velocity.y = 0;
player.m_jumping = false; player.m_jumping = false;
} }
float right_border = WIDTH - player.rect.getSize().x - player.rect.getOutlineThickness(); float right_border = WIDTH - player.rect.getSize().x - player.rect.getOutlineThickness();
@@ -74,47 +101,44 @@ int main() {
if(player.rect.getPosition().x < player.rect.getOutlineThickness()) { if(player.rect.getPosition().x < player.rect.getOutlineThickness()) {
player.rect.setPosition({ player.rect.getOutlineThickness(), player.rect.getPosition().y}); player.rect.setPosition({ player.rect.getOutlineThickness(), player.rect.getPosition().y});
} }
// if(rect.getGlobalBounds().findIntersection(player.rect.getGlobalBounds())) {
// vel_y = -20.0f;
// // vel_y = -20.0f;
// }
// if(rect.getGlobalBounds().findIntersection(player.rect.getGlobalBounds())) {
// if()
float right_border_rect = WIDTH - test_square.rect.getSize().x - test_square.rect.getOutlineThickness();
float groundY_rect = line.rect.getPosition().y - floor_height - (test_square.rect.getSize().y);
if(test_square.rect.getPosition().x > right_border_rect) {
test_square.rect.setPosition({ right_border_rect, test_square.rect.getPosition().y });
test_square.velocity = { -20, 0 };
}
if(test_square.rect.getPosition().x < test_square.rect.getOutlineThickness()) {
test_square.rect.setPosition({ test_square.rect.getOutlineThickness(), test_square.rect.getPosition().y });
test_square.velocity = { 20, 0 };
}
if(test_square.rect.getPosition().y >= groundY_rect) {
test_square.rect.setPosition({ test_square.rect.getPosition().x, groundY_rect });
}
auto collision = player.find_intersection(test_square);
float groundY_rect = line.getPosition().y - floor_height - (rect.getSize().y); switch (collision) {
if(rect.getPosition().x > right_border) { case CollisionDir::Top:
rect.setPosition({ right_border, rect.getPosition().y }); test_square.velocity = { 0, -20 };
break;
case CollisionDir::Bottom:
test_square.velocity = { 0, -20 };
break;
case CollisionDir::Left:
test_square.velocity = { -20, 0 };
break;
case CollisionDir::Right:
test_square.velocity = { 20, 0 };
break;
default: break;
} }
if(rect.getPosition().x < rect.getPosition().x + rect.getOutlineThickness()) {
rect.setPosition({ rect.getOutlineThickness(), rect.getPosition().y }); for(int i = 0; i < wall_list.size(); i++) {
window.draw(wall_list[i].rect);
} }
if(rect.getPosition().y >= groundY_rect) {
rect.setPosition({ rect.getPosition().x, groundY_rect });
vel_y = 0;
}
//
// auto playerBounds = player.rect.getGlobalBounds();
// auto rectBounds = rect.getGlobalBounds();
//
// // Create debug rectangles
// sf::RectangleShape playerBox(playerBounds.size);
// playerBox.setPosition(playerBounds.position);
// playerBox.setFillColor(sf::Color::Transparent);
// playerBox.setOutlineColor(sf::Color::Red);
// playerBox.setOutlineThickness(1.0f);
//
// sf::RectangleShape rectBox(rectBounds.size);
// rectBox.setPosition(rectBounds.position);
// rectBox.setFillColor(sf::Color::Transparent);
// rectBox.setOutlineColor(sf::Color::Red);
// rectBox.setOutlineThickness(1.0f);
//
// window.draw(playerBox);
// window.draw(rectBox);
window.draw(rect);
window.draw(player.rect); window.draw(player.rect);
window.draw(line); window.draw(test_square.rect);
window.draw(line.rect);
window.display(); window.display();
} }
} }