Skip to content

Commit

Permalink
optimize MoveAlgo::tryMove
Browse files Browse the repository at this point in the history
* use skip-move strategy to save CPU performance on idle npc's
#188
  • Loading branch information
Try committed Aug 29, 2021
1 parent a8ecb48 commit 4632d24
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 22 deletions.
12 changes: 3 additions & 9 deletions game/game/movealgo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const float MoveAlgo::closeToPointThreshold = 50;
const float MoveAlgo::climbMove = 55;
const float MoveAlgo::gravity = DynamicWorld::gravity;
const float MoveAlgo::eps = 2.f; // 2-santimeters
const float MoveAlgo::epsAni = 0.25f; // 25-millimeters
const int32_t MoveAlgo::flyOverWaterHint = 999999;

MoveAlgo::MoveAlgo(Npc& unit)
Expand Down Expand Up @@ -72,11 +71,6 @@ bool MoveAlgo::tryMove(float x, float y, float z) {
}

bool MoveAlgo::tryMove(float x,float y,float z, DynamicWorld::CollisionTest& out) {
if(flags==NoFlags && std::fabs(x)<epsAni && std::fabs(y)<epsAni && std::fabs(z)<epsAni) {
skipMove = Tempest::Vec3(x,y,z);
return true;
}
skipMove = Tempest::Vec3();
return npc.tryMove({x,y,z},out);
}

Expand Down Expand Up @@ -340,7 +334,7 @@ void MoveAlgo::implTick(uint64_t dt, MvFlags moveFlg) {

if(isInAir()) {
if(npc.isJumpAnim()) {
auto dp = skipMove+npcMoveSpeed(dt,moveFlg);
auto dp = npcMoveSpeed(dt,moveFlg);
tryMove(dp.x,dp.y,dp.z);
fallSpeed += dp;
fallCount += float(dt);
Expand All @@ -356,7 +350,7 @@ void MoveAlgo::implTick(uint64_t dt, MvFlags moveFlg) {
return;
}

auto dp = skipMove+npcMoveSpeed(dt,moveFlg);
auto dp = npcMoveSpeed(dt,moveFlg);
auto pos = npc.position();
float pY = pos.y;
float fallThreshold = stepHeight();
Expand Down Expand Up @@ -860,7 +854,7 @@ void MoveAlgo::onMoveFailed(const Tempest::Vec3& dp, const DynamicWorld::Collisi
Tempest::Vec3 corr;
for(int i=5; i<=35; i+=5) {
for(float angle:{float(i),-float(i)}) {
applyRotation(corr,dp+skipMove,float(angle*M_PI)/180.f);
applyRotation(corr,dp,float(angle*M_PI)/180.f);
if(npc.tryMove(corr)) {
if(forward)
npc.setDirection(npc.rotation()+angle);
Expand Down
3 changes: 0 additions & 3 deletions game/game/movealgo.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ class MoveAlgo final {
Tempest::Vec3 fallSpeed ={};
float fallCount=0.f;

Tempest::Vec3 skipMove = {};

uint64_t climbStart=0;
Tempest::Vec3 climbPos0={};
float climbHeight=0.f;
Expand All @@ -161,6 +159,5 @@ class MoveAlgo final {

static const float gravity;
static const float eps;
static const float epsAni;
static const int32_t flyOverWaterHint;
};
13 changes: 11 additions & 2 deletions game/physics/dynamicworld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -937,13 +937,22 @@ bool DynamicWorld::NpcItem::testMove(const Tempest::Vec3& dst, const Tempest::Ve
return true;
}

bool DynamicWorld::NpcItem::tryMove(const Tempest::Vec3& dp, CollisionTest& out) {
bool DynamicWorld::NpcItem::tryMove(const Tempest::Vec3& to, CollisionTest& out) {
if(!obj)
return false;
return false;

// 2-santimeters
static const float eps = 2;

auto initial = obj->pos;
auto r = obj->r;
int count = 1;
auto dp = to-initial;

if(std::abs(dp.x)<eps && std::abs(dp.y)<eps && std::abs(dp.z)<eps) {
// skip-move
return true;
}

if((dp.x*dp.x+dp.z*dp.z)>r*r || dp.y>obj->h*0.5f) {
const int countXZ = int(std::ceil(std::sqrt(dp.x*dp.x+dp.z*dp.z)/r));
Expand Down
2 changes: 1 addition & 1 deletion game/physics/dynamicworld.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ class DynamicWorld final {
void setUserPointer(void* p);

float centerY() const;
const Tempest::Vec3& position() const;

bool testMove(const Tempest::Vec3& pos, CollisionTest& out);
bool testMove(const Tempest::Vec3& pos, const Tempest::Vec3& from, CollisionTest& out);
Expand All @@ -92,6 +91,7 @@ class DynamicWorld final {
NpcBody* obj = nullptr;
float r = 0.f;
void implSetPosition(const Tempest::Vec3& pos);
const Tempest::Vec3& position() const;

friend class DynamicWorld;
};
Expand Down
11 changes: 4 additions & 7 deletions game/world/objects/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3508,19 +3508,16 @@ bool Npc::tryMove(const Tempest::Vec3& dp) {
}

bool Npc::tryMove(const Vec3& dp, DynamicWorld::CollisionTest& out) {
if(!physic.tryMove(dp,out))
auto to = Vec3(x,y,z)+dp;
if(!physic.tryMove(to,out))
return false;
setViewPosition(physic.position());
setViewPosition(to);
return true;
}

bool Npc::tryTranslate(const Vec3& pos) {
DynamicWorld::CollisionTest out;
if(physic.tryMove(pos-physic.position(),out)) {
setViewPosition(physic.position());
return true;
}
return false;
return tryMove(pos-Vec3(x,y,z),out);
}

Npc::JumpStatus Npc::tryJump() {
Expand Down

0 comments on commit 4632d24

Please sign in to comment.