Ben0981 [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=69706 SOLVED Collision detection notworking <p>Hi I'm currently working on my first project in pico-8 and I loosely followed a tutorial on collision detection and I wrote this function </p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre>function colliderect(x1,y1,w1,h1,x2,y2,w2,h2) xs = w1/2 + w2/2 ys = h1/2 + h2/2 xd = sqrt((x1)^2+(x2)^2) yd = sqrt((y1)^2+(y2)^2) if ((xs &gt; xd and ys &gt; yd)) then return false //colliding else return true //not colliding end end</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>which takes the x and y coordinates and the width and height of two objects and calculates if they collide. The function itself seems to work as it should and gives the correct result when manually inputting the data but when I implement it like this</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre>function update_player() for i in all(bullets) do if (not colliderect(player.x,player.y,player.w,player.h,i.x,i.y,i.w,i.h)) then sfx(1) end end end</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>it doesn't work anymore. Even if I am standing right on the bullet it doesn't play the sound it should when colliding (and yes I did check if it's the right SFX). I am also not getting any errors when running the program so I don't really know what's wrong. Any help would be very much appreciated. </p> <p>Figured out what's wrong already I had a small mistake when calculating xd and yd that is I forgot to add the width and height to them when calculating them. This is how it should have been.</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre>xd = abs((x1+(w1/2))-(x2+(w2/2))) //also learned that an abs function exists for pico yd = abs((y1+(h1/2))-(y2+(h2/2)))</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> https://www.lexaloffle.com/bbs/?tid=49414 https://www.lexaloffle.com/bbs/?tid=49414 Tue, 20 Sep 2022 19:08:06 UTC