|
Post by aurel on Jul 5, 2023 12:31:05 GMT -6
Hi guys I have problem with calculation of x,y positions of spriteson first looks simple but i simply stuck with proper formula my sprites grid is with 360 cells splitted into 15 colons each colon have 24 cell any help? grid is this:
|
|
|
Post by n00b on Jul 5, 2023 14:44:19 GMT -6
To move a sprite in a grid you just increase x or y by the size of the grid.
|
|
|
Post by aurel on Jul 5, 2023 17:11:59 GMT -6
I need to extract x and y position of grid cell in my case grid have 360 cells
|
|
|
Post by n00b on Jul 5, 2023 18:47:39 GMT -6
Use this formula:
'vx and vy could be mouse position, sprite position, or any position on the screen
grid_cell_x = int( vx / grid_cell_width ) * grid_cell_width grid_cell_y = int( vy / grid_cell_height ) * grid_cell_height
In this example int( vx / grid_cell_width ) will return the column and int( vy / grid_cell_height ) will return the row.
|
|
|
Post by aurel on Jul 6, 2023 0:19:30 GMT -6
hi nOOb
and thanks ..that is really a way to use it with column and rows
BUT
i have one array with 360 cells which i filled at start with 0 and 1
with this loop:
infunc fillPlants() 'fill plants array with rand 0 or 1 i=1 while i < 361 plants[i] = rand(1) i = i + 1 wend sert code here then i need to put sprite in cell or array index which have value 1 so i think that i cannot use column and rows or maybe ..there is a better way to do this ?
|
|
|
Post by aurel on Jul 6, 2023 7:57:34 GMT -6
oh boy i add to much sprites it seems that i need better sheme or data section for background sprites ..what you think? even there are over 150 back sprites seems that micro(A) execute it fine ..any suggestion is welcome! i bet that similar program can be made in RCBaisc with less problems
just to show updateSprite routine( it is simple..i think) func updatePlants() i=1 : pcx = 0 : pcy = 0 while i < 361 'cell = i if plants[i] = 1 ShowImgT img4, pcx, pcy, 32, 32 endif if plants[i] = 0 print pcx,pcy,"0" endif
pcx = pcx + 32 if i = 24 : pcy = pcy + 32 :pcx = 0 : endif if i = 48 : pcy = pcy + 32 :pcx = 0 : endif if i = 72 : pcy = pcy + 32 :pcx = 0 : endif if i = 96 : pcy = pcy + 32 :pcx = 0 : endif if i = 120 : pcy = pcy + 32 :pcx = 0 : endif if i = 144 : pcy = pcy + 32 :pcx = 0 : endif if i = 168 : pcy = pcy + 32 :pcx = 0 : endif if i = 192 : pcy = pcy + 32 :pcx = 0 : endif if i = 216 : pcy = pcy + 32 :pcx = 0 : endif if i = 240 : pcy = pcy + 32 :pcx = 0 : endif if i = 264 : pcy = pcy + 32 :pcx = 0 : endif if i = 288 : pcy = pcy + 32 :pcx = 0 : endif if i = 312 : pcy = pcy + 32 :pcx = 0 : endif if i = 336 : pcy = pcy + 32 :pcx = 0 : endif i = i + 1 wend endfn
|
|
|
Post by n00b on Jul 6, 2023 8:50:16 GMT -6
Why don't you just check if i is a multiple of 24. You can do this with a modulus operation. I don't know if microA has modulus but if it doesn't you could replicate it with this:
if (i/24)*24 = i : pcy = pcy + 32 :pcx = 0 : endif
That way you only need 1 IF statement.
|
|
|
Post by aurel on Jul 6, 2023 10:09:07 GMT -6
That way you only need 1 IF statement. One IF.. are you sure ..well i don't know that .. yes micro(A) have modulus % and integer division \
but
i am not sure that i can check expression after IF i think that must be variable only ok i will try !
|
|
|
Post by aurel on Jul 6, 2023 10:24:28 GMT -6
I can't use expression after IF because i check it as error(silly...?) but i use cell to add this checking and with modulus operator work WOW and work really fast n00b ,,you really know the math i am not very well with math and game mechanic at all(shame on me ...) now code is this : func updatePlants() i=1 : pcx = 0 : pcy = 0 while i < 361 plants[1] = 0 if plants[i] = 1 'show plant sprite ShowImgT img4, pcx, pcy, 32, 32 endif ' if plants[i] = 0 ' print pcx,pcy,"0" ' endif
pcx = pcx + 32 'modulus operator work cell = (i % 24)*24 if cell = i : pcy = pcy + 32 :pcx = 0 : endif 'check bush positions if bushCell = i : plants[i] = 0 : endif if bushCell1 = i : plants[i] = 0 : endif if bushCell2 = i : plants[i] = 0 : endif if bushCell3 = i : plants[i] = 0 : endif i = i + 1 wend endfn thanks again !
|
|
|
Post by n00b on Jul 6, 2023 10:54:20 GMT -6
I can't use expression after IF because i check it as error(silly...?) but i use cell to add this checking and with modulus operator work WOW and work really fast n00b ,,you really know the math i am not very well with math and game mechanic at all(shame on me ...) View Attachment now code is this : func updatePlants() i=1 : pcx = 0 : pcy = 0 while i < 361 plants[1] = 0 if plants[i] = 1 'show plant sprite ShowImgT img4, pcx, pcy, 32, 32 endif ' if plants[i] = 0 ' print pcx,pcy,"0" ' endif
pcx = pcx + 32 'modulus operator work cell = (i % 24)*24 if cell = i : pcy = pcy + 32 :pcx = 0 : endif 'check bush positions if bushCell = i : plants[i] = 0 : endif if bushCell1 = i : plants[i] = 0 : endif if bushCell2 = i : plants[i] = 0 : endif if bushCell3 = i : plants[i] = 0 : endif i = i + 1 wend endfn thanks again ! That result will be wrong. What you probably want to do is: cell = int(i / 24)*24 The reason is that modulus will be 0 for multiples of 24. If you want to use modulus you could do this: cell_check = (i % 24) if cell_check = 0 : pcy = pcy + 32 : pcx = 0 : endif They both accomplish the same thing so it really doesn't matter which method you use.
|
|
|
Post by aurel on Jul 6, 2023 11:13:13 GMT -6
I don't know how but work : 'modulus operator work cell = (i % 24)*24 if cell = i : pcy = pcy + 32 :pcx = 0 : endif and really work well, maybe o2 calculate differently or my expression parser work different but above code is fine
|
|