• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Is this the best way of doing bouncing collisons off walls?

hdarren

Member
I have a enemy that bounces around the screen. I have been struggling with getting the bouncing to work when it hits a wall. I want it to bounce away when it hits a wall.

Is this the best way of doing it?

Code:
///move back out of wall
d = point_direction(x,y,xprevious,yprevious);

while place_meeting(x,y,Parent_Wall)
{
  x += lengthdir_x(1,d);
  y += lengthdir_y(1,d);
}

///inverse speed
if (hspeed > 0 and place_meeting(x+1,y,Parent_Wall))
or (hspeed < 0 and place_meeting(x-1,y,Parent_Wall))
{hspeed *= -1;}

if (vspeed > 0 and place_meeting(x,y+1,Parent_Wall))
or (vspeed < 0 and place_meeting(x,y-1,Parent_Wall))
{vspeed *= -1;}
 

NightFrost

Member
Yes, reversing speed is probably the best way to go about it. Not completely accurate where positions are concerned, but nobody will notice. (If you really want to nitpick about it, you calculate the collision position and reverse speed; or maybe even take the remainder of the speed after collision and move the object to the new direction, so movement over time stays completely accurate the whole time.)
 
Top