I need to change my collision and movement physics engine entirely

This commit is contained in:
Andrew Haynes Macbook
2026-01-26 21:38:09 -05:00
parent ef6a5bf507
commit 1ff8d450c4
11 changed files with 177 additions and 216 deletions
+2 -38
View File
@@ -1,5 +1,4 @@
--- ---
# Base Style
BasedOnStyle: Google BasedOnStyle: Google
Standard: Latest Standard: Latest
ColumnLimit: 100 ColumnLimit: 100
@@ -7,50 +6,15 @@ UseTab: Always
TabWidth: 8 TabWidth: 8
IndentWidth: 8 IndentWidth: 8
BreakBeforeBraces: Allman BreakBeforeBraces: Allman
# BreakBeforeBraces: Stroustrup
SpaceBeforeCpp11BracedList: true SpaceBeforeCpp11BracedList: true
SeparateDefinitionBlocks: Always SeparateDefinitionBlocks: Always
Cpp11BracedListStyle: false Cpp11BracedListStyle: false
BreakAfterReturnType: TopLevel BreakAfterReturnType: TopLevel
AlignTrailingComments: AlignTrailingComments:
Kind: Always Kind: Always
# OverEmptyLines: 2
ReflowComments: Always ReflowComments: Always
AllowShortFunctionsOnASingleLine: All AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: AllIfsAndElse AllowShortIfStatementsOnASingleLine: AllIfsAndElse
AllowShortLoopsOnASingleLine: true AllowShortLoopsOnASingleLine: true
# RemoveEmptyLinesInUnwrappedLines: true IndentAccessModifiers: false # Indent access specifiers by one level
AccessModifierOffset: -4 # Optional: adjust offset relative to the class indentation level
# ReflowComments: true
# SortIncludes: true
# IncludeBlocks: Regroup
#
# # Indentation
# ContinuationIndentWidth: 8
# AlignAfterOpenBracket: true
# PointerAlignment: Left
# ReferenceAlignment: Left
#
# # Braces
# BreakBeforeBraces: Custom
# BraceWrapping:
# AfterFunction: false
# AfterClass: false
# AfterControlStatement: false
# AfterNamespace: false
# IndentBraces: false
#
# # Containers & lists
#
# SpaceBeforeCpp11BracedList: true
# BreakAfterOpenBracketBracedList: true
#
# # Control statements & loops
# SpaceBeforeParens: Custom
# SpaceBeforeParensOptions:
# AfterControlStatements: true
# SpaceBeforeRangeBasedForLoopColon: true
#
# # Preprocessor
# IndentPPDirectives: AfterHash
# PPIndentWidth: 0
+1
View File
@@ -0,0 +1 @@
When the user provides code, they prefer suggestions for changes rather than direct modifications to the files, unless they explicitly ask for modifications.
+14 -14
View File
@@ -13,7 +13,6 @@
#include <SFML/Graphics/Color.hpp> #include <SFML/Graphics/Color.hpp>
#include <SFML/System/Vector2.hpp> #include <SFML/System/Vector2.hpp>
#include <array>
#include <string> #include <string>
#include <vector> #include <vector>
@@ -21,34 +20,35 @@
struct ObjectData struct ObjectData
{ {
sf::Vector2<float> size { TILESIZE, TILESIZE }; const sf::Vector2<float> size { TILESIZE, TILESIZE };
sf::Vector2<float> pos { TILESIZE, TILESIZE }; const sf::Vector2<float> pos { TILESIZE, TILESIZE };
sf::Color interior_color { sf::Color::Black }; const sf::Color interior_color { sf::Color::Black };
sf::Color exterior_color { sf::Color::White }; const sf::Color exterior_color { sf::Color::White };
float edge { -2.0f }; const float edge { -2.0f };
}; };
struct EntityData : ObjectData struct EntityData
{ {
sf::Vector2<float> velocity { 0, 0 }; ObjectData object;
const sf::Vector2<float> velocity { 0, 0 };
}; };
struct PlayerData : EntityData struct PlayerData
{ {
float max_dash_acceleration { 0.0f }; EntityData entity;
float max_vertical_acceleration { 0.0f }; const float max_dash_acceleration { 0.0f };
const float max_vertical_acceleration { 0.0f };
}; };
struct LevelData struct LevelData
{ {
// std::array<std::array<char, MAX_COLS>, MAX_ROWS> data {};
const char data[MAX_ROWS][MAX_COLS + 1]; const char data[MAX_ROWS][MAX_COLS + 1];
}; };
struct TimerData struct TimerData
{ {
std::string name { "NULL" }; const std::string name { "NULL" };
float max_time { 1.f }; const float max_time { 1.f };
}; };
extern const std::vector<ObjectData> object_data; extern const std::vector<ObjectData> object_data;
+33 -26
View File
@@ -13,25 +13,38 @@
#include "globals.hpp" #include "globals.hpp"
#include "scheduler.hpp" #include "scheduler.hpp"
float
total_distance(int k);
float const
distance_to_acceleration(int target);
struct Object;
class Entity; class Entity;
class Object;
class Player; class Player;
class Object struct Object
{ {
public: Object(ObjectData data)
Object(ObjectData data); {
rect.setSize(data.size);
rect.setPosition(data.pos);
rect.setOutlineThickness(data.edge);
rect.setFillColor(data.interior_color);
rect.setOutlineColor(data.exterior_color);
};
sf::RectangleShape rect; sf::RectangleShape rect;
}; };
class Entity : public Object class Entity : public Object
{ {
public: public:
Entity(EntityData data) : Object(data), _velocity(data.velocity) {}; Entity(EntityData data) : Object(data.object), _velocity(data.velocity) {};
void set_velocity(sf::Vector2f velocity); void set_velocity(sf::Vector2f velocity);
void set_velocity_x(int velocity); void set_velocity_x(int velocity);
void set_velocity_y(int velocity); void set_velocity_y(int velocity);
sf::Vector2<float> get_velocity(); const sf::Vector2<float> get_velocity();
protected: protected:
sf::Vector2f _velocity; sf::Vector2f _velocity;
@@ -40,36 +53,30 @@ class Entity : public Object
class Player : public Entity class Player : public Entity
{ {
public: public:
Player(PlayerData data, float gravity) Player(PlayerData data)
: Entity(data), : Entity(data.entity),
max_dash_acceleration(data.max_vertical_acceleration), max_dash_acceleration(data.max_dash_acceleration),
max_vertical_acceleration(data.max_vertical_acceleration), max_vertical_acceleration(data.max_vertical_acceleration)
gravity(gravity) {}; {
bounding_box.rect.setSize(
{ total_distance(max_dash_acceleration),
data.entity.object.size.y });
};
Object bounding_box { object_data[1] };
float max_vertical_acceleration; float max_vertical_acceleration;
float max_dash_acceleration; float max_dash_acceleration;
float gravity;
bool m_jumping { false }; bool m_jumping { false };
void handle_movment(Timer& dash, void handle_bounding_box(std::array<std::array<std::unique_ptr<Object>, MAX_COLS>, MAX_ROWS>& tilemap);
std::array<std::array<std::unique_ptr<Object>, 19>, 13>& tilemap); void handle_movment(std::optional<Timer*> dash);
void handle_gravity(); void handle_gravity();
bool get_jumping();
void horizontal_no_out_of_bounds();
void vertical_no_out_of_bounds();
bool get_horizontal_collision( bool get_horizontal_collision(
std::array<std::array<std::unique_ptr<Object>, MAX_COLS>, MAX_ROWS>& tilemap); std::array<std::array<std::unique_ptr<Object>, MAX_COLS>, MAX_ROWS>& tilemap);
bool get_vertical_collision( bool get_vertical_collision(
std::array<std::array<std::unique_ptr<Object>, MAX_COLS>, MAX_ROWS>& tilemap); std::array<std::array<std::unique_ptr<Object>, MAX_COLS>, MAX_ROWS>& tilemap);
// bool get_vertical_bottom_collision(
// std::array<std::array<std::unique_ptr<Object>, MAX_COLS>, MAX_ROWS>& tilemap);
private: private:
std::string direction; std::string direction { "left" };
}; };
float
total_distance(int k);
float
distance_to_acceleration(int target);
#endif #endif
+2 -2
View File
@@ -14,11 +14,11 @@ class Manager
public: public:
void clear(); void clear();
void load(LevelData level); void load(LevelData level);
void draw(sf::RenderWindow& window); const void draw(sf::RenderWindow& window);
void set_next(); void set_next();
void set_previous(); void set_previous();
int get_active();
void set_active(int level); void set_active(int level);
const int get_active();
std::array<std::array<std::unique_ptr<Object>, MAX_COLS>, MAX_ROWS> tilemap = {}; std::array<std::array<std::unique_ptr<Object>, MAX_COLS>, MAX_ROWS> tilemap = {};
private: private:
+2 -1
View File
@@ -2,6 +2,7 @@
#define SCHEDULER_HPP #define SCHEDULER_HPP
#include <ctime> #include <ctime>
#include <optional>
#include <string> #include <string>
#include <atomic> #include <atomic>
#include <vector> #include <vector>
@@ -26,7 +27,7 @@ struct Scheduler {
Scheduler(); Scheduler();
std::vector<Timer> timers; std::vector<Timer> timers;
void update(std::atomic<bool>& running); void update(std::atomic<bool>& running);
int pull(std::string name); std::optional<Timer*> pull(std::string name);
}; };
+12 -22
View File
@@ -14,31 +14,21 @@ const std::vector<ObjectData> object_data {
}, },
}; };
/*
Example EntityData
[]() -> EntityData {
EntityData e;
e.size = { TILESIZE / 1.5f, TILESIZE / 1.5f };
e.pos = { 400.0f, 300.0f };
e.interior_color = sf::Color::Blue;
e.velocity = { 6.0f, 0.0f };
return e;
}(),
*/
const std::vector<EntityData> entity_data { const std::vector<EntityData> entity_data {
{}, {},
}; };
const PlayerData player_data = [] const PlayerData player_data {
{ .entity {
PlayerData data; .object {
data.size = { TILESIZE / 2.0f, TILESIZE / 2.0f }; .size = { TILESIZE / 2.f, TILESIZE / 2.f },
data.edge = -1.0f; .edge = -1.f,
data.velocity = { 3.0f, 0.0f }; },
data.max_dash_acceleration = 30.0f; // 15 .velocity = { 3.f, 0.f },
data.max_vertical_acceleration = 12.5f; // 12.5 default },
return data; .max_dash_acceleration = 30.f,
}(); .max_vertical_acceleration = 12.5f,
};
const std::vector<TimerData> timer_data { const std::vector<TimerData> timer_data {
{}, {},
@@ -110,7 +100,7 @@ const std::vector<LevelData> level_data = {
" ", " ",
" 0 ", " 0 ",
" ", " ",
" 0 0", " 0 ",
"1-----------------2", "1-----------------2",
}}, }},
}; };
+93 -70
View File
@@ -1,13 +1,13 @@
#include "elements.hpp" #include "elements.hpp"
#include <math.h>
#include <SFML/Graphics/Color.hpp> #include <SFML/Graphics/Color.hpp>
#include <SFML/Graphics/Rect.hpp> #include <SFML/Graphics/Rect.hpp>
#include <SFML/Graphics/RectangleShape.hpp> #include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/System/Angle.hpp> #include <SFML/System/Angle.hpp>
#include <SFML/System/Vector2.hpp> #include <SFML/System/Vector2.hpp>
#include <SFML/Window/Keyboard.hpp> #include <SFML/Window/Keyboard.hpp>
#include <cmath>
#include <iostream>
#include <memory> #include <memory>
#include <optional> #include <optional>
@@ -25,23 +25,18 @@ namespace rv = std::views;
float float
total_distance(int k) total_distance(int k)
{ {
if (k > 0) return k + total_distance(k - 1); if (k > 3) return k + total_distance(k - 1);
else return 0; else return 0;
} }
float const float
distance_to_acceleration(int target) distance_to_acceleration(int target)
{ {
return roundf(-1 + sqrtf(1 + 8 * target) / 2); if (target <= 0)
} {
return 0;
Object::Object(ObjectData data) }
{ return roundf((-1 + sqrtf(1 + 8 * target)) / 2);
rect.setSize(data.size);
rect.setPosition(data.pos);
rect.setOutlineThickness(data.edge);
rect.setFillColor(data.interior_color);
rect.setOutlineColor(data.exterior_color);
} }
void void
@@ -62,15 +57,79 @@ Entity::set_velocity_y(int velocity)
_velocity.y = velocity; _velocity.y = velocity;
} }
sf::Vector2<float> const sf::Vector2<float>
Entity::get_velocity() Entity::get_velocity()
{ {
return _velocity; return _velocity;
} }
void void
Player::handle_movment(Timer& dash, Player::handle_bounding_box(
std::array<std::array<std::unique_ptr<Object>, 19>, 13>& tilemap) std::array<std::array<std::unique_ptr<Object>, MAX_COLS>, MAX_ROWS>& tilemap)
{
float max_possible_dist = total_distance(player_data.max_dash_acceleration);
sf::FloatRect dash_check_box;
if (direction == "right")
{
dash_check_box = sf::FloatRect(
sf::Vector2f({ rect.getPosition().x + rect.getSize().x, rect.getPosition().y }),
sf::Vector2f({ max_possible_dist, rect.getSize().y }));
}
else
{
dash_check_box =
sf::FloatRect(sf::Vector2f({ rect.getPosition().x - max_possible_dist,
rect.getPosition().y }),
sf::Vector2f({ max_possible_dist, rect.getSize().y }));
}
float allowed_dash_dist = max_possible_dist;
for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS)))
{
if (tilemap[i][j] == nullptr) continue;
sf::FloatRect tile = tilemap[i][j]->rect.getGlobalBounds();
auto intersection = dash_check_box.findIntersection(tile);
float dist_to_tile;
if (intersection.has_value())
{
if (direction == "right")
{
dist_to_tile =
tile.position.x - (rect.getPosition().x + rect.getSize().x);
}
else
{
dist_to_tile =
rect.getPosition().x - (tile.position.x + tile.size.x);
}
if (dist_to_tile < allowed_dash_dist)
{
allowed_dash_dist = dist_to_tile;
}
}
if (allowed_dash_dist < 0)
{
allowed_dash_dist = 0;
}
bounding_box.rect.setSize({ allowed_dash_dist, rect.getSize().y });
if (direction == "right")
{
bounding_box.rect.setPosition(
{ rect.getPosition().x + rect.getSize().x, rect.getPosition().y });
}
else
{
bounding_box.rect.setPosition(
{ rect.getPosition().x - allowed_dash_dist, rect.getPosition().y });
}
max_dash_acceleration = distance_to_acceleration(allowed_dash_dist);
}
}
void
Player::handle_movment(std::optional<Timer*> dash)
{ {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D))
{ {
@@ -82,10 +141,11 @@ Player::handle_movment(Timer& dash,
direction = "left"; direction = "left";
rect.move({ -_velocity.x, 0 }); rect.move({ -_velocity.x, 0 });
} }
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W) && dash.get_time() <= 0) if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W) && (*dash)->get_time() <= 0 &&
dash.has_value())
{ {
_velocity.x = max_dash_acceleration; _velocity.x = max_dash_acceleration;
dash.reset(); (*dash)->reset();
} }
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space) && !m_jumping && _velocity.x <= 3) if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space) && !m_jumping && _velocity.x <= 3)
{ {
@@ -97,37 +157,20 @@ Player::handle_movment(Timer& dash,
void void
Player::handle_gravity() Player::handle_gravity()
{ {
if (get_velocity().x > player_data.velocity.x) if (get_velocity().x > player_data.entity.velocity.x)
{ {
set_velocity_x(get_velocity().x - gravity); set_velocity_x(get_velocity().x - GRAVITY);
}
else
{
set_velocity_x(get_velocity().x + GRAVITY);
} }
set_velocity_y(get_velocity().y + gravity); set_velocity_y(get_velocity().y + GRAVITY);
rect.move({ 0, get_velocity().y }); rect.move({ 0, get_velocity().y });
} if (get_velocity().y > ((float)TILESIZE / 2 - GRAVITY))
void
Player::horizontal_no_out_of_bounds()
{
float right_border = WIDTH - rect.getSize().x;
if (rect.getPosition().x > right_border)
{ {
rect.setPosition({ right_border, rect.getPosition().y }); set_velocity_y((TILESIZE / 2) - GRAVITY);
}
if (rect.getPosition().x < std::abs(rect.getOutlineThickness()))
{
rect.setPosition({ 0, rect.getPosition().y });
}
}
void
Player::vertical_no_out_of_bounds()
{
if (rect.getPosition().y >= HEIGHT - 48)
{
rect.setPosition({ rect.getPosition().x, HEIGHT - 48 });
set_velocity_y(0);
m_jumping = false;
} }
} }
@@ -174,7 +217,8 @@ Player::get_vertical_collision(
auto intersection = self.findIntersection(tile); auto intersection = self.findIntersection(tile);
if (!intersection) continue; if (!intersection) continue;
if (intersection->position.y <= 0) continue; if (intersection->position.y <= 0) continue;
if (intersection->size.y < intersection->size.x || intersection->size.y - intersection->size.x < rect.getSize().x) if (intersection->size.y < intersection->size.x ||
intersection->size.y - intersection->size.x < rect.getSize().x)
{ {
if (self.position.y < tile.position.y) if (self.position.y < tile.position.y)
{ {
@@ -186,7 +230,9 @@ Player::get_vertical_collision(
} }
else else
{ {
rect.setPosition({ rect.getPosition().x, tile.position.y + tilemap[i][j]->rect.getSize().y }); rect.setPosition(
{ rect.getPosition().x,
tile.position.y + tilemap[i][j]->rect.getSize().y });
set_velocity_y(0); set_velocity_y(0);
return true; return true;
} }
@@ -194,26 +240,3 @@ Player::get_vertical_collision(
} }
return false; return false;
} }
//
// bool
// Player::get_vertical_bottom_collision(
// std::array<std::array<std::unique_ptr<Object>, MAX_COLS>, MAX_ROWS>& tilemap)
// {
// for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS)))
// {
// if (tilemap[i][j] == nullptr) continue;
// auto collision = find_intersection(*tilemap[i][j]);
// if (collision == CollisionDir::None) continue;
// if (collision == CollisionDir::Bottom && get_velocity().y > 0)
// {
// rect.setPosition(
// { rect.getPosition().x,
// tilemap[i][j]->rect.getPosition().y - rect.getSize().y });
// m_jumping = false;
// set_velocity_y(0);
// return true;
// }
// }
// return false;
// }
+8 -33
View File
@@ -15,7 +15,7 @@
#ifdef __APPLE__ #ifdef __APPLE__
#include <range/v3/view/cartesian_product.hpp> #include <range/v3/view/cartesian_product.hpp>
#include <range/v3/view/iota.hpp> #include <range/v3/view/iota.hpp>
namespace rv = ranges::views; // namespace rv = ranges::views;
std::string assets = "/Users/andrew/personal/Projects/CPP/engine/assets/"; std::string assets = "/Users/andrew/personal/Projects/CPP/engine/assets/";
#else #else
#include <ranges> #include <ranges>
@@ -41,8 +41,7 @@ main()
sf::RenderWindow window(sf::VideoMode({ WIDTH, HEIGHT }), "engine", sf::Style::Default); sf::RenderWindow window(sf::VideoMode({ WIDTH, HEIGHT }), "engine", sf::Style::Default);
window.setFramerateLimit(FPS); window.setFramerateLimit(FPS);
Player player { player_data, GRAVITY }; Player player { player_data };
// player.initialize_bounding_box(player.get_velocity().x);
sf::Font font(assets + "pixel.ttf"); sf::Font font(assets + "pixel.ttf");
sf::Text text(font, "0, 0", 16); sf::Text text(font, "0, 0", 16);
@@ -77,46 +76,22 @@ main()
} }
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Enter) && if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Enter) &&
scheduler.timers.at(1).below_or_zero()) (*scheduler.pull("Dash"))->below_or_zero())
{ {
map_manager.set_next(); map_manager.set_next();
scheduler.timers.at(1).reset(); (*scheduler.pull("Dash"))->reset();
} }
player.handle_movment(scheduler.timers.at(scheduler.pull("Dash")), player.handle_movment(scheduler.pull("Dash"));
map_manager.tilemap); player.get_horizontal_collision(map_manager.tilemap);
std::cout << player.get_horizontal_collision(map_manager.tilemap) << "\n";
player.handle_gravity(); player.handle_gravity();
player.get_vertical_collision(map_manager.tilemap); player.get_vertical_collision(map_manager.tilemap);
player.vertical_no_out_of_bounds(); player.handle_bounding_box(map_manager.tilemap);
// if (player.get_velocity().y > ((float)TILESIZE / 2 - GRAVITY))
// {
// player.set_velocity_y((TILESIZE / 2) - GRAVITY);
// }
// if (!player.get_vertical_bottom_collision(map_manager.tilemap))
// {
// player.get_vertical_top_collision(map_manager.tilemap);
// }
// if (player.bounding_box != nullptr)
// {
// player.update_bounding_box();
// // player.check_bounding_box(map_manager.tilemap);
// }
// for (auto [i, j] :
// rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS)))
// {
// if (map_manager.tilemap[i][j] == nullptr) continue;
// if(player.find_intersection(*map_manager.tilemap[i][j]).x > 0) {
// std::cout << player.find_intersection(*map_manager.tilemap[i][j]).x << "\n";
// // running = false;
// // window.close();
// }
// }
map_manager.draw(window); map_manager.draw(window);
window.draw(player.rect); window.draw(player.rect);
window.draw(player.bounding_box.rect);
window.draw(text); window.draw(text);
window.display(); window.display();
} }
+2 -2
View File
@@ -25,7 +25,7 @@ Manager::clear()
} }
} }
int const int
Manager::get_active() Manager::get_active()
{ {
return _active; return _active;
@@ -53,7 +53,7 @@ Manager::set_previous()
load(level_data[_active]); load(level_data[_active]);
} }
void const void
Manager::draw(sf::RenderWindow& window) Manager::draw(sf::RenderWindow& window)
{ {
for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS))) for (auto [i, j] : rv::cartesian_product(rv::iota(0, MAX_ROWS), rv::iota(0, MAX_COLS)))
+4 -4
View File
@@ -48,13 +48,13 @@ Timer::update()
} }
} }
int std::optional<Timer*>
Scheduler::pull(std::string name) Scheduler::pull(std::string name)
{ {
try { try {
for (int i = 0; i < timers.size(); i++) { for (int i = 0; i < timers.size(); i++) {
if (timers[i].get_name() == name) { if (timers[i].get_name() == name) {
return i; return &timers[i];
} }
} }
throw -1; throw -1;
@@ -62,10 +62,10 @@ Scheduler::pull(std::string name)
catch (int x) { catch (int x) {
std::cout << "No valid timer with name: (" << name << ") as a value\n"; std::cout << "No valid timer with name: (" << name << ") as a value\n";
std::cout << "Using defualt timer values\n"; std::cout << "Using defualt timer values\n";
return -1; return std::nullopt;
} }
return 0; return std::nullopt;
} }
void void