Skip to content

Commit

Permalink
Split amorphous "Object" into "Collider" and "Rigidbody"
Browse files Browse the repository at this point in the history
  • Loading branch information
cbosoft committed May 23, 2021
1 parent 20cbfb2 commit 037b218
Show file tree
Hide file tree
Showing 51 changed files with 468 additions and 463 deletions.
1 change: 1 addition & 0 deletions include/bounce/bounce.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "renderer/renderer.hpp"
#include "resources/manager.hpp"
#include "physics/field/force_fields.hpp"
#include "physics/rigidbody/rigidbody.hpp"
#include "ui/bar/bar.hpp"
#include "procedural/perlin/perlin.hpp"
#include "procedural/text/manger/manager.hpp"
Expand Down
6 changes: 3 additions & 3 deletions include/bounce/event/collision/collision.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

#include "../event.hpp"

class Object;
class Collider;
class CollisionEvent : public ImmediateEvent {
public:
CollisionEvent(Object *a, Object *b);
CollisionEvent(Collider *a, Collider *b);

void run(Game *game) override;

Object *_a, *_b;
Collider *_a, *_b;
};
1 change: 0 additions & 1 deletion include/bounce/game/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ class Game {

std::vector<Scene *> scene_stack;
std::list<Event *> events;
std::vector<Object *> all_objects;
std::map<std::string, Scene *> scenes_by_name;
std::list<Transform *> graveyard;

Expand Down
74 changes: 0 additions & 74 deletions include/bounce/object/object.hpp

This file was deleted.

49 changes: 49 additions & 0 deletions include/bounce/physics/collider/collider.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#pragma once

#include "../../transform/transform.hpp"
#include "../../serialisation/json.hpp"
#include "../../renderer/renderable/renderable.hpp"
#include "../../renderer/renderable/mesh/mesh.hpp"

enum ShapeType {ST_CIRCLE, ST_RECT};
enum CollisionDirection {CD_TOP, CD_LEFT, CD_BOTTOM, CD_RIGHT};

class Collider : public Transform {
public:
explicit Collider(Transform *parent);
explicit Collider(json j);

void on_update() override;
virtual void on_collision(Collider *other) {}
void get_renderables(std::list<const Renderable *> &out) const override;

void set_shape_rectangle(double w, double h);
void set_shape_circle(double radius);
void get_rect_half_extents(double &hw, double &hh) const;
void get_circle_radius(double &r) const;
[[nodiscard]] ShapeType get_shape_type() const;

void set_interaction_mask(unsigned int mask);
[[nodiscard]] bool can_interact() const;
[[nodiscard]] unsigned int get_interaction_mask() const;
[[nodiscard]] bool does_interact_with(const Collider *other) const;
[[nodiscard]] bool does_interact_with(unsigned int mask) const;

void set_touching(CollisionDirection dir);
void set_not_touching(CollisionDirection dir);
void set_not_touching_anything();
[[nodiscard]] bool is_touching(CollisionDirection dir) const;
[[nodiscard]] bool is_touching_top() const;
[[nodiscard]] bool is_touching_left() const;
[[nodiscard]] bool is_touching_bottom() const;
[[nodiscard]] bool is_touching_right() const;

json serialise() override;

protected:
double _hw, _hh;
ShapeType _shape_type;
unsigned int _touching_flags;
unsigned int _interaction_mask;
MeshRenderable *_renderable_collider;
};
26 changes: 10 additions & 16 deletions include/bounce/physics/engine/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
#include <chrono>
#include <vector>

#include "../../object/object.hpp"
#include "../collider/collider.hpp"
#include "../rigidbody/rigidbody.hpp"
#include "../field/field.hpp"
#include "../shape/shape.hpp"
#include "../material/material.hpp"

typedef std::chrono::high_resolution_clock Clock;
typedef std::chrono::duration<double, std::ratio<1, 1>> Duration;

struct CollisionInformation {
Object *b;
Rigidbody *b;
arma::vec2 normal;
arma::vec2 at;
double when;
Expand All @@ -26,32 +26,26 @@ class PhysicsEngine {

void timestep();
void timestep_objects();
void resolve_collision(Object *a, Object *b, const arma::vec2 &normal);
void resolve_collision_free_bodies(Object *a, Object *b, const arma::vec2 &normal);
void resolve_collision_one_fixed(Object *free_body, Object *fixed_body, const arma::vec2 &normal);
void resolve_collision(Rigidbody *a, Rigidbody *b, const arma::vec2 &normal);

static PhysicsMaterial get_overall_material_properties(const PhysicsMaterial &a, const PhysicsMaterial &b);
void check_set_time();
void set_timescale(double v);
[[nodiscard]] double get_time() const;
[[nodiscard]] double get_dt() const;

void register_object(Object *obj);
void unregister_object(Object *obj);
[[nodiscard]] std::list<Object *> get_active_objects() const;

[[nodiscard]] bool check_will_collide(const Object *a, const Object *b, CollisionInformation &ci) const;
[[nodiscard]] bool check_will_collide(const Rigidbody *a, const Rigidbody *b, CollisionInformation &ci) const;

private:
[[nodiscard]] bool check_will_collide_circle_circle(const Object *a, const Object *b, CollisionInformation &ci) const;
[[nodiscard]] bool check_will_collide_circle_rect(const Object *circle, const Object *rect, CollisionInformation &ci) const;
[[nodiscard]] bool check_will_collide_rect_rect(const Object *a, const Object *b, CollisionInformation &ci) const;
void resolve_collision_free_bodies(Rigidbody *a, Rigidbody *b, const arma::vec2 &normal);
void resolve_collision_one_fixed(Rigidbody *free_body, Rigidbody *fixed_body, const arma::vec2 &normal);

void traverse_get_objects(Transform *t, std::list<Object *> &out) const;
[[nodiscard]] bool check_will_collide_circle_circle(const Rigidbody *a, const Rigidbody *b, CollisionInformation &ci) const;
[[nodiscard]] bool check_will_collide_circle_rect(const Rigidbody *circle, const Rigidbody *rect, CollisionInformation &ci) const;
[[nodiscard]] bool check_will_collide_rect_rect(const Rigidbody *a, const Rigidbody *b, CollisionInformation &ci) const;

PhysicsEngine();

double dt, time, irl_time, timescale;
Clock::time_point epoch;
std::list<Object *> _all_objects;
};
2 changes: 1 addition & 1 deletion include/bounce/physics/field/air_resistance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class AirResField : public ForceField {
public:
[[nodiscard]] AirResField(double strength);

arma::vec2 f(Object *obj) const override;
arma::vec2 f(Rigidbody *obj) const override;

void set_strength(double strength);

Expand Down
2 changes: 1 addition & 1 deletion include/bounce/physics/field/bounded_field.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class BoundedForceField : public virtual ForceField {
BoundedForceField(const arma::vec2 &topleft,
const arma::vec2 &bottomright);

arma::vec2 measure_at(Object *obj) const override;
arma::vec2 measure_at(Rigidbody *obj) const override;

private:
arma::vec2 topleft, bottomright;
Expand Down
6 changes: 3 additions & 3 deletions include/bounce/physics/field/field.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#pragma once

#include "../rigidbody/rigidbody.hpp"
#include "../../transform/transform.hpp"
#include "../../object/object.hpp"

class ForceField: public Transform {
public:
virtual ~ForceField() = default;

virtual arma::vec2 measure_at(Object *obj) const;
virtual arma::vec2 f(Object *obj) const =0;
virtual arma::vec2 measure_at(Rigidbody *obj) const;
virtual arma::vec2 f(Rigidbody *obj) const =0;
};
2 changes: 1 addition & 1 deletion include/bounce/physics/field/gravity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class GravityField : public ForceField {
public:
[[nodiscard]] GravityField(double strength);

arma::vec2 f(Object *obj) const override;
arma::vec2 f(Rigidbody *obj) const override;

void set_strength(double strength);

Expand Down
2 changes: 1 addition & 1 deletion include/bounce/physics/field/point_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class PointSourceForceField : public ForceField {
public:
PointSourceForceField(double s);
arma::vec2 f(Object *obj) const;
arma::vec2 f(Rigidbody *obj) const;
private:
double s;
};
2 changes: 1 addition & 1 deletion include/bounce/physics/field/unbounded_linear.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ class UnboundedLinearForceField : public ForceField {
UnboundedLinearForceField(double mx, double cx, double my, double cy);

private:
arma::vec2 f(Object *obj) const override;
arma::vec2 f(Rigidbody *obj) const override;
double mx, cx, my, cy;
};
40 changes: 40 additions & 0 deletions include/bounce/physics/rigidbody/rigidbody.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include "../collider/collider.hpp"
#include "../material/material.hpp"

class Rigidbody : public Collider {
public:
explicit Rigidbody(Transform *parent);
explicit Rigidbody(json j);

void timestep_force(double dt);
void timestep_velocity(double dt);

void set_mass(double mass);
[[nodiscard]] double get_mass() const;

[[nodiscard]] bool is_fixed() const;
void set_fixed(bool value);

void set_velocity(const arma::vec2 &velocity);
[[nodiscard]] const arma::vec2 &get_velocity() const;

void set_force(const arma::vec2 &force);
void add_force(const arma::vec2 &force);
[[nodiscard]] const arma::vec2 &get_force() const;

[[nodiscard]] const PhysicsMaterial &get_material() const;
void set_bounciness(double bounciness);
void set_friction(double friction);

virtual void on_physics_update() {}

json serialise() override;

protected:
arma::vec2 _velocity, _previous_velocity, _force;
double _mass, _inv_mass;
PhysicsMaterial _material;
bool _fixed;
};
25 changes: 0 additions & 25 deletions include/bounce/physics/shape/shape.hpp

This file was deleted.

1 change: 0 additions & 1 deletion include/bounce/renderer/renderable/renderable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "../../transform/transform.hpp"
#include "../texture/texture.hpp"

class Object;
class Renderable : public Transform {
public:
Renderable();
Expand Down
1 change: 0 additions & 1 deletion include/bounce/renderer/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <armadillo>

#include "texture/texture.hpp"
#include "../object/object.hpp"
#include "../transform/rect/rect.hpp"

/**
Expand Down
1 change: 0 additions & 1 deletion include/bounce/scene/menu/menuitem/menuitem.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#include "../../../object/object.hpp"
#include "../menu.hpp"

class MenuItem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <sstream>

#include "../menu.hpp"
#include "../../../renderer/renderables.hpp"

class TextInputPopUp : public Menu {
public:
Expand Down
2 changes: 0 additions & 2 deletions include/bounce/scene/scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include "camera/camera.hpp"
#include "../input/context/context.hpp"
#include "../object/object.hpp"
#include "../transform/transform.hpp"
#include "../physics/field/field.hpp"
#include "../game/game.hpp"
Expand All @@ -16,7 +15,6 @@ class Scene: public InputContext, public Transform {
Scene(json j, int dummy);

[[nodiscard]] const std::string &get_name() const;
std::vector<Object *> find_objects_near_to(Transform *t, double radius) const;

void add_field(ForceField *field);
[[nodiscard]] const std::vector<ForceField *> &get_fields() const;
Expand Down
Loading

0 comments on commit 037b218

Please sign in to comment.