aliminator8
New Member
- Reaction score
- 3
Hi guys,
Main Problem...
Creates more (xyz) structs than it destroys.
therefore errors with unable to allocate id to object after count (global integer) reaches 18,000.
Background info and minor problems...
This system i made is meant to simulate a circular object on a slope, so it rolls down in a correct manner.
The dummy unit basically moves in relation to the normal of the slope it is on.
It works but i haven't been able to make the object gain momentum correctly.
I got the Normalization formula from http://www.fullonsoftware.co.uk/snippets/content/Math_-_Calculating_Face_Normals.pdf
i don't fully understand it but seems to be working.
So if anyone has any suggestions about coding and the physics system it would greatly be appreciated.
code below:
Main Problem...
Creates more (xyz) structs than it destroys.
therefore errors with unable to allocate id to object after count (global integer) reaches 18,000.
Background info and minor problems...
This system i made is meant to simulate a circular object on a slope, so it rolls down in a correct manner.
The dummy unit basically moves in relation to the normal of the slope it is on.
It works but i haven't been able to make the object gain momentum correctly.
I got the Normalization formula from http://www.fullonsoftware.co.uk/snippets/content/Math_-_Calculating_Face_Normals.pdf
i don't fully understand it but seems to be working.
So if anyone has any suggestions about coding and the physics system it would greatly be appreciated.
code below:
JASS:
library xyzvectors initializer init
globals
real mass = 0.1
integer count = 0
xyz cvel
real accuracy = 30
endglobals
function counter takes nothing returns nothing
set count = count + 1
call BJDebugMsg(I2S(count))
endfunction
function uncounter takes nothing returns nothing
set count = count - 1
call BJDebugMsg(I2S(count))
endfunction
function GetTerrainZ takes real x, real y returns real z
local unit u = CreateUnit(Player(0),039;hfoo039;, x, y, 0)
local location l = GetUnitLoc(u)
local real r = GetLocationZ(l)
call RemoveLocation(l)
call RemoveUnit(u)
return r
endfunction
struct xyz
//for handing points in 3d (x,y,z)
real x
real y
real z
static method define takes real x, real y returns xyz
local xyz vel = xyz.allocate()
local real z1 = GetTerrainZ(x,y)
call counter()
set vel.x = x
set vel.y = y
set vel.z = z1
return vel
endmethod
method add takes xyz a returns nothing
set this.x = a.x + this.x
set this.y = a.y + this.y
set this.z = a.z + this.z
endmethod
endstruct
function normal takes xyz a, xyz b, xyz c returns xyz
local xyz v1 = xyz.create()
local xyz v2 = xyz.create()
local xyz norm = xyz.create()
call counter()
call counter()
call counter()
set v1.x = b.x - a.x
set v1.y = b.y - a.y
set v1.z = b.z - a.z
set v2.x = c.x - a.x
set v2.y = c.y - a.y
set v2.z = c.z - a.z
set norm.x = (v1.y * v2.z) - (v1.z * v2.y)
set norm.y = -((v2.z * v1.x) - (v2.x * v1.z))
set norm.z = (v1.x * v2.y) - (v1.y * v2.x)
call v1.destroy()
call v2.destroy()
call uncounter()
call uncounter()
return norm
endfunction
function periodic takes nothing returns nothing
local unit u = gg_unit_hfoo_0000 //testing unit
local real x = GetUnitX(u)
local real d // distance
local real y = GetUnitY(u)
local real z = GetLocationZ(GetUnitLoc(u))
local real xy = 0.00 // angle on xy plane
local real xytoz = 0.00 // angle of elevation
local xyz a = xyz.define(x,y+accuracy) //creating 3 vertices at (x+0,y+30),(x+30,y-30) and (x-30,y-30)
local xyz b = xyz.define(x+accuracy,y-accuracy) // to make a triangle
local xyz c = xyz.define(x-accuracy,y-accuracy) // and then find the normal of the triangle
local xyz vel = normal(a,b,c) //calculate normal
call counter()
call counter()
call counter()
set d = SquareRoot((vel.y*vel.y) + (vel.x*vel.x))//set d as distance of the normal on the xy plane.(xy distance or 3d distance?)
set vel.x = vel.x / d //convert to unit vector (i think...?)
set vel.y = vel.y / d
set vel.z = vel.z / d
set xy = Atan2(vel.y, vel.x) //calc angle on xy plane
set xytoz = Atan2(SquareRoot((vel.x*vel.x)+ (vel.y *vel.y)),vel.z)//// angle of elevation
set z = d*Sin(xytoz)
set d = d*Cos(xytoz) // set distance smaller because if elevation = 90 then distance should equal 0
set y = d*Sin(xy)
set x = d*Cos(xy)
//instant velocity
call SetUnitX(u,GetUnitX(u)+(x/333+(accuracy-30)))//instantaneous movement...
call SetUnitY(u,GetUnitY(u)+(y/333+(accuracy-30)))
//continuous velocity
//cvel is global xyz struct.
call cvel.add(vel)//tally current normal with old ones to create continuous velocity.... this effect semi-works but could be improved...
//continuous velocity works so if you just got of a slope and moved onto flat terrain your force will continue to move
//you in the previous direction
set cvel.x = cvel.x / (1+mass)
set cvel.y = cvel.y / (1+mass)
call SetUnitX(u,GetUnitX(u)-(cvel.x/30+(accuracy-30)))
call SetUnitY(u,GetUnitY(u)-(cvel.y/30+(accuracy-30)))
////////////////
call vel.destroy()
call a.destroy()
call b.destroy()
call c.destroy()
call uncounter()
call uncounter()
call uncounter()
call uncounter()
endfunction
function init takes nothing returns nothing
local trigger t = CreateTrigger()
set cvel = xyz.create()
call counter()
call TriggerRegisterTimerEventPeriodic(t,0.03)
call TriggerAddAction(t,function periodic)
endfunction
endlibrary