Trying to implement smooth collisions without other issues
This commit is contained in:
@@ -21,3 +21,42 @@
|
||||
*/
|
||||
#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_
|
||||
|
||||
#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
|
||||
void 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
|
||||
@@ -156,6 +156,10 @@ void InWorld::Update() {
|
||||
|
||||
//update all entities
|
||||
for (auto& it : characterMap) {
|
||||
//skip this player's character
|
||||
if (it.first == characterIndex) {
|
||||
continue;
|
||||
}
|
||||
it.second.Update();
|
||||
}
|
||||
for (auto& it : monsterMap) {
|
||||
@@ -198,7 +202,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;
|
||||
|
||||
Reference in New Issue
Block a user