ZOSK [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=28373 Multiple objects collision with bounce <p>Hi everyone,<br /> I'm currently working on a game where multiple enemies (cubes) spawn in the corners of the screen, and then move towards the player. I also have what I call patterns, that are large rectangles moving along the screen. So I made this function that make the enemies collide with the pattern and bounce off it.<br /> My problem is that some enemies just go through the pattern, or sometimes the opposite, bounce way too fast.<br /> How would you guys handle stuff 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>-- this is called each frame when updating the enemies -- r.x, r.y, r.w, and r.h are respectively the patterns location, width and height -- dx and dy is the patterns directions -- x,y, and s are respectively the enemies position and size -- vx, and vy are the enemies velocity on each axis if x+vx&lt;r.x+r.w and x+s&gt;r.x+dx and y+s&gt;r.y+dy and y+vy&lt;r.y+r.h then if x+vx&lt;r.x then vx=-abs(vx)*1.15 elseif x+vx+s&gt;r.x+r.w then vx=abs(vx)*1.15 end if y+vy&lt;r.y then vy=-abs(vy)*1.15 elseif y+vy+s&gt;r.y+r.h then vy=abs(vy)*1.15 end end -- this is to try and set a maximum value to the bounce vx=min(abs(vx),bouncecap)*sgn(vx) vy=min(abs(vy),bouncecap)*sgn(vy)</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=34948 https://www.lexaloffle.com/bbs/?tid=34948 Sun, 04 Aug 2019 11:16:26 UTC