Formula Test...

Status
Not open for further replies.
I really dont know what to call this and IDK how to explain well but let me just draw and give a code sample...

I want a missile from up above to target the ground like the drawing below, it's like an aircraft shoots a missile to a building...
attachment.php


Below is the code/formula I use, my test shows that it's accurate, I wanna know if the calculations are correct or is there any snippet available somewhere?...
If it's correct, how to call it?...

JASS:
function GetDown takes real h, real d, real s returns real
    return h-((s/d)*h)
endfunction

struct TestH
    real d //distance
    real h //height
    real s //speed
    
    implement CTLExpire
        if .d > 0 then
            set .d = .d - .s
            set .h = GetDown(.h, .d, .s)
            call BJDebugMsg("distance=="+R2S(.d))
            call BJDebugMsg("height=="+R2S(.h))
        else
            call .destroy()
        endif
    implement CTLEnd

    static method run takes nothing returns nothing
        local thistype this = create()
        set .d = GetRandomReal(600, 1000)
        set .h = GetRandomReal(200, 500)
        set .s = GetRandomReal(5, 15)
        call BJDebugMsg("DISTANCE==============="+R2S(.d))
        call BJDebugMsg("HEIGHT==============="+R2S(.h))
        call BJDebugMsg("SPEED==============="+R2S(.s))
    endmethod
    
    static method onInit takes nothing returns nothing
        local trigger t = CreateTrigger()
        call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_EFFECT)
        call TriggerAddAction(t, function TestH.run)      
    endmethod
endstruct
 

Attachments

  • prj.jpg
    prj.jpg
    16.3 KB · Views: 261
That's not accurate, sample;
H = 300
DIST = 800
SPEED = 10
PERIOD = 0.03125

returns >>> 0.1171875

Did you test my code?, it requires CTL though, say if the variables are the same...

everytime it returns:
296.2, 292.4, 288.6, 284.8 etc...until distance is less than 0...period doesnt matter at all coz it only executes, not part of the calculations...
 
Test this against your own.

JASS:
library height requires CTL, Print
    globals
        private location loc = Location(0,0)
    endglobals

    function GetLocZ takes real x, real y returns real
        call MoveLocation(loc, x, y)
        return GetLocationZ(loc)
    endfunction

    struct test extends array
        real dx     // Speed in x axis
        real dy     // Speed in y axis
        real dz     // Speed in z axis
        real z1     // Z offset at launch position
        real h      // Current height
        
        unit u
        
        integer k   // Determines when the instance is terminated, you can replace this
        
        implement CTL
            local real x
            local real y
            local real z
        implement CTLExpire
            set x = GetUnitX(.u) + dx
            set y = GetUnitY(.u) + dy
            
            call SetUnitX(.u, x)
            call SetUnitY(.u, y)
            
            set .h = .h + .dz
            set z = .z1 + .h - GetLocZ(x, y)
            call SetUnitFlyHeight(.u, z, 0)
            
            set .k = .k - 1
            if .k == 0 then
                //call Print(R2S(GetUnitFlyHeight(.u)), 5)
                call KillUnit(.u)
                set .u = null
                call .destroy()
            endif
        implement CTLEnd

        static method run takes unit u1, unit u2, real velocity returns nothing
            local thistype this = create()
            
            local real x1 = GetUnitX(u1)
            local real x2 = GetUnitX(u2)
            local real y1 = GetUnitY(u1)
            local real y2 = GetUnitY(u2)
            local real z1 = GetUnitFlyHeight(u1) + GetLocZ(x1, y1)
            local real z2 = GetUnitFlyHeight(u2) + GetLocZ(x2, y2)
            
            local real Dx = (x2 - x1)*(x2 - x1)
            local real Dy = (y2 - y1)*(y2 - y1)
            local real Dz = (z2 - z1)*(z2 - z1)
            
            local real a = Atan2(y2 - y1, x2 - x1)
            
            local real s = SquareRoot(Dx + Dy + Dz)     // Actual distance to travel
            local real t1 = s/velocity                  // The time it takes to travel
            local real k = t1/0.031250000               // How many times the position is updated
                                                        // until destination is reached
            
            local real dx = SquareRoot(Dx+Dy)*Cos(a)/k
            local real dy = SquareRoot(Dx+Dy)*Sin(a)/k
            local real dz = (z2 - z1)/k
            
            //call Print(R2S(a*bj_RADTODEG), 5)
            
            set .k = R2I(k)
            set .dx = dx
            set .dy = dy
            set .dz = dz
            set .z1 = GetLocZ(x1, y1)
            set .h = GetUnitFlyHeight(u1)

            set .u = CreateUnit(Player(15), 'u000', x1, y1, 0)
            call UnitAddAbility(.u, 'Arav')
            call SetUnitFlyHeight(.u, GetUnitFlyHeight(u1), 0)
            call SetUnitFacing(.u, a*bj_RADTODEG)
        endmethod
        
        static method onInit takes nothing returns nothing     
        endmethod
    endstruct

endlibrary
 

Attachments

Status
Not open for further replies.
Back
Top