-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add physics component #29
Changes from 1 commit
4952586
ae4abd3
f47724e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,29 @@ | ||||||||||||
#include <src/components/physics.hpp> | ||||||||||||
|
||||||||||||
namespace component { | ||||||||||||
|
||||||||||||
Physics::Physics(glm::vec2 friction, float gravity, float mass) : m_friction(friction), m_gravity(gravity), m_mass(mass) {} | ||||||||||||
|
||||||||||||
void Physics::tick(bave::Seconds const dt) { | ||||||||||||
static constexpr bave::Seconds ft{0.005}; // 5ms | ||||||||||||
|
||||||||||||
if (dt.count() > tick_limit) { return; } // return for unexpected dt values, particularly during the beginning of the state | ||||||||||||
|
||||||||||||
for (bave::Seconds t{}; t <= dt; t += ft) { | ||||||||||||
integrate(t.count()); | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
void Physics::integrate(float dt) { | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||||||||||||
m_acceleration.y += m_gravity * dt; | ||||||||||||
m_velocity = (m_velocity + (m_acceleration / m_mass) * dt) * m_friction; | ||||||||||||
|
||||||||||||
m_velocity.x = m_velocity.x > max_velocity.x ? max_velocity.x : m_velocity.x; | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there must be a better way to constrain the velocity |
||||||||||||
m_velocity.y = m_velocity.y > max_velocity.y ? max_velocity.y : m_velocity.y; | ||||||||||||
m_velocity.x = m_velocity.x < -max_velocity.x ? -max_velocity.x : m_velocity.x; | ||||||||||||
m_velocity.y = m_velocity.y < -max_velocity.y ? -max_velocity.y : m_velocity.y; | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||||||||||||
|
||||||||||||
m_position += m_velocity * dt; | ||||||||||||
} | ||||||||||||
|
||||||||||||
} // namespace component |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,28 @@ | ||||||||||
#pragma once | ||||||||||
#include <bave/app.hpp> | ||||||||||
#include <bave/graphics/sprite.hpp> | ||||||||||
|
||||||||||
namespace component { | ||||||||||
|
||||||||||
static constexpr glm::vec2 max_velocity{1000.f, 1000.f}; | ||||||||||
static constexpr float tick_limit{0.03f}; //this should possibly go somewhere else and be globally accessible | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Globals in headers should not be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This represents time and should use chrono units ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also think 30ms is too little, 100ms is ok as the death-spiral-bail-out value.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed! |
||||||||||
|
||||||||||
class Physics { | ||||||||||
|
||||||||||
public: | ||||||||||
|
||||||||||
glm::vec2 m_position{}; | ||||||||||
glm::vec2 m_velocity{}; | ||||||||||
glm::vec2 m_acceleration{}; | ||||||||||
|
||||||||||
glm::vec2 m_friction{}; | ||||||||||
float m_gravity{}; | ||||||||||
float m_mass{}; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If all this is public, a constructor isn't needed, users can just do: auto physics = Physics{
.m_friction = /* ... */,
.m_gravity = /* ... */,
.m_mass = /* ... */,
}; As a style thing, this is one reason why I don't prefix There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. However, shouldn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you think friction, position, velocity etc. should be public? or should I write getters for them? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree intuitively about gravity, but being able to change it might make it easier to simulate light-weightedness (for certain enemies or drops, etc). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It depends on whether you want them to be "set once and then it's read-only" or "set at your discretion". The former will take the values in the constructor and provide getters (but not setters). Latter would get rid of the constructor and keep the members public. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Is this a member variable? Try removing the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep, it's a member variable of namespace component {
constexpr glm::vec2 max_velocity_v{1000.f};
constexpr bave::Seconds tick_limit_v{100ms};
class Physics {
bave::Seconds m_residue{};
public:
glm::vec2 position{};
glm::vec2 velocity{};
glm::vec2 acceleration{};
glm::vec2 friction{};
float gravity{};
float mass{};
void tick(bave::Seconds const dt);
void integrate(bave::Seconds dt);
};
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok I realized it's the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so, I guess I need to define a constructor then, because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, all the fields of aggregate types must be public. auto custom = Physics{/* ... */};
auto default_ = Physics{}; |
||||||||||
|
||||||||||
Physics(glm::vec2 friction, float gravity, float mass = 1.f); | ||||||||||
|
||||||||||
void tick(bave::Seconds const dt); | ||||||||||
void integrate(float dt); | ||||||||||
}; | ||||||||||
|
||||||||||
} // namespace component |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a subtle issue here: some residue of
dt
that's less thanft
remains un-integrated on everytick
. It would be better to accumulate that in a member and add it to the nextdt
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would this fix work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so, something like this would be better: