aboutsummaryrefslogtreecommitdiffstats
path: root/yage/physics
diff options
context:
space:
mode:
Diffstat (limited to 'yage/physics')
-rw-r--r--yage/physics/CMakeLists.txt6
-rw-r--r--yage/physics/README.org27
-rw-r--r--yage/physics/body.cpp34
-rw-r--r--yage/physics/body.h56
-rw-r--r--yage/physics/collider.h43
-rw-r--r--yage/physics/collisionbody.h28
-rw-r--r--yage/physics/particlebody.cpp54
-rw-r--r--yage/physics/particlebody.h33
-rw-r--r--yage/physics/physics.h19
-rw-r--r--yage/physics/rectanglecollider.cpp36
-rw-r--r--yage/physics/rectanglecollider.h30
-rw-r--r--yage/physics/rigidbody.cpp20
-rw-r--r--yage/physics/rigidbody.h28
13 files changed, 414 insertions, 0 deletions
diff --git a/yage/physics/CMakeLists.txt b/yage/physics/CMakeLists.txt
new file mode 100644
index 00000000..46c97596
--- /dev/null
+++ b/yage/physics/CMakeLists.txt
@@ -0,0 +1,6 @@
+set(YAGE_PHYSICS_SOURCES
+ physics/rectanglecollider.cpp
+ physics/rigidbody.cpp
+ physics/particlebody.cpp
+ physics/body.cpp
+ )
diff --git a/yage/physics/README.org b/yage/physics/README.org
new file mode 100644
index 00000000..0620cc93
--- /dev/null
+++ b/yage/physics/README.org
@@ -0,0 +1,27 @@
+#+ TITLE : README
+#+ DATE : <2017 - 04 - 17 Mon>
+#+ AUTHOR:
+#+ EMAIL : yannherklotz @yann - arch
+#+ OPTIONS : ':nil *:t -:t ::t <:t H:3 \n:nil ^:t arch:headline
+#+ OPTIONS : author : t c : nil creator : comment d : (not"LOGBOOK") date : t
+#+ OPTIONS : e : t email : nil f : t inline : t num : t p : nil pri : nil stat : t
+#+ OPTIONS : tags : t tasks : t tex : t timestamp : t toc : t todo : t | : t
+#+ CREATOR : Emacs 25.1.1(Org mode 8.2.10)
+#+ DESCRIPTION:
+#+ EXCLUDE_TAGS : noexport
+#+ KEYWORDS:
+#+ LANGUAGE : en
+#+ SELECT_TAGS : export
+
+*Physics Engine
+
+ **Acceleration,
+ speed and position
+
+ I have a = dv / dt;
+v = dp / dt;
+
+I am going to use the second order runga kutta method with a = 0, b = 1,
+ alpha =
+ 1 / 2 and beta =
+ 1 / 2
diff --git a/yage/physics/body.cpp b/yage/physics/body.cpp
new file mode 100644
index 00000000..8d38e70a
--- /dev/null
+++ b/yage/physics/body.cpp
@@ -0,0 +1,34 @@
+/* ----------------------------------------------------------------------------
+ * body.cpp
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com> -- MIT License
+ * See file LICENSE for more details
+ * ----------------------------------------------------------------------------
+ */
+
+#include <YAGE/Physics/body.h>
+
+#include <utility>
+
+namespace yage
+{
+
+const double Body::GRAVITY = -9.81;
+
+double Body::xPosition() const
+{
+ return position_[0];
+}
+
+double Body::yPosition() const
+{
+ return position_[1];
+}
+
+Body::Body(Vector2d position, double mass, Vector2d velocity, bool gravity)
+ : position_(std::move(position)), mass_(mass),
+ velocity_(std::move(velocity)), gravity_(gravity)
+{
+}
+
+} // namespace yage
diff --git a/yage/physics/body.h b/yage/physics/body.h
new file mode 100644
index 00000000..bd33a9ac
--- /dev/null
+++ b/yage/physics/body.h
@@ -0,0 +1,56 @@
+/* ----------------------------------------------------------------------------
+ * body.h
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com> -- MIT License
+ * See file LICENSE for more details
+ * ----------------------------------------------------------------------------
+ */
+
+#ifndef YAGE_PHYSICS_BODY_H
+#define YAGE_PHYSICS_BODY_H
+
+#include <YAGE/Math/matrix.h>
+
+namespace yage
+{
+class Body
+{
+public:
+ // gravity constant
+ static const double GRAVITY;
+
+protected:
+ // center of mass of the object
+ Vector2d position_ = Vector2d(0, 0);
+
+ // mass of the object
+ double mass_ = 1;
+
+ // current velocity of the object
+ Vector2d velocity_ = Vector2d(0, 0);
+
+ // boolean that defines if gravity can act on the object
+ bool gravity_ = true;
+
+ // current acceleration
+ Vector2d acceleration_ = Vector2d(0, 0);
+
+ // force acting on the body
+ Vector2d force_ = Vector2d(0, 0);
+
+public:
+ // apply force to the object and update the velocity
+ virtual void applyForce(const Vector2d &force) = 0;
+ virtual void update() = 0;
+
+ double xPosition() const;
+ double yPosition() const;
+
+protected:
+ // protected constructor to initialize member variables
+ Body(Vector2d position = Vector2d(0, 0), double mass = 1,
+ Vector2d velocity = Vector2d(0, 0), bool gravity = false);
+};
+} // namespace yage
+
+#endif
diff --git a/yage/physics/collider.h b/yage/physics/collider.h
new file mode 100644
index 00000000..2fd2ff89
--- /dev/null
+++ b/yage/physics/collider.h
@@ -0,0 +1,43 @@
+/* ----------------------------------------------------------------------------
+ * collider.h
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com> -- MIT License
+ * See file LICENSE for more details
+ * ----------------------------------------------------------------------------
+ */
+
+#ifndef YAGE_PHYSICS_COLLIDER_H
+#define YAGE_PHYSICS_COLLIDER_H
+
+#include <glm/glm.hpp>
+
+namespace yage
+{
+
+// The Collider class helps collision detection by providing a general shape
+// for different shapes to have their own collision algorithms.
+class Collider
+{
+protected:
+ // position of the object
+ glm::vec2 position_;
+
+ // size of the object
+ glm::vec2 size_;
+
+public:
+ Collider(const glm::vec2 &position, const glm::vec2 &size)
+ : position_(position), size_(size)
+ {
+ }
+
+ // function that checks if two colliders are colliding
+ virtual bool collides(const Collider &collider) const = 0;
+
+ // function that returns if a point is inside the shape
+ virtual bool inside(const glm::vec2 &point) const = 0;
+};
+
+} // namespace yage
+
+#endif
diff --git a/yage/physics/collisionbody.h b/yage/physics/collisionbody.h
new file mode 100644
index 00000000..715c4a54
--- /dev/null
+++ b/yage/physics/collisionbody.h
@@ -0,0 +1,28 @@
+/* ----------------------------------------------------------------------------
+ * collisionbody.h
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com> -- MIT License
+ * See file LICENSE for more details
+ * ----------------------------------------------------------------------------
+ */
+
+#ifndef YAGE_COLLISION_BODY_H
+#define YAGE_COLLISION_BODY_H
+
+#include "body.h"
+
+namespace yage
+{
+
+// a collision body will be a body that is static and not affected by gravity,
+// with infinite mass
+class CollisionBody : public Body
+{
+public:
+ CollisionBody();
+ virtual ~CollisionBody();
+};
+
+} // yage
+
+#endif
diff --git a/yage/physics/particlebody.cpp b/yage/physics/particlebody.cpp
new file mode 100644
index 00000000..bdb81eac
--- /dev/null
+++ b/yage/physics/particlebody.cpp
@@ -0,0 +1,54 @@
+/* ----------------------------------------------------------------------------
+ * particlebody.cpp
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com> -- MIT License
+ * See file LICENSE for more details
+ * ----------------------------------------------------------------------------
+ */
+
+#include <YAGE/Physics/particlebody.h>
+
+#include <cmath>
+#include <iostream>
+
+namespace yage
+{
+
+ParticleBody::ParticleBody(const Vector2d &position, double mass,
+ const Vector2d &velocity, bool gravity)
+ : Body(position, mass, velocity, gravity)
+{
+}
+
+void ParticleBody::applyForce(const Vector2d &force)
+{
+ force_ += force;
+}
+
+void ParticleBody::update()
+{
+ // set the time_step for 60fps
+ double time_step = 1.0 / 60.0;
+
+ // set the last acceleration
+ Vector2d last_acceleration = acceleration_;
+
+ // update the position of the body
+ position_ += velocity_ * time_step +
+ (0.5 * last_acceleration * std::pow(time_step, 2));
+
+ // update the acceleration
+ if (gravity_) {
+ acceleration_ =
+ Vector2d(force_.x() / mass_, (GRAVITY + force_.y()) / mass_);
+ } else {
+ acceleration_ = Vector2d(force_.x() / mass_, force_.y() / mass_);
+ }
+
+ Vector2d avg_acceleration = (acceleration_ + last_acceleration) / 2.0;
+
+ // update the velocity of the body
+ velocity_ += avg_acceleration * time_step;
+}
+
+} // namespace yage
diff --git a/yage/physics/particlebody.h b/yage/physics/particlebody.h
new file mode 100644
index 00000000..a0b9bdad
--- /dev/null
+++ b/yage/physics/particlebody.h
@@ -0,0 +1,33 @@
+/* ----------------------------------------------------------------------------
+ * particlebody.h
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com> -- MIT License
+ * See file LICENSE for more details
+ * ----------------------------------------------------------------------------
+ */
+
+#ifndef YAGE_PHYSICS_PARTICLE_BODY_H
+#define YAGE_PHYSICS_PARTICLE_BODY_H
+
+#include "body.h"
+
+#include <YAGE/Math/matrix.h>
+
+namespace yage
+{
+
+class ParticleBody : public Body
+{
+public:
+ ParticleBody(const Vector2d &position = Vector2d(0, 0), double mass = 1,
+ const Vector2d &velocity = Vector2d(0, 0),
+ bool gravity = true);
+
+ // apply a force to the rigid body
+ void applyForce(const Vector2d &force) override;
+ void update() override;
+};
+
+} // namespace yage
+
+#endif
diff --git a/yage/physics/physics.h b/yage/physics/physics.h
new file mode 100644
index 00000000..900f4b6a
--- /dev/null
+++ b/yage/physics/physics.h
@@ -0,0 +1,19 @@
+/* ----------------------------------------------------------------------------
+ * physics.h
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com> -- MIT License
+ * See file LICENSE for more details
+ * ----------------------------------------------------------------------------
+ */
+
+#ifndef YAGE_PHYSICS_H
+#define YAGE_PHYSICS_H
+
+#include "body.h"
+#include "collider.h"
+#include "collisionbody.h"
+#include "particlebody.h"
+#include "rectanglecollider.h"
+#include "rigidbody.h"
+
+#endif
diff --git a/yage/physics/rectanglecollider.cpp b/yage/physics/rectanglecollider.cpp
new file mode 100644
index 00000000..64887278
--- /dev/null
+++ b/yage/physics/rectanglecollider.cpp
@@ -0,0 +1,36 @@
+/* ----------------------------------------------------------------------------
+ * rectanglecollider.cpp
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com> -- MIT License
+ * See file LICENSE for more details
+ * ----------------------------------------------------------------------------
+ */
+
+#include <YAGE/Physics/rectanglecollider.h>
+
+namespace yage
+{
+
+RectangleCollider::RectangleCollider(const glm::vec2 &position,
+ const glm::vec2 &size)
+ : Collider(position, size)
+{
+}
+
+bool RectangleCollider::collides(const Collider &collider) const
+{
+ for (int i = position_.x; i < position_.x + size_.x; ++i) {
+ for (int j = position_.y; j < position_.y + size_.y; ++j) {
+ return collider.inside(glm::vec2(i, j));
+ }
+ }
+ return false;
+}
+
+inline bool RectangleCollider::inside(const glm::vec2 &point) const
+{
+ return position_.x < point.x && position_.x + size_.x > point.x &&
+ position_.y < point.y && position_.y + size_.y > point.y;
+}
+
+} // namespace yage
diff --git a/yage/physics/rectanglecollider.h b/yage/physics/rectanglecollider.h
new file mode 100644
index 00000000..c009f665
--- /dev/null
+++ b/yage/physics/rectanglecollider.h
@@ -0,0 +1,30 @@
+/* ----------------------------------------------------------------------------
+ * rectanglecollider.h
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com> -- MIT License
+ * See file LICENSE for more details
+ * ----------------------------------------------------------------------------
+ */
+
+#ifndef YAGE_RECTANGLE_COLLIDER_H
+#define YAGE_RECTANGLE_COLLIDER_H
+
+#include "collider.h"
+
+#include <glm/glm.hpp>
+
+namespace yage
+{
+
+class RectangleCollider : public Collider
+{
+public:
+ RectangleCollider(const glm::vec2 &position, const glm::vec2 &size);
+
+ bool collides(const Collider &collider) const override;
+ bool inside(const glm::vec2 &point) const override;
+};
+
+} // namespace yage
+
+#endif
diff --git a/yage/physics/rigidbody.cpp b/yage/physics/rigidbody.cpp
new file mode 100644
index 00000000..dcab5f2f
--- /dev/null
+++ b/yage/physics/rigidbody.cpp
@@ -0,0 +1,20 @@
+/* ----------------------------------------------------------------------------
+ * rigidbody.cpp
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com> -- MIT License
+ * See file LICENSE for more details
+ * ----------------------------------------------------------------------------
+ */
+
+#include <YAGE/Physics/rigidbody.h>
+
+namespace yage
+{
+
+RigidBody::RigidBody(const Vector2d &position, double mass,
+ const Vector2d &velocity, bool gravity)
+ : ParticleBody(position, mass, velocity, gravity)
+{
+}
+
+} // namespace yage
diff --git a/yage/physics/rigidbody.h b/yage/physics/rigidbody.h
new file mode 100644
index 00000000..67ccb4ca
--- /dev/null
+++ b/yage/physics/rigidbody.h
@@ -0,0 +1,28 @@
+/* ----------------------------------------------------------------------------
+ * rigidbody.h
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com> -- MIT License
+ * See file LICENSE for more details
+ * ----------------------------------------------------------------------------
+ */
+
+#ifndef YAGE_RIGID_BODY_H
+#define YAGE_RIGID_BODY_H
+
+#include "particlebody.h"
+
+#include <glm/glm.hpp>
+
+namespace yage
+{
+
+class RigidBody : public ParticleBody
+{
+public:
+ RigidBody(const Vector2d &position = Vector2d(0, 0), double mass = 1,
+ const Vector2d &velocity = Vector2d(0, 0), bool gravity = true);
+};
+
+} // namespace yage
+
+#endif