Trying to implement smooth collisions without other issues
This commit is contained in:
@@ -21,3 +21,42 @@
|
|||||||
*/
|
*/
|
||||||
#include "local_character.hpp"
|
#include "local_character.hpp"
|
||||||
|
|
||||||
|
void 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;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
origin += motion;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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.w - bounds.x - 1));
|
||||||
|
}
|
||||||
|
else if (velocityX < 0) {
|
||||||
|
ret = std::max(ret, (it.x + it.w) - origin.x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
double LocalCharacter::CorrectVelocityY(std::list<BoundingBox>& boxList, double velocityY) {
|
||||||
|
return velocityY;
|
||||||
|
}
|
||||||
|
|||||||
@@ -23,14 +23,22 @@
|
|||||||
#define LOCALCHARACTER_HPP_
|
#define LOCALCHARACTER_HPP_
|
||||||
|
|
||||||
#include "base_character.hpp"
|
#include "base_character.hpp"
|
||||||
|
#include "bounding_box.hpp"
|
||||||
|
#include "vector2.hpp"
|
||||||
|
|
||||||
|
#include <list>
|
||||||
|
|
||||||
class LocalCharacter: public BaseCharacter {
|
class LocalCharacter: public BaseCharacter {
|
||||||
public:
|
public:
|
||||||
LocalCharacter() = default;
|
LocalCharacter() = default;
|
||||||
virtual ~LocalCharacter() = default;
|
virtual ~LocalCharacter() = default;
|
||||||
|
|
||||||
private:
|
void ProcessCollisions(std::list<BoundingBox>& boxList);
|
||||||
//NOTE: NO MEMBERS
|
|
||||||
|
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
|
#endif
|
||||||
@@ -156,6 +156,10 @@ void InWorld::Update() {
|
|||||||
|
|
||||||
//update all entities
|
//update all entities
|
||||||
for (auto& it : characterMap) {
|
for (auto& it : characterMap) {
|
||||||
|
//skip this player's character
|
||||||
|
if (it.first == characterIndex) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
it.second.Update();
|
it.second.Update();
|
||||||
}
|
}
|
||||||
for (auto& it : monsterMap) {
|
for (auto& it : monsterMap) {
|
||||||
@@ -198,7 +202,7 @@ void InWorld::Update() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//process the collisions
|
//process the collisions
|
||||||
std::cout << "boxList.size(): " << boxList.size() << std::endl;
|
localCharacter->ProcessCollisions(boxList);
|
||||||
|
|
||||||
//update the camera
|
//update the camera
|
||||||
camera.x = localCharacter->GetOrigin().x - camera.marginX;
|
camera.x = localCharacter->GetOrigin().x - camera.marginX;
|
||||||
|
|||||||
Reference in New Issue
Block a user