From 56a92d302f62e753f895df769099ebd3f115b94a Mon Sep 17 00:00:00 2001 From: Gent Date: Sat, 26 Sep 2020 18:45:19 -0400 Subject: [PATCH] Add curve parameter to lerp --- src/TableData.cpp | 12 ++++++++++-- src/TransportManager.cpp | 12 ++++++++---- src/TransportManager.hpp | 1 + 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/TableData.cpp b/src/TableData.cpp index 13993f3..c820f9f 100644 --- a/src/TableData.cpp +++ b/src/TableData.cpp @@ -239,10 +239,11 @@ void TableData::loadPaths(int* nextId) { std::cout << "[INFO] Loaded " << TransportManager::SkywayPaths.size() << " skyway paths" << std::endl; // slider circuit + int sliders = 0; nlohmann::json pathDataSlider = pathData["slider"]; for (nlohmann::json::iterator _sliderPoint = pathDataSlider.begin(); _sliderPoint != pathDataSlider.end(); _sliderPoint++) { auto sliderPoint = _sliderPoint.value(); - if (sliderPoint["stop"]) { // check if this point in the circuit is a stop + if (sliderPoint["stop"] && sliders % 2 == 0) { // check if this point in the circuit is a stop // spawn a slider std::cout << "bus ID was " << *nextId << std::endl; BaseNPC* slider = new BaseNPC(sliderPoint["iX"], sliderPoint["iY"], sliderPoint["iZ"], 1, (*nextId)++, NPC_BUS); @@ -250,6 +251,7 @@ void TableData::loadPaths(int* nextId) { NPCManager::updateNPCPosition(slider->appearanceData.iNPC_ID, slider->appearanceData.iX, slider->appearanceData.iY, slider->appearanceData.iZ); // set slider path to a rotation of the circuit constructPathSlider(pathDataSlider, 0, slider->appearanceData.iNPC_ID); + sliders++; } } @@ -300,7 +302,13 @@ void TableData::constructPathSlider(nlohmann::json points, int rotations, int sl for (int i = 0; i < stopTime + 1; i++) // repeat point if it's a stop route.push(from); // add point A to the queue WarpLocation to = { point["iX"] , point["iY"] , point["iZ"] }; // point B coords - TransportManager::lerp(&route, from, to, SLIDER_SPEED); // lerp from A to B (arbitrary speed) + float curve = 1; + if (stopTime > 0) { // point A is a stop + curve = 2.0f; + } else if (point["stop"]) { // point B is a stop + curve = 0.35f; + } + TransportManager::lerp(&route, from, to, SLIDER_SPEED, curve); // lerp from A to B (arbitrary speed) from = to; // update point A stopTime = point["stop"] ? SLIDER_STOP_TICKS : 0; } diff --git a/src/TransportManager.cpp b/src/TransportManager.cpp index 64f959e..dc8409d 100644 --- a/src/TransportManager.cpp +++ b/src/TransportManager.cpp @@ -311,17 +311,21 @@ void TransportManager::stepNPCPathing() { /* * Linearly interpolate between two points and insert the results into a queue. */ -void TransportManager::lerp(std::queue* queue, WarpLocation start, WarpLocation end, int gapSize) { +void TransportManager::lerp(std::queue* queue, WarpLocation start, WarpLocation end, int gapSize, float curve) { int dXY = hypot(end.x - start.x, end.y - start.y); // XY plane distance int distanceBetween = hypot(dXY, end.z - start.z); // total distance - int lerps = distanceBetween / gapSize; // integer division to ensure a whole number of in-between points - for (int i = 0; i < lerps; i++) { + int lerps = distanceBetween / gapSize; // number of intermediate points to add + for (int i = 1; i <= lerps; i++) { WarpLocation lerp; // lerp math - float frac = (i + 1) * 1.0f / (lerps + 1); + //float frac = i / (lerps + 1); + float frac = powf(i, curve) / powf(lerps + 1, curve); lerp.x = (start.x * (1.0f - frac)) + (end.x * frac); lerp.y = (start.y * (1.0f - frac)) + (end.y * frac); lerp.z = (start.z * (1.0f - frac)) + (end.z * frac); queue->push(lerp); // add lerp'd point } } +void TransportManager::lerp(std::queue* queue, WarpLocation start, WarpLocation end, int gapSize) { + lerp(queue, start, end, gapSize, 1); +} diff --git a/src/TransportManager.hpp b/src/TransportManager.hpp index d905455..7df3825 100644 --- a/src/TransportManager.hpp +++ b/src/TransportManager.hpp @@ -34,5 +34,6 @@ namespace TransportManager { void stepNPCPathing(); void stepSkywaySystem(); + void lerp(std::queue*, WarpLocation, WarpLocation, int, float); void lerp(std::queue*, WarpLocation, WarpLocation, int); }