Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of el17dg by
Revision 40:e3bbda7444fa, committed 2019-05-07
- Comitter:
- Noximilien
- Date:
- Tue May 07 15:22:35 2019 +0000
- Parent:
- 39:ca77a6d574e6
- Commit message:
- The Final, Submission Version. I have read and agreed to the academic integrity. SID:201160286
Changed in this revision
| collision_lib.lib | Show diff for this revision Revisions of this file |
| collision_lib/collision_lib.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/collision_lib.lib Tue May 07 15:20:16 2019 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -collision_lib#e4d12321fffd
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/collision_lib/collision_lib.h Tue May 07 15:22:35 2019 +0000
@@ -0,0 +1,80 @@
+#ifndef COLLISION_LIB_H
+#define COLLISION_LIB_H
+
+#include <math.h>
+
+/**
+ * @struct Point
+ * @brief Position or vector on the screen. Supports basic vector arithmetics.
+ */
+struct Point {
+ /*@{*/
+ int x; /**< the x position */
+ int y; /**< the y position */
+ /** Default constructor */
+ Point() : x(0), y(0) {}
+ /** Constructor with x and y parameters */
+ Point(int _x, int _y) : x(_x), y(_y) {}
+
+ /**
+ * @brief Vector addition.
+ */
+ Point operator+ (const Point& rhs) const {
+ Point result(x + rhs.x, y + rhs.y);
+ return result;
+ }
+
+ /**
+ * @brief Vector substraction.
+ */
+ Point operator- (const Point& rhs) const {
+ Point result(x - rhs.x, y - rhs.y);
+ return result;
+ }
+
+ /**
+ * @brief Length of vector squared.
+ */
+ int lengthSquared() {
+ return x * x + y * y;
+ }
+};
+
+/**
+ * CicleBounds Class
+ * @brief Class to define object's bounds as a circle with center(e.g. offset).
+ * @author Dmitrijs Griskovs
+ * @date 15/04/2019
+ */
+class CircleBounds {
+public:
+ /** Default constructor */
+ CircleBounds() {};
+ /** Constructor with center and radius parameters */
+ CircleBounds(Point _center, float _radius): center(_center), radius(_radius) {};
+ Point center;
+ float radius;
+};
+
+/**
+ * @brief Determines whether the two circles collide.
+ * @details the function takes in position and circle bounds (center and radius)
+ * of two objects and returns wether they collided.
+ *
+ * @param pos_a positon of x and y of the first object. (const Point&).
+ * @param bounds_a center point and radius of the first object. (const CircleBounds&).
+ * @param pos_b positon of x and y of the second object. (const Point&).
+ * @param bounds_b center point and radius of the second object. (const CircleBounds&).
+ * @return true if the two circles overlap.
+ */
+inline bool circleCollideTwoObjects(
+ const Point& pos_a, const CircleBounds& bounds_a,
+ const Point& pos_b, const CircleBounds& bounds_b
+) {
+ Point center_a = pos_a + bounds_a.center;
+ Point center_b = pos_b + bounds_b.center;
+ Point difference = center_a - center_b;
+ return difference.lengthSquared() <= pow(bounds_a.radius + bounds_b.radius, 2);
+}
+
+#endif
\ No newline at end of file
