Skip to content

Instantly share code, notes, and snippets.

@run-dlang
Created October 13, 2023 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save run-dlang/997df012306ef69a4bf232ef199b033f to your computer and use it in GitHub Desktop.
Save run-dlang/997df012306ef69a4bf232ef199b033f to your computer and use it in GitHub Desktop.
Code shared from run.dlang.io. Run with '-unittest'
auto targetPosition = target.headPosition;
import std.stdio;
//writefln!"Writing yaw: %s"(cameraPosition.yawTo(targetPosition));
//writefln!"Writing pitch: %s"(cameraPosition.pitchTo(targetPosition));
immutable controlAngles = getControlAngles;
immutable viewAngles = getViewAngles;
immutable currentYaw = controlAngles[1];
immutable currentPitch = controlAngles[0];
auto spread = 0.0F;
if (const spreadPointer = localPlayer.weaponName in spreadByWeapon)
{
spread = *spreadPointer;
targetPosition.z += 9;
}
immutable yawDifference = getDifferenceBetweenAngles(currentYaw, viewAngles[1]);
immutable pitchDifference = getDifferenceBetweenAngles(currentPitch, viewAngles[0]);
immutable neededYaw = normalize(cameraPosition.yawTo(targetPosition)
- yawDifference * 2 * recoilCompensationScale);
immutable neededPitch = normalize(cameraPosition.pitchTo(targetPosition)
- pitchDifference * 2 * recoilCompensationScale - spread / (180 / PI));
immutable yawDelta = getDifferenceBetweenAngles(currentYaw, neededYaw);
immutable pitchDelta = getDifferenceBetweenAngles(currentPitch, neededPitch);
if (GetKeyState(aimKey) < 0)
{
immutable angle = atan2(pitchDelta, yawDelta);
immutable distance = min(
degreesPerSecond.toRadians / 128,
sqrt(yawDelta ^^ 2 + pitchDelta ^^ 2));
immutable smoothedYawDelta = cos(angle) * distance;
immutable smoothedPitchDelta = sin(angle) * distance;
if (!smoothedYawDelta.isNaN && !smoothedPitchDelta.isNaN)
{
setControlAngles([
currentPitch + smoothedPitchDelta,
currentYaw + smoothedYawDelta
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment