fix: Fix collision detection between creatures

This commit is contained in:
Vsevolod Kremianskii 2020-10-20 19:47:44 +07:00
parent cb155fd1c0
commit 72aec62ece
3 changed files with 31 additions and 1 deletions

View file

@ -38,7 +38,7 @@ struct RaycastProperties {
int flags { 0 };
glm::vec3 origin { 0.0f };
glm::vec3 direction { 0.0f };
SpatialObject *except { nullptr };
const SpatialObject *except { nullptr };
};
struct RaycastResult {

View file

@ -54,6 +54,7 @@ static const float kDrawDebugDistance = 64.0f;
static const float kPartyMemberFollowDistance = 4.0f;
static const float kMaxDistanceToTestCollision = 64.0f;
static const float kElevationTestZ = 1024.0f;
static const float kCreatureObstacleTestZ = 0.1f;
static const char kPartyLeaderTag[] = "party-leader";
static const char kPartyMember1Tag[] = "party-member-1";
@ -281,6 +282,31 @@ bool Area::findCameraObstacle(const glm::vec3 &origin, const glm::vec3 &dest, gl
return false;
}
bool Area::findCreatureObstacle(const Creature &creature, const glm::vec3 &dest) const {
glm::vec3 origin(creature.position());
origin.z += kCreatureObstacleTestZ;
glm::vec3 adjustedDest(dest);
adjustedDest.z += kCreatureObstacleTestZ;
glm::vec3 originToDest(adjustedDest - origin);
glm::vec3 dir(glm::normalize(originToDest));
RaycastProperties props;
props.flags = kRaycastObjects | kRaycastAABB;
props.origin = origin;
props.direction = dir;
props.except = &creature;
RaycastResult result;
if (_collisionDetector.raycast(props, result)) {
return result.distance <= glm::length(originToDest);
}
return false;
}
void Area::add(const shared_ptr<SpatialObject> &object) {
_objects.push_back(object);
_objectsByType[object->type()].push_back(object);
@ -442,6 +468,9 @@ bool Area::moveCreatureTowards(Creature &creature, const glm::vec2 &dest, bool r
Room *room = nullptr;
if (findCreatureObstacle(creature, position)) {
return false;
}
if (getElevationAt(position, room, position.z)) {
creature.setRoom(room);
creature.setPosition(position);

View file

@ -158,6 +158,7 @@ private:
void updateTriggers(const Creature &creature);
bool findCameraObstacle(const glm::vec3 &origin, const glm::vec3 &dest, glm::vec3 &intersection) const;
bool findCreatureObstacle(const Creature &creature, const glm::vec3 &dest) const;
bool getElevationAt(const glm::vec2 &position, Room *&room, float &z) const;
glm::vec3 getSelectableScreenCoords(uint32_t objectId, const UpdateContext &ctx) const;