Object2D.h
1 /*
2 
3 Copyright (c) 2018, Johnathan Corkery. (jcorkery@umich.edu)
4 All rights reserved.
5 
6 This file is part of the Dynacoe project (https://github.com/jcorks/Dynacoe)
7 Dynacoe was released under the MIT License, as detailed below.
8 
9 
10 
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is furnished
16 to do so, subject to the following conditions:
17 
18 The above copyright notice and this permission notice shall
19 be included in all copies or substantial portions of the Software.
20 
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 DEALINGS IN THE SOFTWARE.
28 
29 
30 
31 */
32 
33 #ifndef H_OBJECT2D_INCLUDED
34 #define H_OBJECT2D_INCLUDED
35 
36 #include <Dynacoe/Component.h>
37 #include <Dynacoe/Color.h>
38 #include <Dynacoe/Util/BoundingBox.h>
39 
40 
41 
42 
43 namespace Dynacoe {
44 
47 
54 
55 class Object2D : public Component {
56  public:
57 
58 
59 
60  class Collider {
61  public:
62  class Line {
63  public:
64  Line(){};
65  Line(const Dynacoe::Vector & a, const Dynacoe::Vector & b);
66  bool Intersects(const Line & other) const;
69  };
70  Collider(){};
71  Collider(const std::vector<Dynacoe::Vector> &);
72  Collider(float radius, uint32_t numPts=8);
73  Collider(const std::vector<Collider> &);
74 
75  bool WillContainPoint(const Dynacoe::Vector & other) const;
76 
77  void UpdateTransition(const Dynacoe::Vector & after);
78 
79  bool CollidesWith(const Collider & otherd) const;
80 
81 
82 
83  Entity::ID lastCollided;
84 
85  const BoundingBox & GetMomentBounds() {return smearBounds;}
86  private:
87  void SetFromPoints(const std::vector<Dynacoe::Vector> &);
88 
89  std::vector<Line> boundingBox;
90  std::vector<Line> staticPoints;
91  std::vector<Line> smear;
92 
93  BoundingBox smearBounds;
94  Vector oldPosition;
95  bool stationary;
96  bool isSet;
97 
98  };
99 
100 
101  Object2D();
102  ~Object2D();
103 
104  void OnAttach();
105 
106 
111  void AddVelocity(double factor, double direction);
112  void AddVelocityTowards(double factor, const Dynacoe::Vector &);
113 
114 
119  void SetVelocity(double factor, double direction);
120  void SetVelocityTowards(double factor, const Dynacoe::Vector &);
121 
127  void SetFrictionX(double amt);
128 
131  void SetFrictionY(double amt);
132 
135  double GetDirection();
136 
139  void Halt();
140 
143  double GetVelocityX();
144 
147  double GetVelocityY();
148 
149 
152  double GetSpeed();
153 
154 
157  void SetSpeed(double);
158 
162 
163 
164 
165  Collider collider;
166 
167 
168  static void DrawColliders(const Color & c);
169 
170  std::string GetInfo();
171 
172  void Update();
173  private:
174  friend class CollisionManager;
175  double speedX;
176  double speedY;
177  double frictionX;
178  double frictionY;
179  Dynacoe::Vector last;
180 
181 };
183 
184 }
185 
186 #endif
A standard object representing an RGBA color.
Definition: Color.h:47
3D and 2D positional vector.
Definition: Vector.h:55
void AddVelocity(double factor, double direction)
Compounds a velocity vector with the current velocity.
double GetSpeed()
Returns the speed of the object.
Definition: AssetID.h:37
void Halt()
Reduces the speed of the Entity to 0.
std::string GetInfo()
Returns a string containing human-readable information on the state of the component.
void SetVelocity(double factor, double direction)
Sets the velocity vector of an object.
A component that gives movement and collision-detection related attributes to an Entity in 2D space...
Definition: Object2D.h:55
Dynacoe::Vector GetNextPosition()
Returns the position of the Entity after another frame of movement.
void OnAttach()
Function that is called upon a host entity's Attach() function calling. It is run right after the hos...
void SetSpeed(double)
Sets the speed of the object in the current direction.
void SetFrictionY(double amt)
Same as SetFrictionX(), but in the vertical direction.
Class that extends the functionality of an Entity, but as a removable and addable object...
Definition: Component.h:66
double GetVelocityX()
Returns the horizontal component of the current velocity.
Uniquely identifies an Entity.
Definition: Entity.h:67
double GetVelocityY()
Returns the vertical component of the current velocity.
void SetFrictionX(double amt)
Sets the amount of resistance to speed measured as a percentile.
double GetDirection()
Returns the current direction that the entity is traveling in degrees.