• 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!

Bouncing Off Wall Grid

R

Robert

Guest
I have a 32x32 grid dungeon and a wall is a single cell, so I have a bunch of walls repeating to create the illusion of a single continuous wall. This has worked just fine for everything else, but when it comes to bouncing objects off them there is a problem. It seems that when the bouncing object hits the edge of two touching walls the resulting bounce angle might be inverted because it hit the corner of the back facing wall and not the corner of the front facing wall.

See the image below to help understand. The green arrow is the angle I want the ball the go, and the yellow arrow is the angle it goes. Im pretty sure it's because it is hitting that corner of wall 1, and not the back corner of wall 2.

 
Last edited by a moderator:
R

Robert

Guest
Alright ignore my original post, I edited it with an updated problem, the original problem I worked out by moving it to a new collision event with the walls, but it's still having problem, as depicted in my updated post.

Thanks
 

Ragster

Member
EDIT: Okay, so basically all you need to figure is out is which direction the ball is hitting the wall from, then inverse either the x speed or the y speed. Here's a topic related to the same issue.
I'm thinking something along the lines of this will work, but I'm not entirely sure... You may have to look into Game Maker Studio's physic's engine to get perfect results.

Try this one out first:
obj_ball COLLISION WITH obj_wall
Code:
var dir;
dir = point_direction(x,y,other.x,other.y);
if (abs(angle_difference(dir,0)) <= 45 or abs(angle_difference(dir,180)) <= 45) hspeed = -hspeed else vspeed = -vspeed;
If that doesn't work, this code is a bit more demanding, but I think it should work:
obj_ball COLLISION WITH obj_wall
Code:
var dir,check;
dir = point_direction(x,y,other.x,other.y);
if (abs(angle_difference(dir,0)) < 45 or abs(angle_difference(dir,180)) < 45) hspeed = -hspeed;
else if (abs(angle_difference(dir,90)) < 45 or abs(angle_difference(dir,270)) < 45) vspeed = -vspeed;
else {
   check = false;
   if (collision_point(x+17,y,obj_wall,false,true) or collision_point(x-17,y,obj_wall,false,true)) {
      hspeed = -hspeed;
      check = true;
   } if (collision_point(x,y+17,obj_wall,false,true) or collision_point(x,y-17,obj_wall,false,true)) {
      vspeed = -vspeed;
      check = true;
   } if (!check) {
      hspeed = -hspeed;
      vspeed = -vspeed;
   }
}
 
Last edited:

lolslayer

Member
You can make a script that calculates the bounce angle on a line, then split the map up in lines for the bounce collisions instead of blocks
 
R

Robert

Guest
Hey guys thanks for the replies. Ragster, I appreciate your detailed response but Im worried that you were not really understanding my problem. The issue isn't that I need to find out what angle the ball is hitting at, but how to prevent the ball from colliding with that corner of Wall 1 as shown in the example. The ball hits the corner of wall 1 and is repelled back in the opposite bounce direction.

Lolslayer, your idea is more in line with what I need, but doing what you are suggesting would take just as long and be just as difficult as trying to create single wall objects to cover an entire room. It would be really hard to map out the edges of the walls I fear.
 
Top