Compare commits

...

3 Commits

Author SHA1 Message Date
Kayne Ruse 7c51f820d5 I fucking hate collisions 2014-12-27 20:27:24 +11:00
Kayne Ruse 8bf45bc8e4 Merge branch 'high-water-mark' into collisions 2014-12-27 19:30:12 +11:00
Kayne Ruse 5c1ea1988e Trying to implement smooth collisions without other issues 2014-12-27 16:43:40 +11:00
4 changed files with 72 additions and 4 deletions
+56
View File
@@ -21,3 +21,59 @@
*/
#include "local_character.hpp"
bool LocalCharacter::ProcessCollisions(std::list<BoundingBox>& boxList) {
if (CheckCollisionSimple(boxList, origin + motion)) {
Vector2 velocity;
velocity.x = CorrectVelocityX(boxList, motion.x);
velocity.y = CorrectVelocityY(boxList, motion.y);
origin += velocity;
return true;
}
else {
origin += motion;
return false;
}
}
bool LocalCharacter::CheckCollisionSimple(std::list<BoundingBox>& boxList, Vector2 newPos) {
for (auto& it : boxList) {
if (it.CheckOverlap(bounds + newPos)) {
return true;
}
}
return false;
}
double LocalCharacter::CorrectVelocityX(std::list<BoundingBox>& boxList, double velocityX) {
double ret = velocityX;
for (auto& it : boxList) {
if (it.CheckOverlap(bounds + origin + Vector2(velocityX, 0) )) {
if (velocityX > 0) {
ret = std::min(ret, it.x - (origin.x + bounds.x + bounds.w -1));
ret = std::min(ret, 0.0);
}
else if (velocityX < 0) {
ret = std::max(ret, (it.x + it.w) - (origin.x + bounds.x));
ret = std::max(ret, 0.0);
}
}
}
return ret;
}
double LocalCharacter::CorrectVelocityY(std::list<BoundingBox>& boxList, double velocityY) {
double ret = velocityY;
for (auto& it : boxList) {
if (it.CheckOverlap(bounds + origin + Vector2(0, velocityY) )) {
if (velocityY > 0) {
ret = std::min(ret, it.y - (origin.y + bounds.y + bounds.h -1));
ret = std::min(ret, 0.0);
}
else if (velocityY < 0) {
ret = std::max(ret, (it.y + it.h) - (origin.y + bounds.y));
ret = std::max(ret, 0.0);
}
}
}
return ret;
}
+10 -2
View File
@@ -23,14 +23,22 @@
#define LOCALCHARACTER_HPP_
#include "base_character.hpp"
#include "bounding_box.hpp"
#include "vector2.hpp"
#include <list>
class LocalCharacter: public BaseCharacter {
public:
LocalCharacter() = default;
virtual ~LocalCharacter() = default;
private:
//NOTE: NO MEMBERS
bool ProcessCollisions(std::list<BoundingBox>& boxList);
protected:
bool CheckCollisionSimple(std::list<BoundingBox>& boxList, Vector2 newPos);
double CorrectVelocityX(std::list<BoundingBox>& boxList, double velocityX);
double CorrectVelocityY(std::list<BoundingBox>& boxList, double velocityY);
};
#endif
+2 -2
View File
@@ -154,7 +154,7 @@ void InWorld::Update() {
//update all entities
for (auto& it : characterMap) {
it.second.Update();
// it.second.Update();
}
for (auto& it : monsterMap) {
it.second.Update();
@@ -196,7 +196,7 @@ void InWorld::Update() {
}
//process the collisions
std::cout << "boxList.size(): " << boxList.size() << std::endl;
localCharacter->ProcessCollisions(boxList);
//update the camera
camera.x = localCharacter->GetOrigin().x - camera.marginX;
+4
View File
@@ -92,6 +92,10 @@ public:
return ret;
}
//unary operators
Vector2 operator-() { return {-x, -y}; }
//comparison operators
bool operator==(Vector2 v) { return (x == v.x && y == v.y); }
bool operator!=(Vector2 v) { return (x != v.x || y != v.y); }