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.
My problem is that some enemies just go through the pattern, or sometimes the opposite, bounce way too fast.
How would you guys handle stuff like this ?
-- 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<r.x+r.w and x+s>r.x+dx and y+s>r.y+dy and y+vy<r.y+r.h then if x+vx<r.x then vx=-abs(vx)*1.15 elseif x+vx+s>r.x+r.w then vx=abs(vx)*1.15 end if y+vy<r.y then vy=-abs(vy)*1.15 elseif y+vy+s>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)
[Please log in to post a comment]