Author Topic: Been attempting to implement knockback - Some results  (Read 11099 times)

Offline WeaponMaster

  • Newbie Mark III
  • *
  • Posts: 46
Re: Been attempting to implement knockback - Some results
« Reply #30 on: May 26, 2019, 03:08:27 am »
I've got things basically settled code wise, tomorrow afternoon I'm going to do a small test to make sure nothing is completely broken and then post the updated code to the SVN.

Edit: Knockback has been stress tested. By moving the clearing for knockback to the main ship logic, there's never ships that don't get knockback. Though with the new logic sometimes ships go in random directions. I'm sure that's mathematically correct, but it feels weird when 99% of ships get pulled to the center, and 1% ends up getting pushed away.

Knockback Test, level 10 AI, infinite range VWing, infinite range knockback, -1300 knockback power: https://youtu.be/bLQ9EVKhemQ
« Last Edit: May 26, 2019, 03:55:28 pm by WeaponMaster »

Offline RocketAssistedPuffin

  • Arcen Volunteer
  • Sr. Member
  • *****
  • Posts: 260
Re: Been attempting to implement knockback - Some results
« Reply #31 on: May 27, 2019, 10:49:36 am »
Ark One and Military Stations now use this. Going to be looking into perhaps some of the ship variants like Harassers to get small effect versions. I'll also create a few more Eyes for it.
Autistic, so apologies for any communication difficulties!

Offline AnnoyingOrange

  • Jr. Member Mark II
  • **
  • Posts: 71
Re: Been attempting to implement knockback - Some results
« Reply #32 on: May 28, 2019, 04:27:22 am »
Though with the new logic sometimes ships go in random directions. I'm sure that's mathematically correct, but it feels weird when 99% of ships get pulled to the center, and 1% ends up getting pushed away.

I'm not sure that's mathematically correct, especially as it looks like you're testing with only one big blob of pulling ships: in that case, the pull center should always be within the blob, no matter where the target is or how many of the pulling ships hit it.

Glad to hear the performance is good, though.

Offline WeaponMaster

  • Newbie Mark III
  • *
  • Posts: 46
Re: Been attempting to implement knockback - Some results
« Reply #33 on: May 29, 2019, 09:18:31 pm »
The only thing I could possibly guess (and this is noted in my code) is that there's an integer overflow that happens when too many knockbacks happen at once.

Code: [Select]
            ArcenPoint centroid = ArcenPoint.ZeroZeroPoint;
            for ( int i = 0; i < knockbackCount; i++ )
            {
                ArcenPoint point = knockbackList[i].LeftItem;
                centroid += point; // Sum all sources of knockback, might need a temporary 64 bit variable if there's overflow issues with lots of knockback sources
            }
            centroid.X /= knockbackCount;
            centroid.Y /= knockbackCount; // Get the averages of the locations

The radius for a gravwell is 26,000 and 0,0 is in the top left corner I think, so then the max possible size of either one of the variables is 52,000. The size of an int is 2,147,000,000 which means you can fit 41,000 knockbacks before an overflow....

I've got no clue on what's going wrong with the code, or if there's even an issue. There might be an issue with the knockback not resetting after one frame, but that doesn't make sense because I clear the knockback list once the ship updates its position on the main thread. Right now it goes: (main)Update position > (main)Take Damage > (main)Add knockback source > (short term planning thread)Calculate movement > (main)Update position and clear knockback.

Offline x4000

  • Chris McElligott Park, Arcen Founder and Lead Dev
  • Arcen Staff
  • Zenith Council Member Mark III
  • *****
  • Posts: 31,651
Re: Been attempting to implement knockback - Some results
« Reply #34 on: May 29, 2019, 09:51:32 pm »
You probably are indeed getting an overflow, because it's not using an integer... it's using a long, in the form of an FInt, which is basically fixed-integer math.  You're going to have a lot of problems with that, because the maximum of that is... 12 bits less than a long, IIRC?  I don't recall what that works out to, but it's much lower than the int ceiling IIRC.

Revised the following thoughts at the bottom after reading the code more carefully.
What I suggest is that you offset each of these points to the zero position and have them all be relative.  So basically if you use their offset (negative or positive) from the center of the battlefield (Engine_AIW2.Instance.CombatCenter), you'll have vastly smaller numbers and use both the positive and negative part of the integer range.  There are a variety of kinds of calculations that work best if their math is centered around the actual centerpoint as if the centerpoint were 0,0 in it's own "local coordinate space," anyway. 

...But then there are others that work better if all the numbers stay in the positive integer range and never go negative, hence us having the battlefield offset the way it is.

Another option would be to use System.Numerics.Vector4 or similar, and then add them as those.  If you do those as vector operations ( centroid /= knockbackCount ), then it actually will run twice as fast as the code you have here does.  System.Numerics.Vectors use SIMD, which gives you 2-4 operations for the cost of one, when it comes to the additions and divisions, etc.  They also don't have the same sort of precision problems in general.

What they DO have, unfortunately, is a tendency to be slightly different on different machines.  So there's a slight drift that will come out of the knockback that will have to be fixed via desync-fixing code that is already planned anyway.  So if you can do it with ArcenPoints, that's awesome because that will be deterministic.  But if you're having trouble with that, then the alternative is not bad at all.

Revised thoughts on the knockback centroids after re-reading your code:

- It looks like you're already centering this on zero, which makes sense to me.  I'm guessing that you're using the sources of knockback as magnitude offsets from the point of the ship, is that correct?  So if my ship is at 12444,50660 position, but gets knocked back by 1200,-600 relative to that, that's what is in the knockbackList... I hope?  If so, then I can't see how you'd be getting an overflow issue. 

- If not, then I can see why the overflow issue is probably happening, and so just subtracting Engine_AIW2.Instance.CombatCenter from every knockbackList.LeftItem before it is added, and then at the end adding Engine_AIW2.Instance.CombatCenter to the centroid, would solve the problem.
Have ideas or bug reports for one of our games?  Mantis for Suggestions and Bug Reports. Thanks for helping to make our games better!