-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from claudiosegala/sixth
Sixth
- Loading branch information
Showing
45 changed files
with
1,288 additions
and
633 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#pragma once | ||
|
||
#include <Util.h> | ||
#include <GameObject.h> | ||
#include <Collision.h> | ||
#include <Component.h> | ||
#include <Vec2.h> | ||
#include <Rect.h> | ||
|
||
class Collider : public Component { | ||
public: | ||
|
||
Rect box; | ||
|
||
Collider (GameObject&, Vec2 scale = {1, 1}, Vec2 offset = {0, 0}); | ||
|
||
void Update(float); | ||
|
||
void Render(); | ||
|
||
bool Is(std::string); | ||
|
||
void SetScale(Vec2); | ||
|
||
void SetOffset(Vec2); | ||
|
||
private: | ||
|
||
Vec2 offset; | ||
|
||
Vec2 scale; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#pragma once | ||
|
||
#include <Rect.h> | ||
#include <Vec2.h> | ||
|
||
#include <algorithm> | ||
#include <cmath> | ||
|
||
class Collision { | ||
public: | ||
|
||
// Observação: IsColliding espera ângulos em radianos! | ||
// Para usar graus, forneça a sua própria implementação de Rotate, | ||
// ou transforme os ângulos no corpo de IsColliding. | ||
static inline bool IsColliding(Rect& a, Rect& b, float angleOfA, float angleOfB) { | ||
Vec2 A[] = { Vec2( a.vector.x, a.vector.y + a.height ), | ||
Vec2( a.vector.x + a.width, a.vector.y + a.height ), | ||
Vec2( a.vector.x + a.width, a.vector.y ), | ||
Vec2( a.vector.x, a.vector.y ) | ||
}; | ||
Vec2 B[] = { Vec2( b.vector.x, b.vector.y + b.height ), | ||
Vec2( b.vector.x + b.width, b.vector.y + b.height ), | ||
Vec2( b.vector.x + b.width, b.vector.y ), | ||
Vec2( b.vector.x, b.vector.y ) | ||
}; | ||
|
||
for (auto& v : A) { | ||
v = Rotate(v - a.Center(), angleOfA) + a.Center(); | ||
} | ||
|
||
for (auto& v : B) { | ||
v = Rotate(v - b.Center(), angleOfB) + b.Center(); | ||
} | ||
|
||
Vec2 axes[] = { Norm(A[0] - A[1]), Norm(A[1] - A[2]), Norm(B[0] - B[1]), Norm(B[1] - B[2]) }; | ||
|
||
for (auto& axis : axes) { | ||
float P[4]; | ||
|
||
for (int i = 0; i < 4; ++i) P[i] = Dot(A[i], axis); | ||
|
||
float minA = *std::min_element(P, P + 4); | ||
float maxA = *std::max_element(P, P + 4); | ||
|
||
for (int i = 0; i < 4; ++i) P[i] = Dot(B[i], axis); | ||
|
||
float minB = *std::min_element(P, P + 4); | ||
float maxB = *std::max_element(P, P + 4); | ||
|
||
if (maxA < minB || minA > maxB) | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private: | ||
|
||
static inline float Mag(const Vec2& p) { | ||
return std::sqrt(p.x * p.x + p.y * p.y); | ||
} | ||
|
||
static inline Vec2 Norm(const Vec2& p) { | ||
return p * (1.f / Mag(p)); | ||
} | ||
|
||
static inline float Dot(const Vec2& a, const Vec2& b) { | ||
return a.x * b.x + a.y * b.y; | ||
} | ||
|
||
static inline Vec2 Rotate(const Vec2& p, float angle) { | ||
float cs = std::cos(angle), sn = std::sin(angle); | ||
return Vec2 ( p.x * cs - p.y * sn, p.x * sn + p.y * cs ); | ||
} | ||
}; | ||
|
||
// Aqui estão três operadores que sua classe Vec2 deve precisar, se já não tiver. | ||
// Se sua classe tiver métodos para Mag, Norm, Dot e/ou Rotate, você pode substituir | ||
// os usos desses métodos por usos dos seus, mas garanta que deem resultados corretos. | ||
|
||
// Vec2 operator+(const Vec2& rhs) const { | ||
// return Vec2(x + rhs.x, y + rhs.y); | ||
// } | ||
|
||
// Vec2 operator-(const Vec2& rhs) const { | ||
// return Vec2(x - rhs.x, y - rhs.y); | ||
// } | ||
|
||
// Vec2 operator*(const float rhs) const { | ||
// return Vec2(x * rhs, y * rhs); | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.