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 7 commits
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
2 changes: 2 additions & 0 deletions source/InjectHooksMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
#include "Birds.h"
#include "Hud.h"
#include "CarFXRenderer.h"
#include "Curves.h"
#include "ProcObjectMan.h"
#include "ProcSurfaceInfo.h"
#include "Pickup.h"
Expand Down Expand Up @@ -540,6 +541,7 @@ void InjectHooksMain() {
CRopes::InjectHooks();
CWeaponInfo::InjectHooks();
CCurrentVehicle::InjectHooks();
CCurves::InjectHooks();
CPlaceName::InjectHooks();
CUserDisplay::InjectHooks();
COnscreenTimer::InjectHooks();
Expand Down
83 changes: 83 additions & 0 deletions source/game_sa/Curves.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include "StdInc.h"
#include "Curves.h"

void CCurves::InjectHooks() {
RH_ScopedClass(CCurves);
RH_ScopedCategoryGlobal();

RH_ScopedGlobalInstall(TestCurves, 0x43C600);
RH_ScopedGlobalInstall(DistForLineToCrossOtherLine, 0x43C610);
RH_ScopedGlobalInstall(CalcSpeedVariationInBend, 0x43C660);
RH_ScopedGlobalInstall(CalcSpeedScaleFactor, 0x43C710);
RH_ScopedGlobalInstall(CalcCorrectedDist, 0x43C880);
RH_ScopedGlobalInstall(CalcCurvePoint, 0x43C900);
}

// unused
// 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;
}

// 0x43C660
float CCurves::CalcSpeedVariationInBend(CVector2D const& startCoors, CVector2D const& endCoors, CVector2D startDir, CVector2D endDir) {
const auto dirDot = startDir.Dot(endDir);
if (dirDot <= 0.f) { // Directions point in the opposite direction
return 1.f / 3.f;
} else if (dirDot <= 0.7f) { // Point in different directions, with ~45 deg tolerance
return (1.f - dirDot / 0.7f) / 3.f;
Pirulax marked this conversation as resolved.
Show resolved Hide resolved
}

// Points in the same dir with ~45 deg tolerance
return CCollision::DistToMathematicalLine2D(endCoors.x, endCoors.y, endDir.x, endDir.y, startCoors.x, startCoors.y) / (startCoors - endCoors).Magnitude() / 3.f;
}

// 0x43C710
float CCurves::CalcSpeedScaleFactor(CVector2D const& startCoors, CVector2D const& endCoors, CVector2D startDir, CVector2D endDir) {
if (const auto startCrossEndDist = DistForLineToCrossOtherLine(startCoors, startDir, endCoors, endDir); startCrossEndDist > 0.f) {
if (const auto endCrossStartDist = DistForLineToCrossOtherLine(endCoors, endDir, startCoors, startDir); endCrossStartDist > 0.f) {
const auto minDist = std::min({ startCrossEndDist, endCrossStartDist, 5.f }); // Min. of the 2 distances, but not higher than 5
return 2.f * minDist + (startCrossEndDist - minDist) + (endCrossStartDist - minDist);
}
}
return (startCoors - endCoors).Magnitude() / (1.f - CalcSpeedVariationInBend(startCoors, endCoors, startDir, endDir));
}

// 0x43C880
float CCurves::CalcCorrectedDist(float curr, float total, float variance, float& outT) {
if (total >= 0.00001f) {
const auto prog = curr / total;
outT = 0.5f - std::cos(PI * prog) * 0.5f;
return std::sin(prog * TWO_PI) * (total / TWO_PI) * variance
+ curr * (1.f - 2.f * variance + 1.f) * 0.5f;
} else {
outT = 0.5f;
return 0.f;
}
}

// 0x43C900
void CCurves::CalcCurvePoint(
const CVector& startPos,
const CVector& endPos,
const CVector& startDir,
const CVector& endDir,
float timeProgress,
int32 totalTimeMs,
CVector& outPos,
CVector& outSpeed
) {
timeProgress = std::clamp(timeProgress, 0.f, 1.f);

const auto curveFactor = CalcSpeedVariationInBend(startPos, endPos, startDir, endDir);
const auto curvePoint = lerp(startPos, endPos, timeProgress);

outPos = curvePoint + (curveFactor * (endDir - startDir));
outSpeed = lerp(startDir, endDir, timeProgress) / ((float)totalTimeMs / 1000.f);
Comment on lines +137 to +141
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

tbh i'd just not hook it
this code can't possibly be 100% correct

}
42 changes: 42 additions & 0 deletions source/game_sa/Curves.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once

#include "Vector.h"

class CVector;

/*!
* All instances of `CVector` that could be changed were changed to be `CVector2D` as the z component is ignored every time.
*/
class CCurves {
public:
static void InjectHooks();

static void TestCurves();
/*!
* @addr 0x43C610
*
* @brief Calculate the smallest distance needed for 2 mathematical lines to cross
*
* @param lineStart The first line's base point
* @param lineDir The first line's direction
* @param otherLineStart The other line's base point
* @param otherLineDir The other line's direction
*
* Calculates the distance to the closest point on the math. line defined by (`otherLineStart`, `otherLineEnd`)
* that is crossed by the math. line defined by (`lineStart`, `lineDir`).
*/
static float DistForLineToCrossOtherLine(CVector2D lineStart, CVector2D lineDir, CVector2D otherLineStart, CVector2D otherLineDir); // Originally last 2 vectors were 2 floats each, changed it for readability
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);
static void CalcCurvePoint(
const CVector& startPos,
const CVector& endPos,
const CVector& startDir,
const CVector& endDir,
float time,
int32 totalTimeMs,
CVector& outPos,
CVector& outSpeed
);
};
Loading