ELEC2645 (2018/19) / Mbed 2 deprecated el17dg

Dependencies:   mbed

Fork of el17dg by Dmitrijs Griskovs

Committer:
Noximilien
Date:
Tue Apr 16 21:16:33 2019 +0000
Revision:
30:d454d0cb72bc
Parent:
29:579e00b7f118
Child:
31:becb8f6bf7b7
Hace modified some comments. Have added a feature of a force shield. Updated the tutorial, Have finished the settings mode.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Noximilien 21:0eb394495b8a 1 #ifndef GEOMETRY_H
Noximilien 21:0eb394495b8a 2 #define GEOMETRY_H
Noximilien 21:0eb394495b8a 3
Noximilien 21:0eb394495b8a 4 #include <math.h>
Noximilien 29:579e00b7f118 5
Noximilien 29:579e00b7f118 6 ////////////////////////////////////////////////////////////////////////
Noximilien 29:579e00b7f118 7 /**
Noximilien 30:d454d0cb72bc 8 * A structure to represent the positions of a sprite on the screen.
Noximilien 29:579e00b7f118 9 */
Noximilien 21:0eb394495b8a 10 struct Point {
Noximilien 29:579e00b7f118 11 /*@{*/
Noximilien 29:579e00b7f118 12 int x; /**< the x position */
Noximilien 29:579e00b7f118 13 int y; /**< the y position */
Noximilien 21:0eb394495b8a 14 Point() : x(0), y(0) {}
Noximilien 21:0eb394495b8a 15 Point(int _x, int _y) : x(_x), y(_y) {}
Noximilien 21:0eb394495b8a 16
Noximilien 21:0eb394495b8a 17 Point operator+ (const Point& rhs) const {
Noximilien 21:0eb394495b8a 18 Point result(x + rhs.x, y + rhs.y);
Noximilien 21:0eb394495b8a 19 return result;
Noximilien 21:0eb394495b8a 20 }
Noximilien 21:0eb394495b8a 21 Point operator- (const Point& rhs) const {
Noximilien 21:0eb394495b8a 22 Point result(x - rhs.x, y - rhs.y);
Noximilien 21:0eb394495b8a 23 return result;
Noximilien 21:0eb394495b8a 24 }
Noximilien 21:0eb394495b8a 25 int lengthSquared() {
Noximilien 21:0eb394495b8a 26 return x * x + y * y;
Noximilien 21:0eb394495b8a 27 }
Noximilien 21:0eb394495b8a 28 };
Noximilien 29:579e00b7f118 29 /** CicleBounds Class
Noximilien 29:579e00b7f118 30 * @brief A library for appointing the sprites' bounds.
Noximilien 29:579e00b7f118 31 * @author Dmitrijs Griskovs
Noximilien 29:579e00b7f118 32 * @date 15/04/2019
Noximilien 29:579e00b7f118 33 */
Noximilien 21:0eb394495b8a 34 class CircleBounds {
Noximilien 21:0eb394495b8a 35 public:
Noximilien 29:579e00b7f118 36 ///////////////////////////////////////////////////////////////////////////////////////
Noximilien 29:579e00b7f118 37 /** Constructor */
Noximilien 27:f05f4e738ba9 38 CircleBounds() {};
Noximilien 27:f05f4e738ba9 39 CircleBounds(Point _center, float _radius): center(_center), radius(_radius) {};
Noximilien 21:0eb394495b8a 40 Point center;
Noximilien 21:0eb394495b8a 41 float radius;
Noximilien 30:d454d0cb72bc 42
Noximilien 30:d454d0cb72bc 43
Noximilien 29:579e00b7f118 44 /** @brief a function that determines whether the two sprites have collided.
Noximilien 29:579e00b7f118 45 * @details the function takes in the position and the bounds (center and radius)
Noximilien 29:579e00b7f118 46 * two sprites (x and y positions and the circle area of a sprite) then performs the
Noximilien 29:579e00b7f118 47 * pythagoras calaculation and returns a bool statment if there is an overlap
Noximilien 29:579e00b7f118 48 * in the circle area of two sprites.
Noximilien 29:579e00b7f118 49 *
Noximilien 29:579e00b7f118 50 * @param pos_a positon of x and y of first sprite. (const Point&).
Noximilien 29:579e00b7f118 51 * @param bounds_a center point and radius of the first sprite. (const CircleBounds&).
Noximilien 29:579e00b7f118 52 * @param pos_b positon of x and y of second sprite. (const Point&).
Noximilien 29:579e00b7f118 53 * @param bounds_b center point and radius of the second sprite. (const CircleBounds&).
Noximilien 29:579e00b7f118 54 * @return difference.lengthSquared() if the areas of two sprites overlap, it will return true.
Noximilien 29:579e00b7f118 55 */
Noximilien 30:d454d0cb72bc 56 inline bool circleCollideTwoObjects(
Noximilien 21:0eb394495b8a 57 const Point& pos_a, const CircleBounds& bounds_a,
Noximilien 30:d454d0cb72bc 58 const Point& pos_b, const CircleBounds& bounds_b) {
Noximilien 21:0eb394495b8a 59 Point center_a = pos_a + bounds_a.center;
Noximilien 21:0eb394495b8a 60 Point center_b = pos_b + bounds_b.center;
Noximilien 21:0eb394495b8a 61 Point difference = center_a - center_b;
Noximilien 21:0eb394495b8a 62 return difference.lengthSquared() <= pow(bounds_a.radius + bounds_b.radius, 2);
Noximilien 30:d454d0cb72bc 63 }
Noximilien 30:d454d0cb72bc 64 };
Noximilien 21:0eb394495b8a 65
Noximilien 21:0eb394495b8a 66 #endif