Skip to content

Commit

Permalink
RigidBody.h:
Browse files Browse the repository at this point in the history
* Turning RigidBody struct into a class and privatizing the data members.
* Adding velocity and acceleration which update the position in the update() function.
  • Loading branch information
razterizer committed Oct 25, 2024
1 parent eb22bcd commit 92f8527
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
10 changes: 5 additions & 5 deletions Dynamics/DynamicsSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ namespace dynamics

public:

RigidBody* add_rigid_body(Sprite* sprite)
RigidBody* add_rigid_body(Sprite* sprite, const Vec2& vel = {}, const Vec2& acc = {})
{
auto& rb = m_rigid_bodies.emplace_back(std::make_unique<RigidBody>());
rb->init_from_sprite(sprite);
auto& rb = m_rigid_bodies.emplace_back(std::make_unique<RigidBody>(sprite, vel, acc));
return rb.get();
}

void update()
void update(float dt, int sim_frame)
{

for (auto& rb : m_rigid_bodies)
rb->update(dt, sim_frame);
}
};

Expand Down
16 changes: 12 additions & 4 deletions Dynamics/RigidBody.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,36 @@
namespace dynamics
{

struct RigidBody
class RigidBody
{
AABB<float> curr_aabb;
Vec2 orig_pos;
Vec2 curr_centroid;
Vec2 centroid_to_orig_pos;

Vec2 curr_centroid;
Vec2 curr_vel;
Vec2 curr_acc;

Sprite* sprite = nullptr; // Position to be controlled by this rigid body object.

void init_from_sprite(Sprite* s)
public:
RigidBody(Sprite* s, const Vec2& vel = {}, const Vec2& acc = {})
{
sprite = s;
orig_pos = s->pos;
curr_aabb = s->calc_curr_AABB(0).convert<float>();
curr_centroid = s->calc_curr_centroid(0);
centroid_to_orig_pos = orig_pos - curr_centroid;
curr_vel = vel;
curr_acc = acc;
}

void update(int sim_frame)
void update(float dt, int sim_frame)
{
if (sprite != nullptr)
{
curr_vel += curr_acc * dt;
curr_centroid += curr_vel * dt;
//sprite->pos = curr_pos.to_RC_round();
sprite->pos = (curr_centroid + centroid_to_orig_pos).to_RC_round();
curr_aabb = sprite->calc_curr_AABB(sim_frame).convert<float>();
Expand Down

0 comments on commit 92f8527

Please sign in to comment.