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
+37 -7
View File
@@ -1,4 +1,6 @@
#include "elements.hpp"
#include <SFML/Graphics/Rect.hpp>
#include <iostream>
#include <SFML/Graphics/Color.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.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.setFillColor(inside_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_jump_strength = initial_acceleration;
m_velocity.x = velocity_x;
}
void Player::handle_movment() {
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)) {
rect.move({-m_velocity.x, 0});
rect.move({-velocity.x, 0});
}
// if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) {
// rect.move({0, -2.5});
@@ -41,7 +71,7 @@ void Player::handle_movment() {
// }
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space) && !m_jumping) {
m_jumping = true;
m_velocity.y = -m_jump_strength; // push up
velocity.y = -m_jump_strength; // push up
}
}