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
+14 -14
View File
@@ -13,7 +13,6 @@
#include <SFML/Graphics/Color.hpp>
#include <SFML/System/Vector2.hpp>
#include <array>
#include <string>
#include <vector>
@@ -21,34 +20,35 @@
struct ObjectData
{
sf::Vector2<float> size { TILESIZE, TILESIZE };
sf::Vector2<float> pos { TILESIZE, TILESIZE };
sf::Color interior_color { sf::Color::Black };
sf::Color exterior_color { sf::Color::White };
float edge { -2.0f };
const sf::Vector2<float> size { TILESIZE, TILESIZE };
const sf::Vector2<float> pos { TILESIZE, TILESIZE };
const sf::Color interior_color { sf::Color::Black };
const sf::Color exterior_color { sf::Color::White };
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 };
float max_vertical_acceleration { 0.0f };
EntityData entity;
const float max_dash_acceleration { 0.0f };
const float max_vertical_acceleration { 0.0f };
};
struct LevelData
{
// std::array<std::array<char, MAX_COLS>, MAX_ROWS> data {};
const char data[MAX_ROWS][MAX_COLS + 1];
};
struct TimerData
{
std::string name { "NULL" };
float max_time { 1.f };
const std::string name { "NULL" };
const float max_time { 1.f };
};
extern const std::vector<ObjectData> object_data;
+37 -30
View File
@@ -13,63 +13,70 @@
#include "globals.hpp"
#include "scheduler.hpp"
float
total_distance(int k);
float const
distance_to_acceleration(int target);
struct Object;
class Entity;
class Object;
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;
};
class Entity : public Object
{
public:
Entity(EntityData data) : Object(data), _velocity(data.velocity) {};
public:
Entity(EntityData data) : Object(data.object), _velocity(data.velocity) {};
void set_velocity(sf::Vector2f velocity);
void set_velocity_x(int velocity);
void set_velocity_y(int velocity);
sf::Vector2<float> get_velocity();
const sf::Vector2<float> get_velocity();
protected:
protected:
sf::Vector2f _velocity;
};
class Player : public Entity
{
public:
Player(PlayerData data, float gravity)
: Entity(data),
max_dash_acceleration(data.max_vertical_acceleration),
max_vertical_acceleration(data.max_vertical_acceleration),
gravity(gravity) {};
public:
Player(PlayerData data)
: Entity(data.entity),
max_dash_acceleration(data.max_dash_acceleration),
max_vertical_acceleration(data.max_vertical_acceleration)
{
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_dash_acceleration;
float gravity;
bool m_jumping { false };
void handle_movment(Timer& dash,
std::array<std::array<std::unique_ptr<Object>, 19>, 13>& tilemap);
void handle_bounding_box(std::array<std::array<std::unique_ptr<Object>, MAX_COLS>, MAX_ROWS>& tilemap);
void handle_movment(std::optional<Timer*> dash);
void handle_gravity();
bool get_jumping();
void horizontal_no_out_of_bounds();
void vertical_no_out_of_bounds();
bool get_horizontal_collision(
std::array<std::array<std::unique_ptr<Object>, MAX_COLS>, MAX_ROWS>& tilemap);
bool get_vertical_collision(
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:
std::string direction;
private:
std::string direction { "left" };
};
float
total_distance(int k);
float
distance_to_acceleration(int target);
#endif
+2 -2
View File
@@ -14,11 +14,11 @@ class Manager
public:
void clear();
void load(LevelData level);
void draw(sf::RenderWindow& window);
const void draw(sf::RenderWindow& window);
void set_next();
void set_previous();
int get_active();
void set_active(int level);
const int get_active();
std::array<std::array<std::unique_ptr<Object>, MAX_COLS>, MAX_ROWS> tilemap = {};
private:
+2 -1
View File
@@ -2,6 +2,7 @@
#define SCHEDULER_HPP
#include <ctime>
#include <optional>
#include <string>
#include <atomic>
#include <vector>
@@ -26,7 +27,7 @@ struct Scheduler {
Scheduler();
std::vector<Timer> timers;
void update(std::atomic<bool>& running);
int pull(std::string name);
std::optional<Timer*> pull(std::string name);
};