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

GML Bouncing off walls

P

pbruce9

Guest
I am trying to make a mini golf game with the ball bouncing off the walls in the level. I have used some different coding than usual to get my game working, but the normal bounce function wont work. I have got the ball to bounce of the top and bottom walls but it wont bounce off side walls, instead it will just freeze next to it.

Code:
  // Create Event for obj_ball

{
    max_power=30;
    pow=0;
    dir=0;
    spd=0;
    m_dir=0;
    held=false;
    frict=0.7;
    percent=20;
}

Code:
 // Step Event for obj_ball

    if (mouse_check_button_pressed(mb_left)){
    if (point_distance(x,y,mouse_x,mouse_y)<32){
    held=true;
    }
}
    if (held) {
    pow=min(percent*point_distance(x,y,mouse_x,mouse_y)/100,max_power);
    dir=point_direction(mouse_x,mouse_y,x,y);
}
    if (mouse_check_button_released(mb_left)){
    spd=pow;
    m_dir=dir;
    held=false;
    pow=0;
}
    if(spd>0){
    x+=lengthdir_x(spd,m_dir);
    y+=lengthdir_y(spd,m_dir);
    if (spd-frict>+0){
    spd-=frict;
    }
    else{
        spd=0;
    }
}
Code:
 // Collision event for obj_ball with obj_wall

{
    m_dir = m_dir * -1
}
Any help would be appreciated.
 
Last edited by a moderator:

Miradur

Member
Hi, take a look here:

Code:
move_bounce_solid(adv);

or

move_bounce_all( adv );
It´s much easier to the beginning.

Miradur
 
P

pbruce9

Guest
Hi, take a look here:

Code:
move_bounce_solid(adv);

or

move_bounce_all( adv );
It´s much easier to the beginning.

Miradur
I tried that but the ball just gets stuck in the wall. I think its because of my coding for the ball movement. Would there be a way I could change my coding so it still has all the elements like going in the direction that I want it too when I release the mouse and having friction?
 
N

NeZvers

Guest
Please, for love of anything that's dear to you, USE INDENTATION when asking for help! And preferably use code insert.
so it look like this:
Code:
///Create Event for obj_ball

{
   max_power=30;
   pow=0;
   dir=0;
   spd=0;
   m_dir=0;
   held=false;
   frict=0.7;
   percent=20;
}


///Step Event for obj_ball

if (mouse_check_button_pressed(mb_left)){
   if (point_distance(x,y,mouse_x,mouse_y)<32){
      held=true;
   }
}
if (held) {
   pow=min(percent*point_distance(x,y,mouse_x,mouse_y)/100,max_power);
   dir=point_direction(mouse_x,mouse_y,x,y);
}
if (mouse_check_button_released(mb_left)){
   spd=pow;
   m_dir=dir;
   held=false;
   pow=0;
}
if(spd>0){
   x+=lengthdir_x(spd,m_dir);
   y+=lengthdir_y(spd,m_dir);
   if (spd-frict>+0){
      spd-=frict;
   }
   else{
      spd=0;
   }
}

///Collision event for obj_ball with obj_wall

{
   m_dir = m_dir * -1
}

I'm also struggling with this problem and I've tried using built in speed and direction movement and using hspd & vspd aproach and Gamemaker have buggy collision with them both.
 
P

pbruce9

Guest
Please, for love of anything that's dear to you, USE INDENTATION when asking for help! And preferably use code insert.
so it look like this:
Code:
///Create Event for obj_ball

{
   max_power=30;
   pow=0;
   dir=0;
   spd=0;
   m_dir=0;
   held=false;
   frict=0.7;
   percent=20;
}


///Step Event for obj_ball

if (mouse_check_button_pressed(mb_left)){
   if (point_distance(x,y,mouse_x,mouse_y)<32){
      held=true;
   }
}
if (held) {
   pow=min(percent*point_distance(x,y,mouse_x,mouse_y)/100,max_power);
   dir=point_direction(mouse_x,mouse_y,x,y);
}
if (mouse_check_button_released(mb_left)){
   spd=pow;
   m_dir=dir;
   held=false;
   pow=0;
}
if(spd>0){
   x+=lengthdir_x(spd,m_dir);
   y+=lengthdir_y(spd,m_dir);
   if (spd-frict>+0){
      spd-=frict;
   }
   else{
      spd=0;
   }
}

///Collision event for obj_ball with obj_wall

{
   m_dir = m_dir * -1
}

I'm also struggling with this problem and I've tried using built in speed and direction movement and using hspd & vspd aproach and Gamemaker have buggy collision with them both.
Okay thanks, it was my first time posting on this, I didn't know what to do. If you find anything to help please let me know, I will too.
 
T

trialuser

Guest
Im not sure if this is the factor, but did u set the solid true?
 
N

NeZvers

Guest
@trialuser Collision event get's triggered without objects being set solid. My own previous attempts were handled all in step event and still, collisions acted weird (after putting counter I found that some seemingly single bounces actually triggered twice).
But yeah for move_bounce_solid() needs to have walls set to solid.
 
P

pbruce9

Guest
@trialuser Yeah I have it on solid, also tried taking solid off an using the function move_bounce_all() but nothing works
 
T

trialuser

Guest
@pbruce9 I see.. what you did:
Code:
// Collision event for obj_ball with obj_wall
m_dir = m_dir * -1;
and you've got the ball to bounce off the top and bottom walls but it won't bounce off side walls;
try this:
Code:
// Collision event for obj_ball with obj_wall
m_dir = 180-m_dir;
and you'll get the ball to bounce off side walls but it won't bounce off the top and bottom walls!
Then I try this:
Code:
// Collision event for obj_ball with obj_wall
if(place_meeting(x,(y+1 or y-1),other)) m_dir = -m_dir;
if(place_meeting((x+1 or x-1),y,other)) m_dir = 180-m_dir;
For a horizontal surface you bounce at 180 - direction.
For a vertical surface you bounce at -1*direction which is equivalent to 0-direction or 360-direction.
Then I run the game.. try to bounce off the top and leftside walls and smile.. (YES!! it's solved!) :)
But it won't bounce off the bottom and rightside walls! :eek:
Maybe I shouldn't put that in the Collision event. So I put that in the Step event but it's still do the same.
But I believe the code above is one of the formula. ;)
 
P

pbruce9

Guest
[FIXED]

Thanks @trialuser

I used those two commands, but I made two separate invisible solid objects, one for the top and bottom walls, and one for the side walls. Then I made two different collision events for the walls.

Code:
 // Collision Event with obj_side_wall
{
   m_dir = 180-m_dir;
}
Code:
 // Collison Event with obj_vertical_wall
{
   m_dir = -m_dir
}
Then, to avoid problems with corner walls, I change the sprites into trapeziums, so they fit nicely in the corners.

gameaker.PNG

The game is now works okay, but sometimes has a weird bounce on the exact corner, but it isn't a big deal for me.
 
Last edited by a moderator:
P

pbruce9

Guest
View attachment 19299 [UPDATE]

In certain places I the ball would glitch into the wall. I realised this was because the trapezium was thin in some places and the ball was moving to fast for the wall to register it had collided. I changed the wall sprites to 8 by 64 so there is always an 8 pixel depth it can collide with, which worked for my game. In the corners you can just turn off 'delete underlying' so they overlap. The ball will then randomly choose which wall to collide with which doesn't really make a big difference in my game.
 
T

trialuser

Guest
@pbruce9 you're welcome and congrats! Thanks for sharing that too, I'm glad to hear it works. Good luck with your mini golf!
 
Last edited by a moderator:
Top