Legacy GM custom grid collision detection

K

kaito

Guest
this is what i have so far.
Code:
//----CREATE----//

w[0,0] = 0   w[0,1] = 7
w[1,0] = 1   w[1,1] = 7
w[2,0] = 1   w[2,1] = 8
w[3,0] = 1   w[3,1] = 9
w[4,0] = 1   w[4,1] = 10
w[5,0] = 2   w[5,1] = 10
w[6,0] = 3   w[6,1] = 10
w[7,0] = 4   w[7,1] = 10
w[8,0] = 5   w[8,1] = 10
w[9,0] = 6   w[9,1] = 10
w[10,0] = 7  w[10,1] = 10
w[11,0] = 8  w[11,1] = 10
w[12,0] = 9  w[12,1] = 10
w[13,0] = 10 w[13,1] = 10
w[14,0] = 11 w[14,1] = 10
w[15,0] = 11 w[15,1] = 9
w[16,0] = 12 w[16,1] = 9
w[17,0] = 13 w[17,1] = 9
w[18,0] = 13 w[18,1] = 8
w[19,0] = 14 w[19,1] = 8
w[20,0] = 15 w[20,1] = 8
Code:
//----STEP----//

x += keyboard_check(vk_right)-keyboard_check(vk_left)
x1 = floor((x-5)/16)     y1 = floor((y-5)/16)
x2 = floor((x-5)/16)     y2 = floor(ceil(y+4)/16)
x3 = floor(ceil(x+4)/16) y3 = floor((y-5)/16)
x4 = floor(ceil(x+4)/16) y4 = floor(ceil(y+4)/16)

i = 0
while i < 21{
if (w[i,0] = x1 and w[i,1] = y1)
or (w[i,0] = x2 and w[i,1] = y2) {x = w[i,0]*16+21 break}
if (w[i,0] = x3 and w[i,1] = y3)
or (w[i,0] = x4 and w[i,1] = y4) {x = w[i,0]*16-5 break}
i += 1}

y += keyboard_check(vk_down)-keyboard_check(vk_up)
x1 = floor((x-5)/16)     y1 = floor((y-5)/16)
x2 = floor((x-5)/16)     y2 = floor(ceil(y+4)/16)
x3 = floor(ceil(x+4)/16) y3 = floor((y-5)/16)
x4 = floor(ceil(x+4)/16) y4 = floor(ceil(y+4)/16)

i = 0
while i < 21{
if (w[i,0] = x1 and w[i,1] = y1)
or (w[i,0] = x3 and w[i,1] = y3) {y = w[i,1]*16+21 break}
if (w[i,0] = x2 and w[i,1] = y2)
or (w[i,0] = x4 and w[i,1] = y4) {y = w[i,1]*16-5 break}
i += 1}
Code:
//----DRAW----//

i = 0
while i < 21{
draw_sprite(spr_wall,0,w[i,0]*16,w[i,1]*16)
i += 1}
c0.PNG
i am interested on how can i improve this.
maybe there is a better way to store x,y of walls or something?
with this code it's gonna be kinda slow if there gonna be more walls.
 

2Dcube

Member
You can already do collision detection using rectangles, which should be faster / easier.
Why would you use a grid in stead? I think you are making things more complicated than necessary.
 
K

kaito

Guest
@2Dcube
collision detection using rectangles
do you mean place_meeting?
can you point out what it does that makes it faster?
my guess is that object positions are stored differently or something else?
 
Top