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();
room->map_instance->PtInHouse(GetPos().ToGlmVec3(), center);
room->map_instance->Scale(center);
glm::vec3 point;
bool ok = room->map_instance->FindNearestPoint(center, 200, point);
bool ok = room->map_instance->FindNearestPoint(center, 10, point);
if (!ok) {
abort();
}

View File

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