Measuring velocity on impact

Everything about mods can be found here.

Moderator: Morax

Measuring velocity on impact

Postby Luxor144 » 17 Jun 2017, 02:26

So, messing with my mod again, and I've come into a bit of a pickle. Short story short - I need to measure the speed of a plane at the moment it crashes into water. Now, obvious method is to use GetVelocity(), but game's engine cheekily stops all the motion at the moment of impact (or, well, at least that's what I managed to gather, as all I get from this function in this case are zeroes). A workaround for that (albeit a sh*tty one) is to measure speed at OnKilled (while the plane is still soaring majestically through the skies, only slightly on fire and in half), but the problem with that is that I need to use the measured speed in another function - OnImpact.
Since these functions happen at different times, is there a way to store the measurement from one function somewhere, and then call it back from the other function when needed? Preferably without adding additional arguments to functions. That self.Trash thing looked promising, but not only I don't know how it functions, I'm also not sure if it works with real numbers.
User avatar
Luxor144
Crusader
 
Posts: 11
Joined: 09 Sep 2013, 08:03
Has liked: 0 time
Been liked: 1 time

Re: Measuring velocity on impact

Postby nine2 » 17 Jun 2017, 04:03

ive never done it but i thought you could just make up variables
in func1
entity.mything = 1
in func2
LOG(entity.mything) // 1
nine2
Councillor - Promotion
 
Posts: 2416
Joined: 16 Apr 2013, 10:10
Has liked: 285 times
Been liked: 515 times
FAF User Name: Anihilnine

Re: Measuring velocity on impact

Postby CookieNoob » 17 Jun 2017, 08:05

try this hook of the unit script:
Code: Select all
local oldUAA2102 = UAA2102
UAA2102 = Class(oldUAA2102) {
    speed_at_death = nil,

    OnKilled = function(self, instigator, type, overkillRatio)
        Forkthread(function(
            self.speed_at_death = self:GetVelocity()
            while not self.speed_at_death[2] == 0 and self:GetPosition()[2] > GetSurfaceHeight(self:GetPosition()[1],self:GetPosition()[3])do
                WaitSeconds(0.2)
                self.speed_at_death = self:GetVelocity()
            end
        end
        AirUnit.OnKilled(self, instigator, type, overkillRatio)
    end,

TypeClass = UAA2102
}


you can then get this speed from
Code: Select all
YourUnit.speed_at_death


for an example you can look at the nomads mothership code:
https://github.com/Nomads-Project/nomad ... pt.lua#L26
https://github.com/Nomads-Project/nomad ... t.lua#L349
this code is called from the acu

alternatively you can put this code into the AirUnit class. Surely this can be optimized but this should work (didnt test it though)
Check out the next level of maps: viewtopic.php?f=53&t=13014
For adaptivity, customizability and less clutter in the vault.
User avatar
CookieNoob
Priest
 
Posts: 477
Joined: 02 Aug 2014, 17:07
Has liked: 65 times
Been liked: 249 times
FAF User Name: CookieNoob

Re: Measuring velocity on impact

Postby Luxor144 » 18 Jun 2017, 22:26

Seems to be working. Many thanks.
User avatar
Luxor144
Crusader
 
Posts: 11
Joined: 09 Sep 2013, 08:03
Has liked: 0 time
Been liked: 1 time


Return to Mods & Tools

Who is online

Users browsing this forum: No registered users and 1 guest