0ad/source/simulation2/components/CCmpUnitMotion_System.cpp

290 lines
11 KiB
C++
Raw Normal View History

/* Copyright (C) 2021 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
#include "precompiled.h"
#include "CCmpUnitMotion.h"
#include "CCmpUnitMotionManager.h"
Add a simple 'pushing' logic to unit motion to improve movement. This implements a form of crowd movement that I've generally called 'unit pushing' in the last few years. Essentially, any two units will push each other away when they're too close. This makes it possible to ignore unit-unit obstructions, and thus makes movement much smoother in crowds. This first iteration of this system only allows pushing between idle units and between moving units (i.e. a moving unit does not affect an idle one). This is because the unitMotion logic to detect it is stuck & needs to use the pathfinders starts breaking: units can fail to move because they are pushed away from their intended movement, and the current logic fails to handle this gracefully. Thankfully, the most value of this patch in terms of player experience is found in the improvements to group movements and shuttling. Other impacts: - As the short pathfinder is called less often, we can increase the starting search range & reduce the # of max turns, both improving collision recovery. - The performance of idle units is slightly worsened, as they must be checked for idle-idle collisions. If needed a 'sleeping' system, as used in physics engine, could be implemented. - In general, however, expect slight performance improvements, as fewer short paths are computed. - Gathering efficiency should increase slightly, since shuttling times are likely reduced slightly. - As a sanity change to improve some edge cases (units that say they're moving, i.e. pushable, but don't actually move), the 'going straight' logic is turned off if a short path has been computed. This requires a few cascading changes to work correctly. Technical notes: - To reduce the cost of the n^2 comparisons that pushing requires, units are only compared within a small square on a grid which is lazily reconstructed each turn. The overhead seems rather small, and this is much simpler than keeping an up-to-date grid. - The design is intended to be parallelisable if needed someday. - The pathfinder's CheckMovement ignores moving units in UnitMotion, as that is now the spec. Idle units are not ignored, which is required for the 'collision' detection to work correctly (see above). Refs #3442 (not fixed - idle units are not pushed by moving units). Fixes #5084 (the overlap can still happen, but units will push each other away). Differential Revision: https://code.wildfiregames.com/D1490 This was SVN commit r25182.
2021-04-02 09:30:59 -07:00
#include "maths/MathUtil.h"
#include "ps/CLogger.h"
#include "ps/Profile.h"
Add a simple 'pushing' logic to unit motion to improve movement. This implements a form of crowd movement that I've generally called 'unit pushing' in the last few years. Essentially, any two units will push each other away when they're too close. This makes it possible to ignore unit-unit obstructions, and thus makes movement much smoother in crowds. This first iteration of this system only allows pushing between idle units and between moving units (i.e. a moving unit does not affect an idle one). This is because the unitMotion logic to detect it is stuck & needs to use the pathfinders starts breaking: units can fail to move because they are pushed away from their intended movement, and the current logic fails to handle this gracefully. Thankfully, the most value of this patch in terms of player experience is found in the improvements to group movements and shuttling. Other impacts: - As the short pathfinder is called less often, we can increase the starting search range & reduce the # of max turns, both improving collision recovery. - The performance of idle units is slightly worsened, as they must be checked for idle-idle collisions. If needed a 'sleeping' system, as used in physics engine, could be implemented. - In general, however, expect slight performance improvements, as fewer short paths are computed. - Gathering efficiency should increase slightly, since shuttling times are likely reduced slightly. - As a sanity change to improve some edge cases (units that say they're moving, i.e. pushable, but don't actually move), the 'going straight' logic is turned off if a short path has been computed. This requires a few cascading changes to work correctly. Technical notes: - To reduce the cost of the n^2 comparisons that pushing requires, units are only compared within a small square on a grid which is lazily reconstructed each turn. The overhead seems rather small, and this is much simpler than keeping an up-to-date grid. - The design is intended to be parallelisable if needed someday. - The pathfinder's CheckMovement ignores moving units in UnitMotion, as that is now the spec. Idle units are not ignored, which is required for the 'collision' detection to work correctly (see above). Refs #3442 (not fixed - idle units are not pushed by moving units). Fixes #5084 (the overlap can still happen, but units will push each other away). Differential Revision: https://code.wildfiregames.com/D1490 This was SVN commit r25182.
2021-04-02 09:30:59 -07:00
#include <unordered_set>
// NB: this TU contains the CCmpUnitMotion/CCmpUnitMotionManager couple.
// In practice, UnitMotionManager functions need access to the full implementation of UnitMotion,
// but UnitMotion needs access to MotionState (defined in UnitMotionManager).
// To avoid inclusion issues, implementation of UnitMotionManager that uses UnitMotion is here.
Add a simple 'pushing' logic to unit motion to improve movement. This implements a form of crowd movement that I've generally called 'unit pushing' in the last few years. Essentially, any two units will push each other away when they're too close. This makes it possible to ignore unit-unit obstructions, and thus makes movement much smoother in crowds. This first iteration of this system only allows pushing between idle units and between moving units (i.e. a moving unit does not affect an idle one). This is because the unitMotion logic to detect it is stuck & needs to use the pathfinders starts breaking: units can fail to move because they are pushed away from their intended movement, and the current logic fails to handle this gracefully. Thankfully, the most value of this patch in terms of player experience is found in the improvements to group movements and shuttling. Other impacts: - As the short pathfinder is called less often, we can increase the starting search range & reduce the # of max turns, both improving collision recovery. - The performance of idle units is slightly worsened, as they must be checked for idle-idle collisions. If needed a 'sleeping' system, as used in physics engine, could be implemented. - In general, however, expect slight performance improvements, as fewer short paths are computed. - Gathering efficiency should increase slightly, since shuttling times are likely reduced slightly. - As a sanity change to improve some edge cases (units that say they're moving, i.e. pushable, but don't actually move), the 'going straight' logic is turned off if a short path has been computed. This requires a few cascading changes to work correctly. Technical notes: - To reduce the cost of the n^2 comparisons that pushing requires, units are only compared within a small square on a grid which is lazily reconstructed each turn. The overhead seems rather small, and this is much simpler than keeping an up-to-date grid. - The design is intended to be parallelisable if needed someday. - The pathfinder's CheckMovement ignores moving units in UnitMotion, as that is now the spec. Idle units are not ignored, which is required for the 'collision' detection to work correctly (see above). Refs #3442 (not fixed - idle units are not pushed by moving units). Fixes #5084 (the overlap can still happen, but units will push each other away). Differential Revision: https://code.wildfiregames.com/D1490 This was SVN commit r25182.
2021-04-02 09:30:59 -07:00
namespace {
/**
* Units push only within their own grid square. This is the size of each square (in arbitrary units).
* TODO: check other values.
*/
static const int PUSHING_GRID_SIZE = 20;
/**
* Pushing is ignored if the combined push force has lower magnitude than this.
*/
static const entity_pos_t MINIMAL_PUSHING = entity_pos_t::FromInt(1)/10;
/**
* When moving, units extert a pushing influence at a greater distance.
*/
static const entity_pos_t PUSHING_MOVING_INFLUENCE_EXTENSION = entity_pos_t::FromInt(1);
/**
* Arbitrary constant used to reduce pushing to levels that won't break physics for our turn length.
*/
static const int PUSHING_REDUCTION_FACTOR = 2;
}
CCmpUnitMotionManager::MotionState::MotionState(CmpPtr<ICmpPosition> cmpPos, CCmpUnitMotion* cmpMotion)
: cmpPosition(cmpPos), cmpUnitMotion(cmpMotion)
{
}
void CCmpUnitMotionManager::Register(CCmpUnitMotion* component, entity_id_t ent, bool formationController)
{
Add a simple 'pushing' logic to unit motion to improve movement. This implements a form of crowd movement that I've generally called 'unit pushing' in the last few years. Essentially, any two units will push each other away when they're too close. This makes it possible to ignore unit-unit obstructions, and thus makes movement much smoother in crowds. This first iteration of this system only allows pushing between idle units and between moving units (i.e. a moving unit does not affect an idle one). This is because the unitMotion logic to detect it is stuck & needs to use the pathfinders starts breaking: units can fail to move because they are pushed away from their intended movement, and the current logic fails to handle this gracefully. Thankfully, the most value of this patch in terms of player experience is found in the improvements to group movements and shuttling. Other impacts: - As the short pathfinder is called less often, we can increase the starting search range & reduce the # of max turns, both improving collision recovery. - The performance of idle units is slightly worsened, as they must be checked for idle-idle collisions. If needed a 'sleeping' system, as used in physics engine, could be implemented. - In general, however, expect slight performance improvements, as fewer short paths are computed. - Gathering efficiency should increase slightly, since shuttling times are likely reduced slightly. - As a sanity change to improve some edge cases (units that say they're moving, i.e. pushable, but don't actually move), the 'going straight' logic is turned off if a short path has been computed. This requires a few cascading changes to work correctly. Technical notes: - To reduce the cost of the n^2 comparisons that pushing requires, units are only compared within a small square on a grid which is lazily reconstructed each turn. The overhead seems rather small, and this is much simpler than keeping an up-to-date grid. - The design is intended to be parallelisable if needed someday. - The pathfinder's CheckMovement ignores moving units in UnitMotion, as that is now the spec. Idle units are not ignored, which is required for the 'collision' detection to work correctly (see above). Refs #3442 (not fixed - idle units are not pushed by moving units). Fixes #5084 (the overlap can still happen, but units will push each other away). Differential Revision: https://code.wildfiregames.com/D1490 This was SVN commit r25182.
2021-04-02 09:30:59 -07:00
MotionState state(CmpPtr<ICmpPosition>(GetSimContext(), ent), component);
if (!formationController)
m_Units.insert(ent, state);
else
m_FormationControllers.insert(ent, state);
}
void CCmpUnitMotionManager::Unregister(entity_id_t ent)
{
EntityMap<MotionState>::iterator it = m_Units.find(ent);
if (it != m_Units.end())
{
m_Units.erase(it);
return;
}
it = m_FormationControllers.find(ent);
if (it != m_FormationControllers.end())
m_FormationControllers.erase(it);
}
void CCmpUnitMotionManager::OnTurnStart()
{
for (EntityMap<MotionState>::value_type& data : m_FormationControllers)
data.second.cmpUnitMotion->OnTurnStart();
for (EntityMap<MotionState>::value_type& data : m_Units)
data.second.cmpUnitMotion->OnTurnStart();
}
void CCmpUnitMotionManager::MoveUnits(fixed dt)
{
Move(m_Units, dt);
}
void CCmpUnitMotionManager::MoveFormations(fixed dt)
{
Move(m_FormationControllers, dt);
}
void CCmpUnitMotionManager::Move(EntityMap<MotionState>& ents, fixed dt)
{
Add a simple 'pushing' logic to unit motion to improve movement. This implements a form of crowd movement that I've generally called 'unit pushing' in the last few years. Essentially, any two units will push each other away when they're too close. This makes it possible to ignore unit-unit obstructions, and thus makes movement much smoother in crowds. This first iteration of this system only allows pushing between idle units and between moving units (i.e. a moving unit does not affect an idle one). This is because the unitMotion logic to detect it is stuck & needs to use the pathfinders starts breaking: units can fail to move because they are pushed away from their intended movement, and the current logic fails to handle this gracefully. Thankfully, the most value of this patch in terms of player experience is found in the improvements to group movements and shuttling. Other impacts: - As the short pathfinder is called less often, we can increase the starting search range & reduce the # of max turns, both improving collision recovery. - The performance of idle units is slightly worsened, as they must be checked for idle-idle collisions. If needed a 'sleeping' system, as used in physics engine, could be implemented. - In general, however, expect slight performance improvements, as fewer short paths are computed. - Gathering efficiency should increase slightly, since shuttling times are likely reduced slightly. - As a sanity change to improve some edge cases (units that say they're moving, i.e. pushable, but don't actually move), the 'going straight' logic is turned off if a short path has been computed. This requires a few cascading changes to work correctly. Technical notes: - To reduce the cost of the n^2 comparisons that pushing requires, units are only compared within a small square on a grid which is lazily reconstructed each turn. The overhead seems rather small, and this is much simpler than keeping an up-to-date grid. - The design is intended to be parallelisable if needed someday. - The pathfinder's CheckMovement ignores moving units in UnitMotion, as that is now the spec. Idle units are not ignored, which is required for the 'collision' detection to work correctly (see above). Refs #3442 (not fixed - idle units are not pushed by moving units). Fixes #5084 (the overlap can still happen, but units will push each other away). Differential Revision: https://code.wildfiregames.com/D1490 This was SVN commit r25182.
2021-04-02 09:30:59 -07:00
PROFILE2("MotionMgr_Move");
std::unordered_set<std::vector<EntityMap<MotionState>::iterator>*> assigned;
for (EntityMap<MotionState>::iterator it = ents.begin(); it != ents.end(); ++it)
{
Add a simple 'pushing' logic to unit motion to improve movement. This implements a form of crowd movement that I've generally called 'unit pushing' in the last few years. Essentially, any two units will push each other away when they're too close. This makes it possible to ignore unit-unit obstructions, and thus makes movement much smoother in crowds. This first iteration of this system only allows pushing between idle units and between moving units (i.e. a moving unit does not affect an idle one). This is because the unitMotion logic to detect it is stuck & needs to use the pathfinders starts breaking: units can fail to move because they are pushed away from their intended movement, and the current logic fails to handle this gracefully. Thankfully, the most value of this patch in terms of player experience is found in the improvements to group movements and shuttling. Other impacts: - As the short pathfinder is called less often, we can increase the starting search range & reduce the # of max turns, both improving collision recovery. - The performance of idle units is slightly worsened, as they must be checked for idle-idle collisions. If needed a 'sleeping' system, as used in physics engine, could be implemented. - In general, however, expect slight performance improvements, as fewer short paths are computed. - Gathering efficiency should increase slightly, since shuttling times are likely reduced slightly. - As a sanity change to improve some edge cases (units that say they're moving, i.e. pushable, but don't actually move), the 'going straight' logic is turned off if a short path has been computed. This requires a few cascading changes to work correctly. Technical notes: - To reduce the cost of the n^2 comparisons that pushing requires, units are only compared within a small square on a grid which is lazily reconstructed each turn. The overhead seems rather small, and this is much simpler than keeping an up-to-date grid. - The design is intended to be parallelisable if needed someday. - The pathfinder's CheckMovement ignores moving units in UnitMotion, as that is now the spec. Idle units are not ignored, which is required for the 'collision' detection to work correctly (see above). Refs #3442 (not fixed - idle units are not pushed by moving units). Fixes #5084 (the overlap can still happen, but units will push each other away). Differential Revision: https://code.wildfiregames.com/D1490 This was SVN commit r25182.
2021-04-02 09:30:59 -07:00
if (!it->second.cmpPosition->IsInWorld())
{
it->second.needUpdate = false;
continue;
Add a simple 'pushing' logic to unit motion to improve movement. This implements a form of crowd movement that I've generally called 'unit pushing' in the last few years. Essentially, any two units will push each other away when they're too close. This makes it possible to ignore unit-unit obstructions, and thus makes movement much smoother in crowds. This first iteration of this system only allows pushing between idle units and between moving units (i.e. a moving unit does not affect an idle one). This is because the unitMotion logic to detect it is stuck & needs to use the pathfinders starts breaking: units can fail to move because they are pushed away from their intended movement, and the current logic fails to handle this gracefully. Thankfully, the most value of this patch in terms of player experience is found in the improvements to group movements and shuttling. Other impacts: - As the short pathfinder is called less often, we can increase the starting search range & reduce the # of max turns, both improving collision recovery. - The performance of idle units is slightly worsened, as they must be checked for idle-idle collisions. If needed a 'sleeping' system, as used in physics engine, could be implemented. - In general, however, expect slight performance improvements, as fewer short paths are computed. - Gathering efficiency should increase slightly, since shuttling times are likely reduced slightly. - As a sanity change to improve some edge cases (units that say they're moving, i.e. pushable, but don't actually move), the 'going straight' logic is turned off if a short path has been computed. This requires a few cascading changes to work correctly. Technical notes: - To reduce the cost of the n^2 comparisons that pushing requires, units are only compared within a small square on a grid which is lazily reconstructed each turn. The overhead seems rather small, and this is much simpler than keeping an up-to-date grid. - The design is intended to be parallelisable if needed someday. - The pathfinder's CheckMovement ignores moving units in UnitMotion, as that is now the spec. Idle units are not ignored, which is required for the 'collision' detection to work correctly (see above). Refs #3442 (not fixed - idle units are not pushed by moving units). Fixes #5084 (the overlap can still happen, but units will push each other away). Differential Revision: https://code.wildfiregames.com/D1490 This was SVN commit r25182.
2021-04-02 09:30:59 -07:00
}
else
it->second.cmpUnitMotion->PreMove(it->second);
it->second.initialPos = it->second.cmpPosition->GetPosition2D();
it->second.initialAngle = it->second.cmpPosition->GetRotation().Y;
it->second.pos = it->second.initialPos;
it->second.angle = it->second.initialAngle;
Add a simple 'pushing' logic to unit motion to improve movement. This implements a form of crowd movement that I've generally called 'unit pushing' in the last few years. Essentially, any two units will push each other away when they're too close. This makes it possible to ignore unit-unit obstructions, and thus makes movement much smoother in crowds. This first iteration of this system only allows pushing between idle units and between moving units (i.e. a moving unit does not affect an idle one). This is because the unitMotion logic to detect it is stuck & needs to use the pathfinders starts breaking: units can fail to move because they are pushed away from their intended movement, and the current logic fails to handle this gracefully. Thankfully, the most value of this patch in terms of player experience is found in the improvements to group movements and shuttling. Other impacts: - As the short pathfinder is called less often, we can increase the starting search range & reduce the # of max turns, both improving collision recovery. - The performance of idle units is slightly worsened, as they must be checked for idle-idle collisions. If needed a 'sleeping' system, as used in physics engine, could be implemented. - In general, however, expect slight performance improvements, as fewer short paths are computed. - Gathering efficiency should increase slightly, since shuttling times are likely reduced slightly. - As a sanity change to improve some edge cases (units that say they're moving, i.e. pushable, but don't actually move), the 'going straight' logic is turned off if a short path has been computed. This requires a few cascading changes to work correctly. Technical notes: - To reduce the cost of the n^2 comparisons that pushing requires, units are only compared within a small square on a grid which is lazily reconstructed each turn. The overhead seems rather small, and this is much simpler than keeping an up-to-date grid. - The design is intended to be parallelisable if needed someday. - The pathfinder's CheckMovement ignores moving units in UnitMotion, as that is now the spec. Idle units are not ignored, which is required for the 'collision' detection to work correctly (see above). Refs #3442 (not fixed - idle units are not pushed by moving units). Fixes #5084 (the overlap can still happen, but units will push each other away). Differential Revision: https://code.wildfiregames.com/D1490 This was SVN commit r25182.
2021-04-02 09:30:59 -07:00
ENSURE(it->second.pos.X.ToInt_RoundToZero() / PUSHING_GRID_SIZE < m_MovingUnits.width() &&
it->second.pos.Y.ToInt_RoundToZero() / PUSHING_GRID_SIZE < m_MovingUnits.height());
std::vector<EntityMap<MotionState>::iterator>& subdiv = m_MovingUnits.get(
it->second.pos.X.ToInt_RoundToZero() / PUSHING_GRID_SIZE,
it->second.pos.Y.ToInt_RoundToZero() / PUSHING_GRID_SIZE
);
subdiv.emplace_back(it);
assigned.emplace(&subdiv);
}
for (std::vector<EntityMap<MotionState>::iterator>* vec : assigned)
for (EntityMap<MotionState>::iterator& it : *vec)
if (it->second.needUpdate)
it->second.cmpUnitMotion->Move(it->second, dt);
if (&ents == &m_Units)
{
PROFILE2("MotionMgr_Pushing");
for (std::vector<EntityMap<MotionState>::iterator>* vec : assigned)
{
ENSURE(!vec->empty());
std::vector<EntityMap<MotionState>::iterator>::iterator cit1 = vec->begin();
do
{
if ((*cit1)->second.ignore)
continue;
std::vector<EntityMap<MotionState>::iterator>::iterator cit2 = cit1;
while(++cit2 != vec->end())
if (!(*cit2)->second.ignore)
Push(**cit1, **cit2, dt);
}
while(++cit1 != vec->end());
}
}
{
PROFILE2("MotionMgr_PushAdjust");
CmpPtr<ICmpPathfinder> cmpPathfinder(GetSystemEntity());
for (std::vector<EntityMap<MotionState>::iterator>* vec : assigned)
{
for (EntityMap<MotionState>::iterator& it : *vec)
{
if (!it->second.needUpdate || it->second.ignore)
continue;
// Prevent pushed units from crossing uncrossable boundaries
// (we can assume that normal movement didn't push units into impassable terrain).
if ((it->second.push.X != entity_pos_t::Zero() || it->second.push.Y != entity_pos_t::Zero()) &&
!cmpPathfinder->CheckMovement(it->second.cmpUnitMotion->GetObstructionFilter(),
it->second.pos.X, it->second.pos.Y,
it->second.pos.X + it->second.push.X, it->second.pos.Y + it->second.push.Y,
it->second.cmpUnitMotion->m_Clearance,
it->second.cmpUnitMotion->m_PassClass))
{
// Mark them as obstructed - this could possibly be optimised
// perhaps it'd make more sense to mark the pushers as blocked.
it->second.wasObstructed = true;
it->second.wentStraight = false;
it->second.push = CFixedVector2D();
}
// Only apply pushing if the effect is significant enough.
if (it->second.push.CompareLength(MINIMAL_PUSHING) > 0)
{
// If there was an attempt at movement, and the pushed movement is in a sufficiently different direction
// (measured by an extremely arbitrary dot product)
// then mark the unit as obstructed still.
if (it->second.pos != it->second.initialPos &&
(it->second.pos - it->second.initialPos).Dot(it->second.pos + it->second.push - it->second.initialPos) < entity_pos_t::FromInt(1)/2)
{
it->second.wasObstructed = true;
it->second.wentStraight = false;
// Push anyways.
}
it->second.pos += it->second.push;
}
it->second.push = CFixedVector2D();
}
}
}
{
PROFILE2("MotionMgr_PostMove");
for (EntityMap<MotionState>::value_type& data : ents)
{
if (!data.second.needUpdate)
continue;
data.second.cmpUnitMotion->PostMove(data.second, dt);
}
}
Add a simple 'pushing' logic to unit motion to improve movement. This implements a form of crowd movement that I've generally called 'unit pushing' in the last few years. Essentially, any two units will push each other away when they're too close. This makes it possible to ignore unit-unit obstructions, and thus makes movement much smoother in crowds. This first iteration of this system only allows pushing between idle units and between moving units (i.e. a moving unit does not affect an idle one). This is because the unitMotion logic to detect it is stuck & needs to use the pathfinders starts breaking: units can fail to move because they are pushed away from their intended movement, and the current logic fails to handle this gracefully. Thankfully, the most value of this patch in terms of player experience is found in the improvements to group movements and shuttling. Other impacts: - As the short pathfinder is called less often, we can increase the starting search range & reduce the # of max turns, both improving collision recovery. - The performance of idle units is slightly worsened, as they must be checked for idle-idle collisions. If needed a 'sleeping' system, as used in physics engine, could be implemented. - In general, however, expect slight performance improvements, as fewer short paths are computed. - Gathering efficiency should increase slightly, since shuttling times are likely reduced slightly. - As a sanity change to improve some edge cases (units that say they're moving, i.e. pushable, but don't actually move), the 'going straight' logic is turned off if a short path has been computed. This requires a few cascading changes to work correctly. Technical notes: - To reduce the cost of the n^2 comparisons that pushing requires, units are only compared within a small square on a grid which is lazily reconstructed each turn. The overhead seems rather small, and this is much simpler than keeping an up-to-date grid. - The design is intended to be parallelisable if needed someday. - The pathfinder's CheckMovement ignores moving units in UnitMotion, as that is now the spec. Idle units are not ignored, which is required for the 'collision' detection to work correctly (see above). Refs #3442 (not fixed - idle units are not pushed by moving units). Fixes #5084 (the overlap can still happen, but units will push each other away). Differential Revision: https://code.wildfiregames.com/D1490 This was SVN commit r25182.
2021-04-02 09:30:59 -07:00
for (std::vector<EntityMap<MotionState>::iterator>* vec : assigned)
vec->clear();
}
// TODO: ought to better simulate in-flight pushing, e.g. if units would cross in-between turns.
void CCmpUnitMotionManager::Push(EntityMap<MotionState>::value_type& a, EntityMap<MotionState>::value_type& b, fixed dt)
{
// The hard problem for pushing is knowing when to actually use the pathfinder to go around unpushable obstacles.
// For simplicitly, the current logic separates moving & stopped entities:
// moving entities will push moving entities, but not stopped ones, and vice-versa.
// this still delivers most of the value of pushing, without a lot of the complexity.
int movingPush = a.second.isMoving + b.second.isMoving;
// Exception: units in the same control group (i.e. the same formation) never push farther than themselves
// and are also allowed to push idle units (obstructions are ignored within formations,
// so pushing idle units makes one member crossing the formation look better).
if (a.second.controlGroup != INVALID_ENTITY && a.second.controlGroup == b.second.controlGroup)
movingPush = 0;
if (movingPush == 1)
return;
// Treat the clearances as a circle - they're defined as squares, so we'll slightly overcompensate the diagonal
// (they're also full-width instead of half, so we want to divide by two. sqrt(2)/2 is about 0.71 < 5/7).
entity_pos_t combinedClearance = (a.second.cmpUnitMotion->m_Clearance + b.second.cmpUnitMotion->m_Clearance) * 5 / 7;
entity_pos_t maxDist = combinedClearance;
if (movingPush)
maxDist += PUSHING_MOVING_INFLUENCE_EXTENSION;
CFixedVector2D offset = a.second.pos - b.second.pos;
if (offset.CompareLength(maxDist) > 0)
return;
Add a simple 'pushing' logic to unit motion to improve movement. This implements a form of crowd movement that I've generally called 'unit pushing' in the last few years. Essentially, any two units will push each other away when they're too close. This makes it possible to ignore unit-unit obstructions, and thus makes movement much smoother in crowds. This first iteration of this system only allows pushing between idle units and between moving units (i.e. a moving unit does not affect an idle one). This is because the unitMotion logic to detect it is stuck & needs to use the pathfinders starts breaking: units can fail to move because they are pushed away from their intended movement, and the current logic fails to handle this gracefully. Thankfully, the most value of this patch in terms of player experience is found in the improvements to group movements and shuttling. Other impacts: - As the short pathfinder is called less often, we can increase the starting search range & reduce the # of max turns, both improving collision recovery. - The performance of idle units is slightly worsened, as they must be checked for idle-idle collisions. If needed a 'sleeping' system, as used in physics engine, could be implemented. - In general, however, expect slight performance improvements, as fewer short paths are computed. - Gathering efficiency should increase slightly, since shuttling times are likely reduced slightly. - As a sanity change to improve some edge cases (units that say they're moving, i.e. pushable, but don't actually move), the 'going straight' logic is turned off if a short path has been computed. This requires a few cascading changes to work correctly. Technical notes: - To reduce the cost of the n^2 comparisons that pushing requires, units are only compared within a small square on a grid which is lazily reconstructed each turn. The overhead seems rather small, and this is much simpler than keeping an up-to-date grid. - The design is intended to be parallelisable if needed someday. - The pathfinder's CheckMovement ignores moving units in UnitMotion, as that is now the spec. Idle units are not ignored, which is required for the 'collision' detection to work correctly (see above). Refs #3442 (not fixed - idle units are not pushed by moving units). Fixes #5084 (the overlap can still happen, but units will push each other away). Differential Revision: https://code.wildfiregames.com/D1490 This was SVN commit r25182.
2021-04-02 09:30:59 -07:00
entity_pos_t offsetLength = offset.Length();
// If the offset is small enough that precision would be problematic, pick an arbitrary vector instead.
if (offsetLength <= entity_pos_t::Epsilon() * 10)
{
Add a simple 'pushing' logic to unit motion to improve movement. This implements a form of crowd movement that I've generally called 'unit pushing' in the last few years. Essentially, any two units will push each other away when they're too close. This makes it possible to ignore unit-unit obstructions, and thus makes movement much smoother in crowds. This first iteration of this system only allows pushing between idle units and between moving units (i.e. a moving unit does not affect an idle one). This is because the unitMotion logic to detect it is stuck & needs to use the pathfinders starts breaking: units can fail to move because they are pushed away from their intended movement, and the current logic fails to handle this gracefully. Thankfully, the most value of this patch in terms of player experience is found in the improvements to group movements and shuttling. Other impacts: - As the short pathfinder is called less often, we can increase the starting search range & reduce the # of max turns, both improving collision recovery. - The performance of idle units is slightly worsened, as they must be checked for idle-idle collisions. If needed a 'sleeping' system, as used in physics engine, could be implemented. - In general, however, expect slight performance improvements, as fewer short paths are computed. - Gathering efficiency should increase slightly, since shuttling times are likely reduced slightly. - As a sanity change to improve some edge cases (units that say they're moving, i.e. pushable, but don't actually move), the 'going straight' logic is turned off if a short path has been computed. This requires a few cascading changes to work correctly. Technical notes: - To reduce the cost of the n^2 comparisons that pushing requires, units are only compared within a small square on a grid which is lazily reconstructed each turn. The overhead seems rather small, and this is much simpler than keeping an up-to-date grid. - The design is intended to be parallelisable if needed someday. - The pathfinder's CheckMovement ignores moving units in UnitMotion, as that is now the spec. Idle units are not ignored, which is required for the 'collision' detection to work correctly (see above). Refs #3442 (not fixed - idle units are not pushed by moving units). Fixes #5084 (the overlap can still happen, but units will push each other away). Differential Revision: https://code.wildfiregames.com/D1490 This was SVN commit r25182.
2021-04-02 09:30:59 -07:00
// Throw in some 'randomness' so that clumped units unclump more naturally.
bool dir = a.first % 2;
offset.X = entity_pos_t::FromInt(dir ? 1 : 0);
offset.Y = entity_pos_t::FromInt(dir ? 0 : 1);
offsetLength = entity_pos_t::FromInt(1);
}
Add a simple 'pushing' logic to unit motion to improve movement. This implements a form of crowd movement that I've generally called 'unit pushing' in the last few years. Essentially, any two units will push each other away when they're too close. This makes it possible to ignore unit-unit obstructions, and thus makes movement much smoother in crowds. This first iteration of this system only allows pushing between idle units and between moving units (i.e. a moving unit does not affect an idle one). This is because the unitMotion logic to detect it is stuck & needs to use the pathfinders starts breaking: units can fail to move because they are pushed away from their intended movement, and the current logic fails to handle this gracefully. Thankfully, the most value of this patch in terms of player experience is found in the improvements to group movements and shuttling. Other impacts: - As the short pathfinder is called less often, we can increase the starting search range & reduce the # of max turns, both improving collision recovery. - The performance of idle units is slightly worsened, as they must be checked for idle-idle collisions. If needed a 'sleeping' system, as used in physics engine, could be implemented. - In general, however, expect slight performance improvements, as fewer short paths are computed. - Gathering efficiency should increase slightly, since shuttling times are likely reduced slightly. - As a sanity change to improve some edge cases (units that say they're moving, i.e. pushable, but don't actually move), the 'going straight' logic is turned off if a short path has been computed. This requires a few cascading changes to work correctly. Technical notes: - To reduce the cost of the n^2 comparisons that pushing requires, units are only compared within a small square on a grid which is lazily reconstructed each turn. The overhead seems rather small, and this is much simpler than keeping an up-to-date grid. - The design is intended to be parallelisable if needed someday. - The pathfinder's CheckMovement ignores moving units in UnitMotion, as that is now the spec. Idle units are not ignored, which is required for the 'collision' detection to work correctly (see above). Refs #3442 (not fixed - idle units are not pushed by moving units). Fixes #5084 (the overlap can still happen, but units will push each other away). Differential Revision: https://code.wildfiregames.com/D1490 This was SVN commit r25182.
2021-04-02 09:30:59 -07:00
else
{
offset.X = offset.X / offsetLength;
offset.Y = offset.Y / offsetLength;
}
// If the units are moving in opposite direction, check if they might have phased through each other.
// If it looks like yes, move them perpendicularily so it looks like they avoid each other.
// NB: this isn't very precise, nor will it catch 100% of intersections - it's meant as a cheap improvement.
if (movingPush && (a.second.pos - a.second.initialPos).Dot(b.second.pos - b.second.initialPos) < entity_pos_t::Zero())
// Perform some finer checking.
if (Geometry::TestRayAASquare(a.second.initialPos - b.second.initialPos, a.second.pos - b.second.initialPos,
CFixedVector2D(combinedClearance, combinedClearance))
||
Geometry::TestRayAASquare(a.second.initialPos - b.second.pos, a.second.pos - b.second.pos,
CFixedVector2D(combinedClearance, combinedClearance)))
{
offset = offset.Perpendicular();
offsetLength = fixed::Zero();
}
// The formula expects 'normal' pushing if the two entities edges are touching.
entity_pos_t distanceFactor = movingPush ? (maxDist - offsetLength) / (maxDist - combinedClearance) : combinedClearance - offsetLength + entity_pos_t::FromInt(1);
distanceFactor = Clamp(distanceFactor, entity_pos_t::Zero(), entity_pos_t::FromInt(2));
// Mark both as needing an update so they actually get moved.
a.second.needUpdate = true;
b.second.needUpdate = true;
CFixedVector2D pushingDir = offset.Multiply(distanceFactor);
// Divide by an arbitrary constant to avoid pushing too much.
a.second.push += pushingDir.Multiply(movingPush ? dt : dt / PUSHING_REDUCTION_FACTOR);
b.second.push -= pushingDir.Multiply(movingPush ? dt : dt / PUSHING_REDUCTION_FACTOR);
}