some math help^^

darkbeer

Beer is Good!
Reaction score
84
Hey,
I just wanted to make my own jump system, so i tried to code it, but unfortunately i don't get how to calculate the height of a unit while its jumping.

Heres the code I use:

JASS:
library JumpSystem
    
    globals
        integer JumpTotal = 0
        timer JumpTimer = CreateTimer()
        JumpStruct array JumpAr
    endglobals
    
    struct JumpStruct
        unit u
        real dist
        real x
        real speed
        real height
        real angle
        location target
        boolean destroypls
    endstruct        
    
    constant function GetJumpPeriode takes nothing returns real
        return 0.03
    endfunction
    
    function EndJump takes unit u returns nothing
        call SetUnitPathing(u, true)
        call UnitRemoveAbility(u, 'Amrf')
    endfunction
    
    function JumpLoop takes nothing returns nothing
        local JumpStruct dat = JumpStruct.create()
        local integer i = 0
        local location loc
        local location Offset
        local real height
        local real PosZ
        
        call DisplayTextToForce(GetPlayersAll(), "aa")
        
        loop
            exitwhen i == JumpTotal
            set dat = JumpAr<i>
            if dat.destroypls == true then
                call dat.destroy()
                set JumpAr<i> = JumpAr[JumpTotal - 1]
                set JumpTotal = JumpTotal - 1
                set i = i - 1
            else
                set loc = GetUnitLoc(dat.u)
                set Offset = PolarProjectionBJ(loc, dat.speed, dat.angle)
                call SetUnitPositionLoc(dat.u, Offset)
                set PosZ = GetLocationZ(Offset)
                if dat.x &lt;= dat.dist/2 then
                set height =( SquareRoot( dat.height * dat.height - (dat.dist/2 - dat.x) * (dat.dist/2 - dat.x))) //these are the things that give me problems^^
                else
                set height = ( SquareRoot( dat.height * dat.height - (dat.x - dat.dist/2) * (dat.x - dat.dist/2))) //these are the things that give me problems^^
                endif
                call SetUnitFlyHeight(dat.u, height, 0 )
                set dat.x = dat.x + dat.speed
                if dat.x &gt;= dat.dist then
                    set dat.destroypls = true
                    call EndJump(dat.u)
                endif
                call RemoveLocation(Offset)
                set Offset = null
            endif
            set i = i + 1
        endloop
        
        if JumpTotal == 0 then
            call PauseTimer(JumpTimer)
        endif
        
    endfunction
    
    function StartJump takes unit u, real height, real time, location target returns nothing
        local location loc = GetUnitLoc(u)
        local real dist = DistanceBetweenPoints(loc, target)
        local JumpStruct dat = JumpStruct.create()
        
        set dat.target = target
        set dat.u = u
        set dat.dist = dist
        set dat.x = 0
        set dat.speed = dist / time * GetJumpPeriode()
        set dat.height = height
        set dat.destroypls = false
        set dat.angle = AngleBetweenPoints(loc, target)
        
        set JumpAr[JumpTotal] = dat
        
        call SetUnitPathing(u, false)
        call UnitAddAbility(u, &#039;Amrf&#039;)
        
        if JumpTotal == 0 then
            call TimerStart(JumpTimer, GetJumpPeriode(), true, function JumpLoop)
        endif
        
        set JumpTotal = JumpTotal + 1
        
        call RemoveLocation(loc)
        set loc = null
    endfunction
endlibrary

function Trig_Jump_System_Actions takes nothing returns nothing
endfunction

//===========================================================================
function InitTrig_Jump_System takes nothing returns nothing
    local trigger t = CreateTrigger(  )
    call TriggerAddAction( gg_trg_Jump_System, function Trig_Jump_System_Actions )
    set t = null
endfunction
</i></i>


Hope someone got any ideas about it, ill also try to make it dynamic so it the height will adept to the ground level when jumping over a canyon or sth. like that.

thankfull for any suggestions
 
Reaction score
333
Motion with constant acceleration:

set y = v0*t + 0.5*a*t*t + y0

y is the position of the body.
y0 is the body's initial position.
t is the time passed.
a is the acceleration (gravity, in this case).
v is the current velocity (not used yet).

We can assume t is always 1 if you use y instead of y0 as initial position and v instead of v0. You get, roughly:

set y = v + 0.5*a + y
set v = v + a
 

darkbeer

Beer is Good!
Reaction score
84
Thx,
kind of stupid to make it look a circle instead of using physik^^, so i tried to make itusing the constant acceleration, i thought that is the formula:

s = v(0) * t - 0.5 * a * t ^ 2

hm v(0) should be SquareRoot(2 * a * s) or am i wrong?

so i made a new code using that formula, but it doesnt work properly since the unit flys higher and higher instead of falling down -.-

heres new code:

JASS:
library JumpSystem
    
    globals
        real gravity = 10
        integer JumpTotal = 0
        timer JumpTimer = CreateTimer()
        JumpStruct array JumpAr
    endglobals
    
    struct JumpStruct
        real totaltime
        real time
        unit u
        real dist
        real x
        real speed
        real height
        real angle
        location target
        boolean destroypls
    endstruct        
    
    constant function GetJumpPeriode takes nothing returns real
        return 0.03
    endfunction
    
    function EndJump takes unit u returns nothing
        call SetUnitPathing(u, true)
        call UnitRemoveAbility(u, &#039;Amrf&#039;)
    endfunction
    
    function JumpLoop takes nothing returns nothing
        local JumpStruct dat = JumpStruct.create()
        local integer i = 0
        local location loc
        local location Offset
        local real height
        local real PosZ
        local real v0
        
        call DisplayTextToForce(GetPlayersAll(), &quot;aa&quot;)
        
        loop
            exitwhen i == JumpTotal
            set dat = JumpAr<i>
            if dat.destroypls == true then
                call dat.destroy()
                set JumpAr<i> = JumpAr[JumpTotal - 1]
                set JumpTotal = JumpTotal - 1
                set i = i - 1
            else
                set loc = GetUnitLoc(dat.u)
                set dat.time = dat.time + GetJumpPeriode()
                set Offset = PolarProjectionBJ(loc, dat.speed, dat.angle)
                call SetUnitPositionLoc(dat.u, Offset)
                set PosZ = GetLocationZ(Offset)
                set v0 = SquareRoot(2 * gravity * dat.height) // &lt;-- somewhere here is the problem
                set height = v0 * dat.time - 0.5 * gravity * dat.time // &lt;-- somewhere here is the problem
                call BJDebugMsg(R2S(v0 * dat.time))
                call BJDebugMsg(R2S(0.5 * gravity * dat.time * dat.time))
                call SetUnitFlyHeight(dat.u, height, 0 )
                set dat.x = dat.x + dat.speed
                if dat.x &gt;= dat.dist then
                    set dat.destroypls = true
                    call EndJump(dat.u)
                endif
                call RemoveLocation(Offset)
                set Offset = null
            endif
            set i = i + 1
        endloop
        
        if JumpTotal == 0 then
            call PauseTimer(JumpTimer)
        endif
        
    endfunction
    
    function StartJump takes unit u, real height, real time, location target returns nothing
        local location loc = GetUnitLoc(u)
        local real dist = DistanceBetweenPoints(loc, target)
        local JumpStruct dat = JumpStruct.create()
        
        set dat.target = target
        set dat.u = u
        set dat.dist = dist
        set dat.x = 0
        set dat.speed = dist / time * GetJumpPeriode()
        set dat.height = height
        set dat.destroypls = false
        set dat.angle = AngleBetweenPoints(loc, target)
        set dat.time = 0
        set dat.totaltime = time
        
        set JumpAr[JumpTotal] = dat
        
        call SetUnitPathing(u, false)
        call UnitAddAbility(u, &#039;Amrf&#039;)
        
        if JumpTotal == 0 then
            call TimerStart(JumpTimer, GetJumpPeriode(), true, function JumpLoop)
        endif
        
        set JumpTotal = JumpTotal + 1
        
        call RemoveLocation(loc)
        set loc = null
    endfunction
endlibrary

function Trig_Jump_System_Actions takes nothing returns nothing
endfunction

//===========================================================================
function InitTrig_Jump_System takes nothing returns nothing
    local trigger t = CreateTrigger(  )
    call TriggerAddAction( gg_trg_Jump_System, function Trig_Jump_System_Actions )
    set t = null
endfunction

</i></i>


and well, i dont know what to do with a, in real life its the gravity, so i tryed to set it to a constant as well^^
 
Reaction score
333
You don't need to be able to calculate v0, as you will not be using that equation.

Here is the basic calculation for height (which is y) and v (which is velocity), per firing of the timer. You would set v to the force of the jump in your "StartJump" function.

JASS:
set y = v + 0.5*a + y
set v = v + a


I have made a demo map for you to look at, it is attached to this post.
 

Attachments

  • Jump.w3x
    10.4 KB · Views: 86

darkbeer

Beer is Good!
Reaction score
84
First of all thanks for your effort,

unfortunately your formula isnt the thing i was searching for, i want to make my jump spell depending on time and distance, so i can change them^^

I already did the normal movement, i just need to calculate the current height depending on the time.

therefore i thought ill isolate g in this formula:

g = gravity acceleration
v(0) = z start speed
t = total time
h = jump height

2h = v(0) * t - 0.5 * g * t ^ 2 // t and h are given, t is the total time

so it is: g = (2 * v(0) * t - 4 * h)/ t ^2

now i just need to get v(0)

but i dont get how yet^^, heres the a second formula for v:
v = v(0) - g*t, // now at the highest point, v should be zero and t = half the total time required for the jump.

So v(0) should be g * t / 2, as Damien said.

So v(0) = h / t, and g = -2 * h / t ^2

But that doesnt work, can anyone tell me where the mistake is?
 
Reaction score
333
t = duration of jump
a = acceleration (gravity)
dist = distance of jump
deg = direction of jump in degrees

The initial z velocity of the jump is t*(a/-2) (this does not take into account height variation).

The x velocity during the jump is SinBJ(deg)*dist/t

The y velocity during the jump is CosBJ(deg)*dist/t
 

darkbeer

Beer is Good!
Reaction score
84
Hm, if this keeps on i will go crazy -.-,

i just can't find my mistake: [Edit: Found it :)]

here are my formulas:

Given:
H is the maximum height the unit reaches during its jump.
T is the total time required for the jump

Formulas:
h(t) = v(0) * t - 0.5 * g * t ^ 2
v = v(0) - g * t

h(t) will be z of unit
t is the current time of the jump

Required:
v(0) initial speed
g: gravity in real life

h(t) = v(0) * t - 0.5 * g * t ^ 2
v = v(0) - g * t

Now: the highest point should be when t = T / 2
and v there must be 0

So it is v(0) = g * T/2.

So we get: h(t) = g * T/2 * t - g/2 * t ^ 2

okay the highest point should be at half of the total time:

H = g * T/ 2 * T/ 2 - g / 2 * T/2 * T/2

-> H = g * T ^ 2 / 4 - g * T ^ 2 / 8

-> H = g * T ^ 2 / 8

-> g = 8 * H / T ^ 2

-> v(0) = 4 * H / T

-.- Calculation error from me, works now on same ground level :)

Now i want to implent the different terrain heights, so units can jump up cliffs and over canyons.

any suggestions how to make it look real?
 
Reaction score
333
You don't need to know the height halfway through the jump. The demo map I uploaded has jumping over different terrain heights and a much simpler implementation.
 

darkbeer

Beer is Good!
Reaction score
84
Allright, thx a lot damien

Allthought i used my own formula in the end, i know yours is much simpler and probably better^^
but i just wanted to do it in my own way, using physik from school xD :)

heres my temporary code: (If anyone is interested in it^^) // implents jumping over different terrain heights
JASS:
library JumpSystem
    globals
        private integer JumpTotal = 0
        private timer JumpTimer = CreateTimer()
        private JumpStruct array JumpAr
    endglobals
    
    struct JumpStruct
        real totaltime
        real time
        unit u
        real speed
        real height
        real vz
        real vecz
        real g
        real z
        real angle
        boolean destroypls
    endstruct        
    
    private constant function GetJumpPeriode takes nothing returns real
        return 0.03
    endfunction
    
    private function EndJump takes unit u returns nothing
        call SetUnitPathing(u, true)
        call SetUnitFlyHeight(u, 0, 0)
    endfunction
    
    function JumpLoop takes nothing returns nothing
        local JumpStruct dat = JumpStruct.create()
        local integer i = 0
        local location loc
        local location Offset
        local real height
        local real PosZ
        
        loop
            exitwhen i == JumpTotal
            set dat = JumpAr<i>
            if dat.destroypls == true then
                call dat.destroy()
                set JumpAr<i> = JumpAr[JumpTotal - 1]
                set JumpTotal = JumpTotal - 1
                set i = i - 1
            else
                set loc = GetUnitLoc(dat.u)
                set dat.time = dat.time + GetJumpPeriode()
                set Offset = PolarProjectionBJ(loc, dat.speed, dat.angle)
                call SetUnitPositionLoc(dat.u, Offset)
                set PosZ = GetLocationZ(Offset)
                set height = dat.z + dat.vz * dat.time - 0.5 * dat.g * dat.time * dat.time - PosZ - dat.vecz * dat.time * dat.time
                call SetUnitFlyHeight(dat.u, height, 0 )
                if dat.time &gt;= dat.totaltime then
                    set dat.destroypls = true
                    call EndJump(dat.u)
                endif
                call RemoveLocation(Offset)
                set Offset = null
            endif
            set i = i + 1
        endloop
        
        if JumpTotal == 0 then
            call PauseTimer(JumpTimer)
        endif
        
    endfunction
    
    function StartJump takes unit u, real height, real time, location target returns nothing
        local location loc = GetUnitLoc(u)
        local real dist = DistanceBetweenPoints(loc, target)
        local JumpStruct dat = JumpStruct.create()
        local real vz
        
        set dat.u = u
        set dat.z = GetLocationZ(loc)
        set dat.vecz = (dat.z - GetLocationZ(target)) / time / time
        set dat.vz = 4 * height / time
        set dat.g = 8 * height / time / time
        set dat.speed = dist / time * GetJumpPeriode()
        set dat.height = height
        set dat.destroypls = false
        set dat.angle = AngleBetweenPoints(loc, target)
        set dat.time = 0
        set dat.totaltime = time
        
        set JumpAr[JumpTotal] = dat
        
        call SetUnitPathing(u, false)
        call UnitAddAbility(u, &#039;Amrf&#039;)
        call SetUnitFlyHeight(u, 0.01, 0)
        call UnitRemoveAbility(u, &#039;Amrf&#039;)
        
        if JumpTotal == 0 then
            call TimerStart(JumpTimer, GetJumpPeriode(), true, function JumpLoop)
        endif
        
        set JumpTotal = JumpTotal + 1
        
        call RemoveLocation(loc)
        set loc = null
    endfunction
endlibrary

//===========================================================================
function InitTrig_Jump_System takes nothing returns nothing
endfunction

</i></i>


guess i will remove the locations as far as possible and make a few little changes, but it works^^

THX @ damien
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top