Add curve parameter to lerp

This commit is contained in:
Gent 2020-09-26 18:45:19 -04:00
parent 0ea5712f8c
commit 56a92d302f
3 changed files with 19 additions and 6 deletions

View File

@ -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;
}

View File

@ -311,17 +311,21 @@ void TransportManager::stepNPCPathing() {
/*
* Linearly interpolate between two points and insert the results into a queue.
*/
void TransportManager::lerp(std::queue<WarpLocation>* queue, WarpLocation start, WarpLocation end, int gapSize) {
void TransportManager::lerp(std::queue<WarpLocation>* 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<WarpLocation>* queue, WarpLocation start, WarpLocation end, int gapSize) {
lerp(queue, start, end, gapSize, 1);
}

View File

@ -34,5 +34,6 @@ namespace TransportManager {
void stepNPCPathing();
void stepSkywaySystem();
void lerp(std::queue<WarpLocation>*, WarpLocation, WarpLocation, int, float);
void lerp(std::queue<WarpLocation>*, WarpLocation, WarpLocation, int);
}