Small tips for store coordinats, eg map item, buttons...

General discussion about LÖVE, Lua, game development, puns, and unicorns.
DIMMaX
Prole
Posts: 13
Joined: Sat May 06, 2017 9:03 am

Small tips for store coordinats, eg map item, buttons...

Post by DIMMaX »

Small tip for store coordinats, eg map item, buttons, entity positions(player, enemy)

typical use - testtable[id].x and testtable[id].y, for store, but...
testtable - 3000000 elements
testtable[id] = {["x"] = x, ["y"] = y} -- generation 1.4 sec for 3000000 elements and use 267390 - mem in kb
Usually you not need X pos - 123456789.123456789 coordinats, usually need X - 100, or 812, not 0.00334 or 123456789 of x coordinat of button, or eg.
testtable[id] = x + (y / 100000000) -- 0.370 \ 33014 -- it use 33mb !!! and create 0.37 sec, for 3000000 !!! elements/
Then, use -- local x, y = math.modf( testtable[id] ) -- it will fast(or faster?), as get testtable[id].x and testtable[id].y and fast make x1+x2, y1+y2, example -- in gui - need button position + parent window position , button.x = window.x + button_padding.x; button.y = window.y + button_padding.y
if use - pos = x + (y / 100000000)
------ button.pos = window.pos + button_padding -- it will faster

if need x, y = -100 , -200 coordinat, you can use 10000 or 100000, or .... for 0 position, example 10000 - x=0, 10012 - x=12, 99985 - x= -15

P.S. , x * 100000000 + y + (z / 100000000) -- in my project, not need "double" or "int32", but need "+" and "-" for many vec3 (speed and mem hack)
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Small tips for store coordinats, eg map item, buttons...

Post by zorg »

It's not like i'm your typesetting teacher, but holy bananas, batman... the formatting doesn't help either.

But i think i understood and can parse your text for you, so that others can benefit.


Ladies and gents!

You can save space for 2-dimensional coordinates if you don't want fractional positioning, by combining the two coordinate values as integers, one converted to a decimal strictly less than one.

Example:

Code: Select all

co = 123.456 -- Where 123 is x, 456 is y.
Getting/Setting them isn't hard either:

Code: Select all

coordinates = x + (y<0 and -1 or 1) * ((y == 0) and 0 or ((math.abs(y) / math.log10(math.abs(y)))))
x = math.floor(co)
y = math.abs(co) * ....
Oh wait, maybe you can't decide the magnitude of the second number that resides in the fraction dynamically... oh well.


So naturally, the ever-present tradeoff between space and speed is applicable here as well.
Last edited by zorg on Sun May 07, 2017 2:17 am, edited 2 times in total.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm
Contact:

Re: Small tips for store coordinats, eg map item, buttons...

Post by Sir_Silver »

Image
DIMMaX
Prole
Posts: 13
Joined: Sat May 06, 2017 9:03 am

Re: Small tips for store coordinats, eg map item, buttons...

Post by DIMMaX »

Hm....., before, i used
pos = x*100000000 + y
map not bigger than 99999999x99999999 titles
for items on map Is enough...
100000001 - [x = 1, y = 1]; and 99999999 99999999 - [x = 99999999 , y = 99999999 ]
y = math.fmod ( pos, 100000000 )
x = math.floor ( pos / 100000000 )

... but this, faster:

Code: Select all

pos = 3 + (52 / 1000)
tx, ty = math.modf(pos)
print("x: "..tx.."  y: "..ty*1000)
output: x: 3  y: 52
than

Code: Select all

pos = {3, 52}
Last edited by DIMMaX on Sun May 07, 2017 3:51 am, edited 1 time in total.
User avatar
peterrust
Prole
Posts: 42
Joined: Thu Dec 29, 2016 8:49 pm
Location: Bellingham, WA, USA
Contact:

Re: Small tips for store coordinats, eg map item, buttons...

Post by peterrust »

zorg wrote: Sun May 07, 2017 2:16 am the ever-present tradeoff between space and speed is applicable here as well
An alternative method that should require less of a tradeoff is using C data structures, via luajit's FFI module. Objects with x and y properties could be defined as a C struct like this:

Code: Select all

local ffi = require("ffi")
ffi.cdef[[
typedef struct { uint8_t x, y; } position;
]]

local obj = ffi.new("position", x, y)
-- do something with obj.x and obj.y
(the above code is untested, I would love to hear from others who have actual experience w/ FFI c data structures)

uint8_t is only 1 byte, so it will only allow a range of 0-255. You may prefer uint16_t (0-32,767), or uint32_t (0-4,294,967,295). Or the signed version of each (int8_t, int16_t, int32_t) which has the same range but splits it between negative and positive values (i.e. int8_t goes from -128 to 127).

Regular Lua numbers are 64-bit, so one regular Lua number uses the same amount of space as two 32-bit integers.
User avatar
peterrust
Prole
Posts: 42
Joined: Thu Dec 29, 2016 8:49 pm
Location: Bellingham, WA, USA
Contact:

Re: Small tips for store coordinats, eg map item, buttons...

Post by peterrust »

I meant to include a link to the luajit docs for c data structures in FFI: http://luajit.org/ext_ffi.html#cdata
DIMMaX
Prole
Posts: 13
Joined: Sat May 06, 2017 9:03 am

Re: Small tips for store coordinats, eg map item, buttons...

Post by DIMMaX »

I use FFI... it powerfull...
C struct = 0.84 sec and 62mb ||| 1 value = 0.37 sec and 33mb ||| {x, y} = 1.35 sec and 197mb
User avatar
peterrust
Prole
Posts: 42
Joined: Thu Dec 29, 2016 8:49 pm
Location: Bellingham, WA, USA
Contact:

Re: Small tips for store coordinats, eg map item, buttons...

Post by peterrust »

DIMMaX wrote: Sun May 07, 2017 4:00 am C struct = 0.84 sec and 62mb ||| 1 value = 0.37 sec and 33mb ||| {x, y} = 1.35 sec and 197mb
Interesting! What data type did you use, uint8? uint16? uint32?

I suppose the struct would require at least one pointer/address more than a simple value, which would add an additional 32-bits or 64-bits for each item...
DIMMaX
Prole
Posts: 13
Joined: Sat May 06, 2017 9:03 am

Re: Small tips for store coordinats, eg map item, buttons...

Post by DIMMaX »

int8_t - 62mb
int32_t, 78mb
16mb different not critical at 3000000 elements
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Small tips for store coordinats, eg map item, buttons...

Post by zorg »

No one asked the most critical question yet... exactly why do you need to keep 3 million anything in memory at one time? :3
I mean, from the standpoint of a project being a released game or whatever...
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests