Terrain (physics/normal system) struct leaking problem

Prozix

New Member
Reaction score
7
my code

JASS:
library ExtraPhysicsFunctions
function GetTerrainZ  takes real x, real y returns real z
    local unit u = CreateUnit(Player(12),'hnth', x, y, 0)
    local location l = GetUnitLoc(u)
    local real r = GetLocationZ(l)
    call RemoveLocation(l)
    call RemoveUnit(u)
    return r
endfunction
endlibrary

JASS:
library Physics initializer Init requires ExtraPhysicsFunctions
//--------------------------------------------------------
globals
    private constant real STANDARD_MASS = 10.0
    private constant real GRAVITY_ACCELERATION = 9.81
    private constant real FRICTION = 0.98
    
    private constant integer ORDER_RIGHT_CLICK = 851971
    private constant integer ORDER_MOVE = 851986
    private constant integer ORDER_ATTACK_MOVE = 851983
endglobals
//--------------------------------------------------------
function IsMoveOrder takes integer order returns boolean
    return order==ORDER_RIGHT_CLICK or order==ORDER_MOVE or order==ORDER_ATTACK_MOVE
endfunction

struct vec
    real x
    real y
    real z
    static method Create takes nothing returns vec
        local vec v = vec.allocate()
        call v.SetXYZ(0,0,0)
        return v
    endmethod
    //SET-------------------------------------
    method SetByVec takes vec v returns nothing
        set .x = v.x
        set .y = v.y
        set .z = v.z
    endmethod
    method SetXYAutoZ takes real x, real y returns nothing
        set .x = x
        set .y = y
        set .z = GetTerrainZ(x, y)
    endmethod
    method SetXYZ takes real x, real y, real z returns nothing
        set .x = x
        set .y = y
        set .z = z
    endmethod
    //OPERATORS-------------------------------------
    method Add takes vec v returns nothing
        set .x = .x + v.x
        set .y = .y + v.y
        set .z = .z + v.z
    endmethod
    method Substract takes vec v returns nothing
        set .x = .x - v.x
        set .y = .y - v.y
        set .z = .z - v.z
    endmethod
    method MultiplyByVec takes vec v returns nothing
        set .x = .x * v.x
        set .y = .y * v.y
        set .z = .z * v.z
    endmethod
    method MultiplyByReal takes real r returns nothing
        set .x = .x * r
        set .y = .y * r
        set .z = .z * r
    endmethod
    //MISC--------------------------------------------
    static method CrossProduct takes vec v1, vec v2 returns vec
        local vec v = vec.create()
        call v.SetXYZ((v1.y*v2.z - v1.z*v2.y), (v1.z*v2.x - v1.x*v2.z), (v1.x*v2.y - v1.y*v2.x))
        return v
    endmethod
    method Print takes nothing returns nothing
        call BJDebugMsg("vec(" + R2S(.x) + "; " + R2S(.y) + "; " + R2S(.z) + ")")
    endmethod
    method AngleWithXY takes nothing returns real
        local real alpha
        local real ANS
        //calculate alpha
        set ANS = SquareRoot(.x*.x + .y*.y)
        if ANS == 0.0 then
            set alpha = bj_PI/2
        else
            set alpha = Atan(.z/ANS)
        endif
        return alpha
    endmethod
    method Size takes nothing returns real
        return SquareRoot(.x*.x + .y*.y + .z*.z)
    endmethod
endstruct

globals
    vec uVel
