Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Curvy Cruves #482

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 7 additions & 55 deletions source/game_sa/Curves.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ void CCurves::InjectHooks() {
}

// unused
// 0x43C610
// 0x43C600
void CCurves::TestCurves() {
return;
}

// 0x43C610
float CCurves::DistForLineToCrossOtherLine(CVector2D lineStart, CVector2D lineDir, CVector2D otherLineStart, CVector2D otherLineDir) {
const auto t = lineDir.Cross(otherLineDir);
return (t == 0.f) ? -1.f : (lineStart - otherLineStart).Cross(otherLineDir) / -t;
Expand All @@ -45,7 +46,7 @@ float CCurves::CalcSpeedScaleFactor(CVector2D const& startCoors, CVector2D const
return 2.f * minDist + (startCrossEndDist - minDist) + (endCrossStartDist - minDist);
}
}
return (startCoors - endCoors).Magnitude() / (1.f - CCurves::CalcSpeedVariationInBend(startCoors, endCoors, startDir, endDir));
return (startCoors - endCoors).Magnitude() / (1.f - CalcSpeedVariationInBend(startCoors, endCoors, startDir, endDir));
}

// 0x43C880
Expand Down Expand Up @@ -74,58 +75,9 @@ void CCurves::CalcCurvePoint(
) {
timeProgress = std::clamp(timeProgress, 0.f, 1.f);

const auto [pos, speedScale] = [&]() -> std::tuple<CVector, float> {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so you just removed this code? You should just comment it out.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're absolutely right.

const auto startCrossEndDist = DistForLineToCrossOtherLine(startPos, startDir, endPos, endDir);
const auto endCrossStartDist = DistForLineToCrossOtherLine(endPos, endDir, startPos, startDir);
if (startCrossEndDist > 0.f && endCrossStartDist > 0.f) {
const auto minDist = std::min({ startCrossEndDist, endCrossStartDist, 5.f }); // Min. of the 2 distances, but not higher than 5
const auto speedScale = 2.f * minDist + (startCrossEndDist - minDist) + (endCrossStartDist - minDist);
const auto speedScaleWithTime = speedScale * timeProgress;


if (startCrossEndDist - minDist >= speedScaleWithTime) {
return { startPos + startDir * speedScaleWithTime, speedScale };
}

if (speedScaleWithTime >= startCrossEndDist + minDist) {
return { endPos + endDir * speedScaleWithTime, speedScale };
}

// NOTE: Seems like some cubic bezier curve thingy? Not sure.
const auto t = speedScaleWithTime * (startCrossEndDist - minDist) / (2.f * minDist);
const auto d = endCrossStartDist - minDist;
return {
lerp(
(startPos + (d * startDir)) + (minDist * (t * startDir)),
(endPos + (d * endDir)) - (minDist * (t * endDir)),
t
),
speedScale
};
} else {
const auto startEndPosDist2D = (startPos - endPos).Magnitude2D();
const auto speedVarianceInBend = CalcSpeedVariationInBend(startPos, endPos, startDir, endDir);
const auto CalcSpeed = [&](float t) {
return (startEndPosDist2D / (1.f - speedVarianceInBend)) * t;
};
float correctedDistT{};
const auto correctedDist = CalcCorrectedDist(
CalcSpeed(timeProgress),
CalcSpeed(1.f),
speedVarianceInBend,
correctedDistT
);
return {
lerp(
startPos + startDir * correctedDist,
endPos + endDir * (correctedDist - startEndPosDist2D),
timeProgress
),
startEndPosDist2D
};
}
}();
const auto curveFactor = CalcSpeedVariationInBend(startPos, endPos, startDir, endDir);
const auto curvePoint = lerp(startPos, endPos, timeProgress);

outPos = pos;
outSpeed = lerp(endDir, startDir, timeProgress) * speedScale / ((float)totalTimeMs / 1000.f);
outPos = curvePoint + (curveFactor * (endDir - startDir));
outSpeed = lerp(startDir, endDir, timeProgress) / ((float)totalTimeMs / 1000.f);
}
2 changes: 1 addition & 1 deletion source/game_sa/Curves.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CCurves {
static float CalcSpeedVariationInBend(CVector2D const& startCoors, CVector2D const& endCoors, CVector2D startDir, CVector2D endDir); // Same as above
static float CalcSpeedScaleFactor(CVector2D const& startCoors, CVector2D const& endCoors, CVector2D startDir, CVector2D endDir); // Same as above
static float CalcCorrectedDist(float curr, float total, float variance, float& outT);
void CalcCurvePoint(
static void CalcCurvePoint(
const CVector& startPos,
const CVector& endPos,
const CVector& startDir,
Expand Down
Loading