This commit is contained in:
aozhiwei 2023-02-02 14:05:43 +08:00
parent ea813bf43c
commit eeb378adb3
5 changed files with 21 additions and 195 deletions

View File

@ -534,3 +534,5 @@ const int CHECK_SHOT_HOLD_STATE_TIMER_EVENT = a8::TIMER_USER_EVENT + 1;
const int kReviveTimeAdd = 12;
const int kSkinNum = 4;
const int kMoveStep = 80;

View File

@ -2360,7 +2360,7 @@ void Creature::UpdateMove()
if (GetMovement()->UpdatePosition()) {
} else {
if (!GetMovement()->IsFindPath()) {
GetMovement()->CalcTargetPos(100);
GetMovement()->CalcTargetPos(kMoveStep);
if (GetMovement()->UpdatePosition()) {
}
}

View File

@ -87,7 +87,7 @@ void MapInstance::Init()
if (current_uniid_ >= FIXED_OBJECT_MAXID) {
A8_ABORT();
}
LoadHeightData();
//LoadHeightData();
{
navmesh_ = dtAllocNavMesh();
FILE *fp = fopen((mt::MetaMgr::Instance()->GetResDir() + "map4.bin").c_str(), "rb");
@ -351,50 +351,14 @@ bool MapInstance::Raycast(int layer, const glm::vec3& start, const glm::vec3& en
epos[1] = end.y;
epos[2] = end.z;
#if 0
spos[0] = 569.797668;
spos[1] = 7.7130332;
spos[2] = 622.297668;
epos[0] = 610.816589;
epos[1] = 7.7130332;
epos[2] = 650.888733;
#endif
const float extents[3] = {2.f, 4.f, 2.f};
const float extents[3] = {2.f/10, 4.f, 2.f/10};
float nearestPt[3];
dtPolyRef startRef = INVALID_NAVMESH_POLYREF;
class MyDtQueryFtiler : public dtQueryFilter {
public:
std::function<bool (bool, const dtPolyRef, const dtMeshTile*, const dtPoly*)> cb;
bool passFilter(const dtPolyRef ref,
const dtMeshTile* tile,
const dtPoly* poly) const override
{
bool ret = dtQueryFilter::passFilter(ref, tile, poly);
if (cb) {
ret = cb(ret, ref, tile, poly);
}
return ret;
}
};
MyDtQueryFtiler filter;
dtQueryFilter filter;
filter.setIncludeFlags(0xffff);
filter.setExcludeFlags(0);
std::map<const dtPolyRef, std::tuple<const dtMeshTile*, const dtPoly*>> poly_hash;
{
filter.cb =
[&poly_hash] (bool ret, const dtPolyRef ref, const dtMeshTile* tile, const dtPoly* poly) -> bool
{
poly_hash[ref] = std::make_tuple(tile, poly);
return ret;
};
}
dtPolyRef startRef = INVALID_NAVMESH_POLYREF;
navmesh_query_->findNearestPoly(spos, extents, &filter, &startRef, nearestPt);
if (!startRef) {
return false;
@ -405,29 +369,6 @@ bool MapInstance::Raycast(int layer, const glm::vec3& start, const glm::vec3& en
memset(hit_normal_, 0, sizeof(hit_normal_));
navmesh_query_->raycast(startRef, spos, epos, &filter, &t, hit_normal_, polys_, &npolys, MAX_POLYS);
#ifdef DEBUG1
{
std::string dbg_data = a8::Format("npolys:%d t:%f ", {npolys, t});
for (int i = 0; i < npolys; ++i) {
auto itr = poly_hash.find(polys_[i]);
if (itr != poly_hash.end()) {
auto tile = std::get<0>(itr->second);
auto poly = std::get<1>(itr->second);
assert(poly->vertCount > 2);
dbg_data += a8::Format("poly%d: vert_count:%d ", {i + 1, poly->vertCount});
for (int ii = 0; ii < poly->vertCount; ++ii) {
const float* va = &tile->verts[poly->verts[ii] * 3];
dbg_data += a8::Format(" v:%d %f,%f,%f ", {ii, va[0], va[1], va[2]});
}
} else {
abort();
}
}
a8::XPrintf("raycast hit dbg_data:%s\n", {dbg_data});
}
#endif
if (t > 1) {
// No Hit
hit_pos_[0] = epos[0];
@ -441,83 +382,18 @@ bool MapInstance::Raycast(int layer, const glm::vec3& start, const glm::vec3& en
//需要处理spos == epos的情况!!!!
// Hit
dtVlerp(hit_pos_, spos, epos, t);
#if 0
a8::XPrintf("pos1:%f,%f,%f pos2:%f,%f,%f\n",
{
spos[0],
spos[1],
spos[2],
hit_pos_[0],
hit_pos_[1],
hit_pos_[2],
});
#endif
if (std::fabs(hit_pos_[0] - spos[0]) < 0.01f &&
std::fabs(hit_pos_[2] - spos[2]) < 0.01f) {
return false;
}
if (npolys > 0) {
glm::vec3 dir(epos[0] - spos[0], epos[1] - spos[1], epos[2] - spos[2]);
GlmHelper::Normalize(dir);
float hit_pos_copy[3];
dtVcopy(hit_pos_copy, hit_pos_);
float h = 0;
bool ok = false;
//Scale(dir);
for (int ii = npolys - 1; ii >= 0; --ii) {
auto ret = navmesh_query_->getPolyHeight(polys_[ii], hit_pos_copy, &h);
if (ret == DT_SUCCESS){
ok = true;
break;
}
}
if (!ok){
for (int i = 0; i < 30; ++i) {
hit_pos_copy[0] -= dir.x;
hit_pos_copy[2] -= dir.z;
for (int ii = npolys - 1; ii >= 0; --ii) {
auto ret = navmesh_query_->getPolyHeight(polys_[ii], hit_pos_copy, &h);
if (ret == DT_SUCCESS){
ok = true;
break;
}
}
if (ok) {
break;
}
}
dtVcopy(hit_pos_copy, hit_pos_);
for (int i = 0; i < 30; ++i) {
hit_pos_copy[0] += dir.x;
hit_pos_copy[2] += dir.z;
for (int ii = npolys - 1; ii >= 0; --ii) {
auto ret = navmesh_query_->getPolyHeight(polys_[ii], hit_pos_copy, &h);
if (ret == DT_SUCCESS){
ok = true;
break;
}
}
if (ok) {
break;
}
}
}
if (!ok) {
return false;
}
assert(ok);
hit_pos_[0] = hit_pos_copy[0];
hit_pos_[1] = h;
hit_pos_[2] = hit_pos_copy[2];
} else {
//abort();
}
hit_result = true;
}
dtPolyRef endRef = INVALID_NAVMESH_POLYREF;
navmesh_query_->findNearestPoly(hit_pos_, extents, &filter, &endRef, nearestPt);
if (!endRef) {
return false;
}
//epos[0] = nearestPt[0];
hit_pos_[1] = nearestPt[1];
//epos[2] = nearestPt[2];
hit_point.x = hit_pos_[0];
hit_point.y = hit_pos_[1];
hit_point.z = hit_pos_[2];

View File

@ -25,72 +25,20 @@ bool Movement::UpdatePosition()
}
MovePathPoint& curr_point = paths_[path_index_];
{
float h = 0.0f;
if (owner_->room->map_instance->GetPosHeight(curr_point.curr_pos, h)) {
#ifdef DEBUG1
if (owner_->IsPlayer()) {
a8::XPrintf("pos:%f,%f,%f h1:%f\n",
{
owner_->GetPos().x,
owner_->GetPos().y,
owner_->GetPos().z,
h
});
}
#endif
curr_point.curr_pos.y = h;
} else {
//abort();
}
curr_point.curr_pos.AddGlmVec3(curr_point.dir * owner_->GetSpeed());
owner_->SetPos(curr_point.curr_pos);
owner_->CheckSpecObject();
if (owner_->room->map_instance->GetPosHeight(owner_->GetPos(), h)) {
#ifdef DEBUG1
if (owner_->IsPlayer()) {
a8::XPrintf("pos:%f,%f,%f h2:%f\n",
{
owner_->GetPos().x,
owner_->GetPos().y,
owner_->GetPos().z,
h
});
}
#endif
auto new_pos = owner_->GetPos();
new_pos.y = h;
owner_->SetPos(new_pos);
} else {
//abort();
}
}
if (owner_->GetPos().Distance2D2(curr_point.src_pos) - curr_point.distance >= 0.0001f) {
curr_point.tar_pos.y = curr_point.curr_pos.y;
owner_->SetPos(curr_point.tar_pos);
float h = 0.0f;
if (owner_->room->map_instance->GetPosHeight(owner_->GetPos(), h)) {
#if 0
a8::XPrintf("pos:%f,%f,%f h3:%f\n",
{
owner_->GetPos().x,
owner_->GetPos().y,
owner_->GetPos().z,
h
});
#endif
auto new_pos = owner_->GetPos();
new_pos.y = h;
owner_->SetPos(new_pos);
} else {
//abort();
}
++path_index_;
if (path_index_ < paths_.size()) {
MovePathPoint& next_point = paths_[path_index_];
glm::vec3 dir = next_point.tar_pos.ToGlmVec3() - owner_->GetPos().ToGlmVec3();
GlmHelper::Normalize(dir);
next_point.dir.x = dir.x;
next_point.dir.y = 0.0f;
next_point.dir.y = dir.y;
next_point.dir.z = dir.z;
}
}
@ -104,9 +52,9 @@ bool Movement::UpdatePosition()
void Movement::CalcTargetPos(float distance)
{
ClearPath();
#ifdef DEBUG
#ifdef DEBUG1
if (owner_->IsPlayer()) {
a8::XPrintf("CalcTaretPos old_size:%d\n", {paths_.size()});
a8::XPrintf("CalcTaretPos old_size:%d distance:%f\n", {paths_.size(), distance});
}
#endif
Position pos = owner_->GetPos();
@ -187,7 +135,7 @@ void Movement::CalcTargetPos(float distance)
glm::vec3 dir = point.tar_pos.ToGlmVec3() - owner_->GetPos().ToGlmVec3();
GlmHelper::Normalize(dir);
point.dir.x = dir.x;
point.dir.y = 0.0f;
point.dir.y = dir.y;
point.dir.z = dir.z;
paths_.push_back(point);

View File

@ -789,7 +789,7 @@ void Player::_CMMove(f8::MsgHdr& hdr, const cs::CMMove& msg)
std::fabs(new_move_dir.y - GetMoveDir().y) > 0.00001f ||
std::fabs(new_move_dir.z - GetMoveDir().z) > 0.00001f) {
SetMoveDir(new_move_dir);
GetMovement()->CalcTargetPos(500);
GetMovement()->CalcTargetPos(kMoveStep);
}
moving = true;
}