aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/system
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/system')
-rw-r--r--src/crepe/system/AISystem.cpp10
-rw-r--r--src/crepe/system/PhysicsSystem.cpp131
-rw-r--r--src/crepe/system/RenderSystem.cpp23
-rw-r--r--src/crepe/system/RenderSystem.h11
4 files changed, 86 insertions, 89 deletions
diff --git a/src/crepe/system/AISystem.cpp b/src/crepe/system/AISystem.cpp
index d231c7c..680dbb8 100644
--- a/src/crepe/system/AISystem.cpp
+++ b/src/crepe/system/AISystem.cpp
@@ -13,12 +13,10 @@ using namespace std::chrono;
void AISystem::update() {
const Mediator & mediator = this->mediator;
ComponentManager & mgr = mediator.component_manager;
- LoopTimerManager & timer = mediator.loop_timer;
- RefVector<AI> ai_components = mgr.get_components_by_type<AI>();
LoopTimerManager & loop_timer = mediator.loop_timer;
+ RefVector<AI> ai_components = mgr.get_components_by_type<AI>();
- //TODO: Use fixed loop dt (this is not available at master at the moment)
- duration_t dt = loop_timer.get_delta_time();
+ float dt = loop_timer.get_scaled_fixed_delta_time().count();
// Loop through all AI components
for (AI & ai : ai_components) {
@@ -45,7 +43,7 @@ void AISystem::update() {
// Calculate the acceleration (using the above calculated force)
vec2 acceleration = force / rigidbody.data.mass;
// Finally, update Rigidbody's velocity
- rigidbody.data.linear_velocity += acceleration * duration_cast<seconds>(dt).count();
+ rigidbody.data.linear_velocity += acceleration * dt;
}
}
@@ -146,7 +144,7 @@ vec2 AISystem::arrive(const AI & ai, const Rigidbody & rigidbody,
}
float speed = distance / ai.arrive_deceleration;
- speed = std::min(speed, rigidbody.data.max_linear_velocity.length());
+ speed = std::min(speed, rigidbody.data.max_linear_velocity);
vec2 desired_velocity = to_target * (speed / distance);
return desired_velocity - rigidbody.data.linear_velocity;
diff --git a/src/crepe/system/PhysicsSystem.cpp b/src/crepe/system/PhysicsSystem.cpp
index ebf4439..3b3b8ab 100644
--- a/src/crepe/system/PhysicsSystem.cpp
+++ b/src/crepe/system/PhysicsSystem.cpp
@@ -5,88 +5,95 @@
#include "../api/Transform.h"
#include "../api/Vector2.h"
#include "../manager/ComponentManager.h"
+#include "../manager/LoopTimerManager.h"
+#include "../manager/Mediator.h"
#include "PhysicsSystem.h"
using namespace crepe;
void PhysicsSystem::update() {
- ComponentManager & mgr = this->mediator.component_manager;
+
+ const Mediator & mediator = this->mediator;
+ ComponentManager & mgr = mediator.component_manager;
+ LoopTimerManager & loop_timer = mediator.loop_timer;
RefVector<Rigidbody> rigidbodies = mgr.get_components_by_type<Rigidbody>();
- RefVector<Transform> transforms = mgr.get_components_by_type<Transform>();
+ float dt = loop_timer.get_scaled_fixed_delta_time().count();
- double gravity = Config::get_instance().physics.gravity;
+ float gravity = Config::get_instance().physics.gravity;
for (Rigidbody & rigidbody : rigidbodies) {
if (!rigidbody.active) continue;
+ Transform & transform
+ = mgr.get_components_by_id<Transform>(rigidbody.game_object_id).front().get();
switch (rigidbody.data.body_type) {
case Rigidbody::BodyType::DYNAMIC:
- for (Transform & transform : transforms) {
- if (transform.game_object_id == rigidbody.game_object_id) {
+ if (transform.game_object_id == rigidbody.game_object_id) {
+ // Add gravity
- // Add gravity
- if (rigidbody.data.gravity_scale > 0) {
- rigidbody.data.linear_velocity.y
- += (rigidbody.data.mass * rigidbody.data.gravity_scale
- * gravity);
- }
- // Add damping
- if (rigidbody.data.angular_velocity_coefficient > 0) {
- rigidbody.data.angular_velocity
- *= rigidbody.data.angular_velocity_coefficient;
- }
- if (rigidbody.data.linear_velocity_coefficient.x > 0
- && rigidbody.data.linear_velocity_coefficient.y > 0) {
- rigidbody.data.linear_velocity
- *= rigidbody.data.linear_velocity_coefficient;
- }
+ if (rigidbody.data.mass <= 0) {
+ throw std::runtime_error("Mass must be greater than 0");
+ }
- // Max velocity check
- if (rigidbody.data.angular_velocity
- > rigidbody.data.max_angular_velocity) {
- rigidbody.data.angular_velocity
- = rigidbody.data.max_angular_velocity;
- } else if (rigidbody.data.angular_velocity
- < -rigidbody.data.max_angular_velocity) {
- rigidbody.data.angular_velocity
- = -rigidbody.data.max_angular_velocity;
- }
+ if (gravity <= 0) {
+ throw std::runtime_error("Config Gravity must be greater than 0");
+ }
- if (rigidbody.data.linear_velocity.x
- > rigidbody.data.max_linear_velocity.x) {
- rigidbody.data.linear_velocity.x
- = rigidbody.data.max_linear_velocity.x;
- } else if (rigidbody.data.linear_velocity.x
- < -rigidbody.data.max_linear_velocity.x) {
- rigidbody.data.linear_velocity.x
- = -rigidbody.data.max_linear_velocity.x;
- }
+ if (rigidbody.data.gravity_scale > 0 && !rigidbody.data.constraints.y) {
+ rigidbody.data.linear_velocity.y
+ += (rigidbody.data.mass * rigidbody.data.gravity_scale * gravity
+ * dt);
+ }
+ // Add coefficient rotation
+ if (rigidbody.data.angular_velocity_coefficient > 0) {
+ rigidbody.data.angular_velocity
+ *= std::pow(rigidbody.data.angular_velocity_coefficient, dt);
+ }
- if (rigidbody.data.linear_velocity.y
- > rigidbody.data.max_linear_velocity.y) {
- rigidbody.data.linear_velocity.y
- = rigidbody.data.max_linear_velocity.y;
- } else if (rigidbody.data.linear_velocity.y
- < -rigidbody.data.max_linear_velocity.y) {
- rigidbody.data.linear_velocity.y
- = -rigidbody.data.max_linear_velocity.y;
- }
+ // Add coefficient movement horizontal
+ if (rigidbody.data.linear_velocity_coefficient.x > 0
+ && !rigidbody.data.constraints.x) {
+ rigidbody.data.linear_velocity.x
+ *= std::pow(rigidbody.data.linear_velocity_coefficient.x, dt);
+ }
- // Move object
- if (!rigidbody.data.constraints.rotation) {
- transform.rotation += rigidbody.data.angular_velocity;
- transform.rotation = std::fmod(transform.rotation, 360.0);
- if (transform.rotation < 0) {
- transform.rotation += 360.0;
- }
- }
- if (!rigidbody.data.constraints.x) {
- transform.position.x += rigidbody.data.linear_velocity.x;
- }
- if (!rigidbody.data.constraints.y) {
- transform.position.y += rigidbody.data.linear_velocity.y;
+ // Add coefficient movement horizontal
+ if (rigidbody.data.linear_velocity_coefficient.y > 0
+ && !rigidbody.data.constraints.y) {
+ rigidbody.data.linear_velocity.y
+ *= std::pow(rigidbody.data.linear_velocity_coefficient.y, dt);
+ }
+
+ // Max velocity check
+ if (rigidbody.data.angular_velocity
+ > rigidbody.data.max_angular_velocity) {
+ rigidbody.data.angular_velocity = rigidbody.data.max_angular_velocity;
+ } else if (rigidbody.data.angular_velocity
+ < -rigidbody.data.max_angular_velocity) {
+ rigidbody.data.angular_velocity = -rigidbody.data.max_angular_velocity;
+ }
+
+ // Set max velocity to maximum length
+ if (rigidbody.data.linear_velocity.length()
+ > rigidbody.data.max_linear_velocity) {
+ rigidbody.data.linear_velocity.normalize();
+ rigidbody.data.linear_velocity *= rigidbody.data.max_linear_velocity;
+ }
+
+ // Move object
+ if (!rigidbody.data.constraints.rotation) {
+ transform.rotation += rigidbody.data.angular_velocity * dt;
+ transform.rotation = std::fmod(transform.rotation, 360.0);
+ if (transform.rotation < 0) {
+ transform.rotation += 360.0;
}
}
+ if (!rigidbody.data.constraints.x) {
+ transform.position.x += rigidbody.data.linear_velocity.x * dt;
+ }
+ if (!rigidbody.data.constraints.y) {
+ transform.position.y += rigidbody.data.linear_velocity.y * dt;
+ }
}
break;
case Rigidbody::BodyType::KINEMATIC:
diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp
index 51340fb..afd9548 100644
--- a/src/crepe/system/RenderSystem.cpp
+++ b/src/crepe/system/RenderSystem.cpp
@@ -15,6 +15,7 @@
#include "../manager/ResourceManager.h"
#include "RenderSystem.h"
+#include "types.h"
using namespace crepe;
using namespace std;
@@ -29,7 +30,7 @@ void RenderSystem::present_screen() {
ctx.present_screen();
}
-SDLContext::CameraValues RenderSystem::update_camera() {
+void RenderSystem::update_camera() {
ComponentManager & mgr = this->mediator.component_manager;
SDLContext & ctx = this->mediator.sdl_context;
RefVector<Camera> cameras = mgr.get_components_by_type<Camera>();
@@ -40,9 +41,9 @@ SDLContext::CameraValues RenderSystem::update_camera() {
if (!cam.active) continue;
const Transform & transform
= mgr.get_components_by_id<Transform>(cam.game_object_id).front().get();
- SDLContext::CameraValues cam_val = ctx.set_camera(cam);
- cam_val.cam_pos = transform.position + cam.data.postion_offset;
- return cam_val;
+ vec2 new_camera_pos = transform.position + cam.data.postion_offset;
+ ctx.update_camera_view(cam, new_camera_pos);
+ return;
}
throw std::runtime_error("No active cameras in current scene");
}
@@ -69,8 +70,7 @@ void RenderSystem::update() {
this->present_screen();
}
-bool RenderSystem::render_particle(const Sprite & sprite, const SDLContext::CameraValues & cam,
- const double & scale) {
+bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) {
ComponentManager & mgr = this->mediator.component_manager;
SDLContext & ctx = this->mediator.sdl_context;
@@ -93,7 +93,6 @@ bool RenderSystem::render_particle(const Sprite & sprite, const SDLContext::Came
ctx.draw(SDLContext::RenderContext{
.sprite = sprite,
.texture = res,
- .cam = cam,
.pos = p.position,
.angle = p.angle,
.scale = scale,
@@ -102,8 +101,7 @@ bool RenderSystem::render_particle(const Sprite & sprite, const SDLContext::Came
}
return rendering_particles;
}
-void RenderSystem::render_normal(const Sprite & sprite, const SDLContext::CameraValues & cam,
- const Transform & tm) {
+void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) {
SDLContext & ctx = this->mediator.sdl_context;
ResourceManager & resource_manager = this->mediator.resource_manager;
const Texture & res = resource_manager.get<Texture>(sprite.source);
@@ -111,7 +109,6 @@ void RenderSystem::render_normal(const Sprite & sprite, const SDLContext::Camera
ctx.draw(SDLContext::RenderContext{
.sprite = sprite,
.texture = res,
- .cam = cam,
.pos = tm.position,
.angle = tm.rotation,
.scale = tm.scale,
@@ -120,7 +117,7 @@ void RenderSystem::render_normal(const Sprite & sprite, const SDLContext::Camera
void RenderSystem::render() {
ComponentManager & mgr = this->mediator.component_manager;
- const SDLContext::CameraValues & cam = this->update_camera();
+ this->update_camera();
RefVector<Sprite> sprites = mgr.get_components_by_type<Sprite>();
RefVector<Sprite> sorted_sprites = this->sort(sprites);
@@ -130,10 +127,10 @@ void RenderSystem::render() {
const Transform & transform
= mgr.get_components_by_id<Transform>(sprite.game_object_id).front().get();
- bool rendered_particles = this->render_particle(sprite, cam, transform.scale);
+ bool rendered_particles = this->render_particle(sprite, transform.scale);
if (rendered_particles) continue;
- this->render_normal(sprite, cam, transform);
+ this->render_normal(sprite, transform);
}
}
diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h
index e270a6b..fc7b46e 100644
--- a/src/crepe/system/RenderSystem.h
+++ b/src/crepe/system/RenderSystem.h
@@ -2,8 +2,6 @@
#include <cmath>
-#include "facade/SDLContext.h"
-
#include "System.h"
#include "types.h"
@@ -14,7 +12,6 @@ class Sprite;
class Transform;
/**
- * \class RenderSystem
* \brief Manages rendering operations for all game objects.
*
* RenderSystem is responsible for rendering, clearing and presenting the screen, and
@@ -37,7 +34,7 @@ private:
void present_screen();
//! Updates the active camera used for rendering.
- SDLContext::CameraValues update_camera();
+ void update_camera();
//! Renders the whole screen
void render();
@@ -52,8 +49,7 @@ private:
* constructor is now protected i cannot make tmp inside
* \return true if particles have been rendered
*/
- bool render_particle(const Sprite & sprite, const SDLContext::CameraValues & cam,
- const double & scale);
+ bool render_particle(const Sprite & sprite, const double & scale);
/**
* \brief renders a sprite with a Transform component on the screen
@@ -61,8 +57,7 @@ private:
* \param sprite the sprite component that holds all the data
* \param tm the Transform component that holds the position,rotation and scale
*/
- void render_normal(const Sprite & sprite, const SDLContext::CameraValues & cam,
- const Transform & tm);
+ void render_normal(const Sprite & sprite, const Transform & tm);
/**
* \brief sort a vector sprite objects with