Micromod: Crosshair Accurate Weapon Firing 1.2

Projects that alter game functions but do not include new maps belong here.
Forum rules
The Projects forums are ONLY for YOUR PROJECTS! If you are asking questions about a project, either find that project's thread, or start a thread in the General section instead.

Got a cool project idea but nothing else? Put it in the project ideas thread instead!

Projects for any Doom-based engine (especially 3DGE) are perfectly acceptable here too.

Please read the full rules for more details.

Micromod: Crosshair Accurate Weapon Firing 1.2

Postby Ivory Duke » Wed Dec 04, 2019 8:51 am

Inspired by the brilliant work of m8f on his Precise Crosshair mod I decided to write a simple function that dynamically adjusts weapons' projectile firing functions' pitch and angle to make it so that projectiles and hitscans always hit the crosshair.
From my testing everything seem to work as intended so I am releasing an example pk3 for modders

NOTE: IF YOU ADD THIS PK3 TO YOUR LOAD ORDER ALL YOU ARE GONNA GET ARE LOAD ERRORS, THIS IS NOT A MOD THAT OVERRIDES MODS' FIRING FUNCTIONS TO WORK ON THE FLY.
IF YOU WANT THIS TO BE A PART OF YOUR MOD YOU NEED TO EDIT EVERY SINGLE NON HITSCAN PROJECTILE AND FIRING FUNCTION IN YOUR CODE.


Code: Select allExpand view
Changelog 1.1:
It has been kindly brought to my attention that it is better to make the function calculating Angle and Pitch an Action, so the code has been slightly tweaked to make it work that way.
I think it looks nicer and more compact

Changelog 1.2:
- simplified angle/pitch calculation code (VectorAngle duh!);
- fixed an issue where depending on the pitch at which you fired a non hitscan projectile it would warp to a different location after impacting with a solid surface. (I would very much love to record a video of what I mean but I am not going to be on my main computer for a few days)


STEPS FOR PROPER INTEGRATION:
1 - set your player.viewheight and player.attackZoffset so that a projectile fired without any XY and Z Offset will be fired exactly at the crosshair's height.
For example: with the regular DoomGuy player.viewheight of 41 you will need to set player.attackZoffset at 17;

2 - Add this Action to your weapon's code, if all your weapons are inheriting from a common one you only need to place it in the code of the "parent" weapon;
Code: Select allExpand view
Action double, double AnglePitchCalculator(double XYOffset, double ZOffset)
{
   FLineTraceData CrossHairProjection;
   LineTrace(Angle, 10000, Pitch, TRF_THRUACTORS, player.ViewHeight, data: CrossHairProjection);
   
   Double AccurateAngle = 0;
   Double AccuratePitch = 0;
   Float Distance = CrossHairProjection.Distance;
      
   if(XYOffset) { AccurateAngle = VectorAngle(Distance, XYOffset); }
   if(ZOffset) { AccuratePitch = VectorAngle(Distance, ZOffset); }
   
   return AccurateAngle, AccuratePitch;
}


3 - edit your fire frame using this as a reference (you can use any projectile firing function, in this example I am using A_FireProjectile just because)
Code: Select allExpand view
TNT1 A 0 //or whatever is the sprite you are using
{
   double XYOffset = CHOOSE_YOUR_XYOFFSET;
   double ZOffset = CHOOSE_YOUR_ZOFFSET;
   Double AccurateAngle, AccuratePitch;
   [AccurateAngle, AccuratePitch] = AnglePitchCalculator(XYOffset, ZOffset);
   A_FireProjectile("YOUR_PROJECTILE",AccurateAngle,true,XYOffset,ZOffset,0,AccuratePitch);
}


4 - (New from 1.2) To make sure non hitscan projectile hit the desired location at all pitches they may be fired shape your code around this example.
DO NOT SPECIFY YOUR PROJECTILE DECAL IN THE ACTOR PROPERTIES! Otherwise the location where your projectile impacts and the one where the decal is spawned will not match!
Code: Select allExpand view
Class ExampleProjectile : Actor
{
   Vector3 ExploPos;
   
   Override void Tick()
   {
      Super.Tick();
      
      //Find the projected point of impact within the immediate vicinity of the projectile's direction
      FLineTraceData ImpactLocation;
      Float ImpactDistance = 2.f * Vel.Length();
      LineTrace(Angle, ImpactDistance, - VectorAngle(Vel.XY.Length(), Vel.Z), data: ImpactLocation);
      if(ImpactLocation.Distance < ImpactDistance) { ExploPos = ImpactLocation.HitLocation; }
   }
   
   States
   {
   Spawn:
      TNT1 A 1;
      Loop;
   
   Crash:
      TNT1 A 0
      {
         if(ExploPos.Length()) { SetXYZ((ExploPos.X, ExploPos.Y, ExploPos.Z - Height / 2.f)); }
         A_SpawnItemEX("YOUR_CRASH_EXPLO_FX");
      }
      Stop;
   
   XDeath:
      TNT1 A 0
      {
         if(ExploPos.Length()) { SetXYZ((ExploPos.X, ExploPos.Y, ExploPos.Z - Height / 2.f)); }
         A_SpawnItemEX("YOUR_XDEATH_EXPLO_FX");
      }
      Stop;
   
   Death:
      TNT1 A 0
      {
         if(ExploPos.Length()) { SetXYZ((ExploPos.X, ExploPos.Y, ExploPos.Z - Height / 2.f)); }
         A_SprayDecal("YOUR_PROJECTILE_DECAL", Radius);
         A_SpawnItemEX("YOUR_DEATH_EXPLO_FX");
      }
      Stop;
   }
}


The way it works is quite simple, a line tracer (a non damaging hit scan attack of sort) is fired at your viewheight and is used to return the distance it traveled.
This distance represents the projection of the crosshair onto the map geometry.
Then basic trigonometry is used to determine which are the angle and pitch necessary to make the fired projectile travel toward the point of impact from the XY and Z offset you make it spawn at.

Here is a basic demonstration of it applied to a few of QC:DE's weapons I edited to use it.


And that is all folks, let me know if you find any issue.

DOWNLOAD HERE
Last edited by Ivory Duke on Mon Jan 06, 2020 12:13 pm, edited 3 times in total.
User avatar
Ivory Duke
Not an actual duke
 
Joined: 17 Jun 2019

Re: Micromod: Accurate to Crosshair Weapon Firing 1.1

Postby Ivory Duke » Wed Dec 04, 2019 4:09 pm

1.1 update, no functional change. Just been told this way is better
User avatar
Ivory Duke
Not an actual duke
 
Joined: 17 Jun 2019

Re: Micromod: Crosshair Accurate Weapon Firing 1.2

Postby Ivory Duke » Fri Dec 06, 2019 8:24 am

1.2, now non hitscan projectiles are always accurate at all firing pitches
User avatar
Ivory Duke
Not an actual duke
 
Joined: 17 Jun 2019


Return to Gameplay Mods

Who is online

Users browsing this forum: mail.ru [Bot], SallazarSpellcaster, Twitter [Bot] and 9 guests