Added dynamic step size for collision tests

pull/173/head
Javidx9 4 years ago committed by GitHub
parent 43c1ddc6b3
commit 6041b4b677
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 26
      Extensions/olcPGEX_RayCastWorld.h

@ -3,7 +3,7 @@
+-------------------------------------------------------------+
| OneLoneCoder Pixel Game Engine Extension |
| Ray Cast World v1.01 |
| Ray Cast World v1.02 |
+-------------------------------------------------------------+
NOTE: UNDER ACTIVE DEVELOPMENT - THERE ARE BUGS/GLITCHES
@ -65,6 +65,11 @@
Author
~~~~~~
David Barr, aka javidx9, ©OneLoneCoder 2019, 2020
Revisions:
1.00: Initial Release
1.01: Fix NaN check on overlap distance (Thanks Dandistine)
1.02: Added dynamic step size for collisions
*/
#ifndef OLC_PGEX_RAYCASTWORLD_H
@ -262,8 +267,22 @@ void olc::rcw::Engine::Update(float fElapsedTime)
std::shared_ptr<olc::rcw::Object> object = ob.second;
if (!object->bIsActive) continue;
int nSteps = 1;
float fDelta = fElapsedTime;
float fTotalTravel = (object->vel * fElapsedTime).mag2();
float fTotalRadius = (object->fRadius * object->fRadius);
if(fTotalTravel >= fTotalRadius)
{
float fSteps = std::ceil(fTotalTravel / fTotalRadius);
nSteps = int(fSteps);
fDelta = fElapsedTime / fSteps;
}
for (int nStep = 0; nStep < nSteps; nStep++)
{
// Determine where object is trying to be
olc::vf2d vPotentialPosition = object->pos + object->vel * fElapsedTime;
olc::vf2d vPotentialPosition = object->pos + object->vel * fDelta;
// If the object can collide with other objects
if (object->bCollideWithObjects)
@ -311,6 +330,8 @@ void olc::rcw::Engine::Update(float fElapsedTime)
olc::vi2d vAreaBR = { std::max(vCurrentCell.x, vTargetCell.x) + 1, std::max(vCurrentCell.y, vTargetCell.y) + 1 };
// Iterate through each cell in test area
olc::vi2d vCell;
for (vCell.y = vAreaTL.y; vCell.y <= vAreaBR.y; vCell.y++)
@ -365,6 +386,7 @@ void olc::rcw::Engine::Update(float fElapsedTime)
object->pos = vPotentialPosition;
}
}
}
void olc::rcw::Engine::Render()
{

Loading…
Cancel
Save