Skip to content

Commit e30e023

Browse files
committed
Core/Objects: Fix MovePositionToFirstCollision for non-flying units that aren't close to ground
1 parent 05e43d7 commit e30e023

1 file changed

Lines changed: 24 additions & 26 deletions

File tree

src/server/game/Entities/Object/Object.cpp

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3323,40 +3323,41 @@ void WorldObject::MovePosition(Position &pos, float dist, float angle)
33233323
void WorldObject::MovePositionToFirstCollision(Position &pos, float dist, float angle)
33243324
{
33253325
angle += GetOrientation();
3326+
float cosAngle = std::cos(angle);
3327+
float sinAngle = std::sin(angle);
33263328
float destx, desty, destz;
3327-
destx = pos.m_positionX + dist * std::cos(angle);
3328-
desty = pos.m_positionY + dist * std::sin(angle);
3329+
destx = pos.m_positionX + dist * cosAngle;
3330+
desty = pos.m_positionY + dist * sinAngle;
33293331
destz = pos.m_positionZ;
33303332

33313333
// Prevent invalid coordinates here, position is unchanged
33323334
if (!Trinity::IsValidMapCoord(destx, desty))
33333335
{
3334-
TC_LOG_FATAL("misc", "WorldObject::MovePositionToFirstCollision invalid coordinates X: {} and Y: {} were passed!", destx, desty);
3336+
TC_LOG_FATAL("misc", "WorldObject::MovePositionToFirstCollision invalid coordinates Src: {}, dist {}, angle {}, destX: {} destY: {} were passed!",
3337+
pos.ToString(), dist, angle, destx, desty);
33353338
return;
33363339
}
33373340

3341+
float halfHeight = GetCollisionHeight() * 0.5f;
3342+
bool col = false;
3343+
33383344
// Use a detour raycast to get our first collision point
33393345
PathGenerator path(this);
33403346
path.SetUseRaycast(true);
33413347
path.CalculatePath(destx, desty, destz, false);
33423348

33433349
// Check for valid path types before we proceed
3344-
if (!(path.GetPathType() & PATHFIND_NOT_USING_PATH))
3345-
if (path.GetPathType() & ~(PATHFIND_NORMAL | PATHFIND_SHORTCUT | PATHFIND_INCOMPLETE | PATHFIND_FARFROMPOLY_END))
3346-
return;
3347-
3348-
G3D::Vector3 result = path.GetPath().back();
3349-
destx = result.x;
3350-
desty = result.y;
3351-
destz = result.z;
3352-
3353-
// check static LOS
3354-
float halfHeight = GetCollisionHeight() * 0.5f;
3355-
bool col = false;
3356-
3357-
// Unit is flying, check for potential collision via vmaps
3358-
if (path.GetPathType() & PATHFIND_NOT_USING_PATH)
3350+
if (!(path.GetPathType() & (PATHFIND_NOPATH | PATHFIND_NOT_USING_PATH | PATHFIND_FARFROMPOLY_START)))
33593351
{
3352+
G3D::Vector3 const& result = path.GetPath().back();
3353+
destx = result.x;
3354+
desty = result.y;
3355+
destz = result.z;
3356+
}
3357+
else
3358+
{
3359+
// check static LOS
3360+
// Unit is flying, check for potential collision via vmaps
33603361
col = VMAP::VMapFactory::createOrGetVMapManager()->getObjectHitPos(GetMapId(),
33613362
pos.m_positionX, pos.m_positionY, pos.m_positionZ + halfHeight,
33623363
destx, desty, destz + halfHeight,
@@ -3367,9 +3368,8 @@ void WorldObject::MovePositionToFirstCollision(Position &pos, float dist, float
33673368
// Collided with static LOS object, move back to collision point
33683369
if (col)
33693370
{
3370-
destx -= CONTACT_DISTANCE * std::cos(angle);
3371-
desty -= CONTACT_DISTANCE * std::sin(angle);
3372-
dist = std::sqrt((pos.m_positionX - destx) * (pos.m_positionX - destx) + (pos.m_positionY - desty) * (pos.m_positionY - desty));
3371+
destx -= CONTACT_DISTANCE * cosAngle;
3372+
desty -= CONTACT_DISTANCE * sinAngle;
33733373
}
33743374
}
33753375

@@ -3384,18 +3384,16 @@ void WorldObject::MovePositionToFirstCollision(Position &pos, float dist, float
33843384
// Collided with a gameobject, move back to collision point
33853385
if (col)
33863386
{
3387-
destx -= CONTACT_DISTANCE * std::cos(angle);
3388-
desty -= CONTACT_DISTANCE * std::sin(angle);
3389-
dist = std::sqrt((pos.m_positionX - destx)*(pos.m_positionX - destx) + (pos.m_positionY - desty) * (pos.m_positionY - desty));
3387+
destx -= CONTACT_DISTANCE * cosAngle;
3388+
desty -= CONTACT_DISTANCE * sinAngle;
33903389
}
33913390

33923391
float groundZ = VMAP_INVALID_HEIGHT_VALUE;
33933392
Trinity::NormalizeMapCoord(pos.m_positionX);
33943393
Trinity::NormalizeMapCoord(pos.m_positionY);
33953394
UpdateAllowedPositionZ(destx, desty, destz, &groundZ);
33963395

3397-
pos.SetOrientation(GetOrientation());
3398-
pos.Relocate(destx, desty, destz);
3396+
pos.Relocate(destx, desty, destz, GetOrientation());
33993397

34003398
// position has no ground under it (or is too far away)
34013399
if (groundZ <= INVALID_HEIGHT)

0 commit comments

Comments
 (0)