endglobals
//--------------------------------------------------------
private function Update takes nothing returns nothing
    local unit u = gg_unit_Hblm_0001
    
    local real alpha = 0.0 //angle between surface and z=0
    
    local real oX = GetUnitX(u)
    local real oY = GetUnitY(u)
    local real Fz = STANDARD_MASS * GRAVITY_ACCELERATION
    
    //points
    local vec p1 = vec.Create()
    local vec p2 = vec.Create()
    local vec p3 = vec.Create()
    //face vectors
    local vec v1 = vec.Create()
    local vec v2 = vec.Create()
    //normal
    local vec n
    local vec a = vec.Create()
    
    //START doing things
    call p1.SetXYAutoZ(oX + 10, oY)
    call p2.SetXYAutoZ(oX - 8, oY+6)
    call p3.SetXYAutoZ(oX - 8, oY-6)

    call v1.SetXYZ(p2.x-p1.x, p2.y-p1.y, p2.z-p1.z)
    call v2.SetXYZ(p3.x-p1.x, p3.y-p1.y, p3.z-p1.z)
    
    set n = vec.CrossProduct(v1, v2) //point perpendicular to v1 and v2
    
    set alpha = bj_PI/2 - n.AngleWithXY() //2PI(triangle)-PI/2-angle of the normal = angle of the surface with z=0
    
    call a.SetByVec(n)
    call a.MultiplyByReal(Fz*Cos(alpha)/n.Size())
    
    //call a.Print()
    
    call uVel.Add(a)
    call uVel.MultiplyByReal(FRICTION)
    
    if IsMoveOrder(GetUnitCurrentOrder(u)) or uVel.Size() < 1 then
        call uVel.SetXYZ(0,0,0)
    else
        set oX = oX+uVel.x/20
        set oY = oY+uVel.y/20
        //map bounds. can be done better, this is a temporary solution
        if oX > GetRectMaxX(bj_mapInitialPlayableArea)-64 then
            set oX = GetRectMaxX(bj_mapInitialPlayableArea)-64
            if  uVel.x > 0 then
                set uVel.x = -uVel.x
            endif
        elseif oX < GetRectMinX(bj_mapInitialPlayableArea)+64 then
            set oX = GetRectMinX(bj_mapInitialPlayableArea)+64
            if  uVel.x < 0 then
                set uVel.x = -uVel.x
            endif
        endif
        if oY > GetRectMaxY(bj_mapInitialPlayableArea)-64 then
            set oY = GetRectMaxY(bj_mapInitialPlayableArea)-64
            if  uVel.y > 0 then
                set uVel.y = -uVel.y
            endif
        elseif oY < GetRectMinY(bj_mapInitialPlayableArea)+64 then
            set oY = GetRectMinY(bj_mapInitialPlayableArea)+64
            if  uVel.y < 0 then
                set uVel.y = -uVel.y
            endif
        endif
        call SetUnitX(u, oX)
        call SetUnitY(u, oY)
        call SetUnitFacing(u, Rad2Deg(Atan2(uVel.y,uVel.x)))
    endif
    
    //cleanup
    call p1.destroy()
    call p2.destroy()
    call p3.destroy()
    
    call v1.destroy()
    call v2.destroy()
    
    call n.destroy()
    call a.destroy()
endfunction
//--------------------------------------------------------
private function Init takes nothing returns nothing
    local trigger t = CreateTrigger()
    
    call TriggerRegisterTimerEvent(t, 0.03, true)
    call TriggerAddAction(t, function Update)
    
    set uVel = vec.Create()
endfunction
endlibrary
 

aliminator8

New Member
Reaction score
3
(I used your GetTerrainZ function but I used a locust, invunerable, no model, collision size=1, flying, unit to get the Z. It is way more. accurate because it doesn't get displaced. )

Lol great idea. I didn't even think of doing that ;p ,im gonna use that in my map straight away lol...
 

Prozix

New Member
Reaction score
7
the locals are not nulled. Here is a fix which can improve the code's efficiency greatly.
I didn't know a location would take the proper Z coodinate if is moved :O if this works it would be a great improvement indeed.
Do locals require to be nulled even if you are sure you don't use the variables after you have removed/deleted the object it points to?

Lol great idea. I didn't even think of doing that ;p ,im gonna use that in my map straight away lol...
If kingkings code works, you should defenitely use that instead of a unit and everything
 

Prozix

New Member
Reaction score
7
Here's my updated map, with some ingame commands and the code.
 

Attachments

  • Physics0.1.w3x
    37.8 KB · Views: 233
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