Anti-Recoil Public Release
Posted: Fri May 12, 2023 7:20 am
This is still a work in progress the original code belongs to DrNefario and I used the Dead zone Removal from Antithesis.
The code has been heavily modified, features I plan on adding Randomization On Shot and a more customizable Anti-Recoil.
I'm not able to test the code for in-game functionality at the moment, but it compiles, if anyone wants to test it and give feedback that would be cool. It has comments throughout, so people can see what each part does.
The code has been heavily modified, features I plan on adding Randomization On Shot and a more customizable Anti-Recoil.
I'm not able to test the code for in-game functionality at the moment, but it compiles, if anyone wants to test it and give feedback that would be cool. It has comments throughout, so people can see what each part does.
- Code: Select all
// Define constants for vertical and horizontal recoil, and magnitude of adjustment
const fix32 RECOIL_V = 5.0;
const fix32 RECOIL_H = 1.0;
const fix32 Stick_Noise = 13.0;
// Define settings for Two Part Anti-Recoil
const fix32 SPLIT_PARAMETER = 0.3; // Radius of inner circle
const fix32 MOVEMENT_THRESHOLD = 0.9; // Radius of outer ring
// Declare scaling factor variable
fix32 scale_factor;
// Main loop
main {
if (is_active(BUTTON_5) && is_active(BUTTON_8)) { // Activates the anti-recoil system
fix32 stick_x = get_val(STICK_1_X);
fix32 stick_y = get_val(STICK_1_Y);
// Check if right stick position is within inner circle or outer ring
fix32 stick_dist = sqrt(sq(stick_x) + sq(stick_y));
if (stick_dist < SPLIT_PARAMETER) {
// No anti-recoil compensation applied within inner circle
set_val(STICK_1_X, stick_x);
set_val(STICK_1_Y, stick_y);
} else if (stick_dist < MOVEMENT_THRESHOLD) {
// Apply anti-recoil compensation to outer ring
AntiRecoil(STICK_1_Y, STICK_1_X, RECOIL_V, RECOIL_H);
// Get actual stick values
fix32 stick1_x = get_actual(STICK_1_X);
fix32 stick1_y = get_actual(STICK_1_Y);
fix32 stick2_x = get_actual(STICK_2_X);
fix32 stick2_y = get_actual(STICK_2_Y);
// Deadzone remover
set_val(STICK_1_X, abs(stick1_x) < Stick_Noise ? 0.0 : get_val(STICK_1_X));
set_val(STICK_1_Y, abs(stick1_y) < Stick_Noise ? 0.0 : get_val(STICK_1_Y));
set_val(STICK_2_X, abs(stick2_x) < Stick_Noise ? 0.0 : get_val(STICK_2_X));
set_val(STICK_2_Y, abs(stick2_y) < Stick_Noise ? 0.0 : get_val(STICK_2_Y));
}
}
}
// Function to apply anti-recoil to the right stick
void AntiRecoil(uint8 axisY, uint8 axisX, fix32 recoil_Y, fix32 recoil_X)
{
// Check if recoil exists
if (recoil_Y + recoil_X)
{
// Get actual values of right stick Y and X axis
fix32 RY = get_actual(axisY);
fix32 RX = get_actual(axisX);
// Calculate magnitude of current recoil vector
fix32 RXRY = sq(RY) + sq(RX);
// If recoil vector magnitude is smaller than or equal to adjustment magnitude,
// apply simple adjustment to stick value based on recoil and input values
if (RXRY <= sq(Stick_Noise))
{
// Calculate scaling factor
scale_factor = sqrt(sq(recoil_Y) + sq(recoil_X));
// Scale input values based on scaling factor and adjust stick value based on input value magnitude
set_val(axisY, (recoil_Y / scale_factor) * (scale_factor + Stick_Noise));
set_val(axisX, (recoil_X / scale_factor) * (scale_factor + Stick_Noise));
}
// If recoil vector magnitude is larger than adjustment magnitude,
// apply more complex adjustment to stick value based on recoil and input values
else
{
// Calculate true radius of current recoil vector and set scaling factor
fix32 true_radius = min(sqrt(RXRY), 100.0);
scale_factor = 100.0 - Stick_Noise;
// Scale input values based on current recoil vector radius
RY *= ((true_radius - Stick_Noise) / true_radius);
RX *= ((true_radius - Stick_Noise) / true_radius);
// Apply scaling factor to recoil values and adjust stick value based on input value magnitude
recoil_Y -= ((recoil_Y * 0.80) / scale_factor) * true_radius;
recoil_X -= ((recoil_X * 0.80) / scale_factor) * true_radius;
RY += recoil_Y - ((recoil_Y / scale_factor) * abs(RY));
RX += recoil_X - ((recoil_X / scale_factor) * abs(RX));
// Adjust stick value based on final adjusted input value and adjustment magnitude
scale_factor = sqrt(sq(RY) + sq(RX));
RY = (RY / scale_factor) * (scale_factor + Stick_Noise);
RX = (RX / scale_factor) * (scale_factor + Stick_Noise);
// Set clamped stick value based on final adjusted input value
set_val(axisY, clamp(RY, -100f, 100f));
set_val(axisX, clamp(RX, -100f, 100f));
}
}
return;
}