Skip to content
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

Fix 195 #207

Merged
merged 1 commit into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 53 additions & 7 deletions include/System/Physics/sPhysics.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@ namespace Hop::System::Physics
using Hop::System::Physics::sCollision;
using Hop::World::AbstractWorld;

/*
System to update cPhysics components given forces
*/
/**
* @brief Physics system.
*
*/
class sPhysics : public System
{

public:

/**
* @brief Construct a default physics system.
*
*/
sPhysics()
: dt(1.0/900.0),
dtdt(dt*dt),
Expand All @@ -45,13 +50,27 @@ namespace Hop::System::Physics
subSamples(1)
{}

/**
* @brief Update objects
*
* @param m object manager.
* @param collisions optional collision system.
* @param world optional world.
*/
void step
(
EntityComponentSystem * m,
sCollision * collisions,
AbstractWorld * world
sCollision * collisions = nullptr,
AbstractWorld * world = nullptr
);

/**
* @brief Set the strength and direction of gravity.
*
* @param g magnitude.
* @param nx direction in x.
* @param ny direction in y.
*/
void setGravityForce
(
double g,
Expand All @@ -64,6 +83,15 @@ namespace Hop::System::Physics
ngy = ny;
}

/**
* @brief Apply a force to object i.
*
* @param m manager.
* @param i object to apply force to.
* @param fx force in x.
* @param fy force in y.
* @param global apply to all mesh components equally.
*/
void applyForce(
EntityComponentSystem * m,
Id & i,
Expand All @@ -72,21 +100,39 @@ namespace Hop::System::Physics
bool global = false
);

/**
* @brief Apply a torque to object i.
*
* @param m manager.
* @param i object to apply torque to.
* @param tau torque.
*/
void applyTorque(
EntityComponentSystem * m,
Id & i,
double tau
);

/**
* @brief Apply a force to all objects.
*
* @param m manager.
* @param fx force in x.
* @param fy force in y.
* @param global apply to each objects mesh globally.
*/
void applyForce(
EntityComponentSystem * m,
double fx,
double fy,
bool global = false
);

// automatically compute stable simulation parameters
// updating all objects
/**
* @brief Determine parameters for stable forces (resp. collisions).
*
* @param m
*/
void stabaliseObjectParameters(Hop::Object::EntityComponentSystem * m);

int lua_setTimeStep(lua_State * lua)
Expand Down
19 changes: 15 additions & 4 deletions src/System/Physics/sPhysics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,23 @@ namespace Hop::System::Physics
AbstractWorld * world
)
{
for (unsigned k = 0 ; k < subSamples; k++)
if (collisions != nullptr && world != nullptr)
{
for (unsigned k = 0 ; k < subSamples; k++)
{

collisions->update(m, world);
gravityForce(m);
update(m);
collisions->update(m, world);
gravityForce(m);
update(m);
}
}
else
{
for (unsigned k = 0 ; k < subSamples; k++)
{
gravityForce(m);
update(m);
}
}
}

Expand Down