This commit is contained in:
aozhiwei 2023-02-08 15:19:23 +08:00
parent de502ff2f2
commit b4ca1cc60e
2 changed files with 24 additions and 18 deletions

View File

@ -2599,9 +2599,10 @@ void Creature::OnLand()
} }
{ {
glm::vec3 center = GetPos().ToGlmVec3(); glm::vec3 center = GetPos().ToGlmVec3();
room->map_instance->PtInHouse(GetPos().ToGlmVec3(), center);
room->map_instance->Scale(center); room->map_instance->Scale(center);
glm::vec3 point; glm::vec3 point;
bool ok = room->map_instance->FindNearestPoint(center, 200, point); bool ok = room->map_instance->FindNearestPoint(center, 10, point);
if (!ok) { if (!ok) {
abort(); abort();
} }

View File

@ -738,25 +738,30 @@ bool MapInstance::PtInHouse(const glm::vec3& pt, glm::vec3& nearest_pt)
float edged[DT_VERTS_PER_POLYGON]; float edged[DT_VERTS_PER_POLYGON];
float edget[DT_VERTS_PER_POLYGON]; float edget[DT_VERTS_PER_POLYGON];
for (auto& house : houses_) { for (auto& house : houses_) {
float* verts = &house.verts[0]; if (house.verts.empty()) {
if (!house.verts.empty() && dtPointInPolygon(fpt, &house.verts[0], house.verts.size())) { continue;
if (dtDistancePtPolyEdgesSqr(fpt, &house.verts[0], house.verts.size(), edged, edget)) { }
float dmin = edged[0]; float* verts = &(house.verts[0]);
int imin = 0; int nv = house.verts.size() / 3;
for (size_t i = 1; i < house.verts.size(); ++i) { if (!dtPointInPolygon(fpt, verts, nv)) {
if (edged[i] < dmin) { continue;
dmin = edged[i]; }
imin = i; if (dtDistancePtPolyEdgesSqr(fpt, verts, nv, edged, edget)) {
} float dmin = edged[0];
int imin = 0;
for (size_t i = 1; i < nv; ++i) {
if (edged[i] < dmin) {
dmin = edged[i];
imin = i;
} }
const float* va = &verts[imin*3];
const float* vb = &verts[((imin+1)%house.verts.size())*3];
dtVlerp(closest, va, vb, edget[imin]);
nearest_pt.x = closest[0];
nearest_pt.y = closest[1];
nearest_pt.z = closest[2];
return true;
} }
const float* va = &verts[imin*3];
const float* vb = &verts[((imin+1)%nv)*3];
dtVlerp(closest, va, vb, edget[imin]);
nearest_pt.x = closest[0];
nearest_pt.y = closest[1];
nearest_pt.z = closest[2];
return true;
} }
} }
return false; return false;