Problems with boucing objects collisions

I'm making a top down shooter that when you shot it create a bulletshell, and that bullet shell will rotate and move to a certain direction, and if it collide with an wall it will change the movement to the opposite direction.

The problem is that when it collide with the wall the image flickers and the object do not bounce

Here the code:
create:
Code:
rotateSpd = random(4);
spd = 5;
dir = 0;
step:
Code:
//rotaciona a imagem
if(spd > 0){
    spd -= 0.1;
   image_angle += rotateSpd;
}else{
    spd = 0;
}

caseX = lengthdir_x(spd,dir);
caseY = lengthdir_y(spd,dir);

//colisions
if(caseX != 0){
var collisionH = place_meeting(x+caseX,y,oWall);
    if(collisionH){
        repeat(abs(caseX)){
            if(!place_meeting(x+sign(caseX),y,oWall)){
                x += sign(caseX);
            }else break;
        }
        dir = dir + 180;
    }
}
if(caseY != 0){
var collisionV = place_meeting(x,y+caseY,oWall);
    if(collisionV){
        repeat(abs(caseY)){
            if(!place_meeting(x,y+sign(caseY),oWall)){
                y += sign(caseY)
            }else break;
        }
        dir = dir + 180;
    }
}

x += caseX;
y += caseY;
OBS: the "dir" is coming from the player mouse position (point direction) + 90°

So when the player shoot, it will create the bulletcase and assing the direction

*also the sprite is a 8x6 rectangle
 
Last edited:
C

Catastrophe

Guest
Not sure if there's any other issues, but there's an issue that's similar to a recent post here: Once you bounce, and you have code that butts it up at to the wall, that code at the bottom is still going to run and push it further into the wall in the direction it's already going (caseX and caseY have not had their lengthdirs for the new direction yet). This should cause it to get stuck in the wall flipping directions every frame.

So onlhy run those last two lines if you have not bounced that frame
 
Not sure if there's any other issues, but there's an issue that's similar to a recent post here: Once you bounce, and you have code that butts it up at to the wall, that code at the bottom is still going to run and push it further into the wall in the direction it's already going (caseX and caseY have not had their lengthdirs for the new direction yet). This should cause it to get stuck in the wall flipping directions every frame.

So onlhy run those last two lines if you have not bounced that frame
Thanks, Now it's working, i've crated an boolean variable that says if the object has already bounced so it wont bouce again and enter the wall.
And i've created another variable to reset the bounce so it will be able to collide with other wall when in a corridor

but some bulletcases still pass throught the wall, and i have no idea why.

here the new code:
Code:
//rotaciona a imagem
if(spd > 0){
    spd -= 0.1;
   image_angle += rotateSpd;
}else{
    spd = 0;
}

//reseta o bounce
if(bounceReset > 0){
    bounceReset -= 0.2;
}else{
    bounceReset = 0;
}

caseX = lengthdir_x(spd,dir);
caseY = lengthdir_y(spd,dir);

//colisions
if(!bounced){
var collisionH = place_meeting(x+caseX,y,oWall);
    if(collisionH){
        repeat(abs(caseX)){
            if(!place_meeting(x+sign(caseX),y,oWall)){
                x += sign(caseX);
            }else break;
        }
        dir = dir + 180;
        bounced = true;
        bounceReset = 1;
    }
    
var collisionV = place_meeting(x,y+caseY,oWall);
    if(collisionV){
        repeat(abs(caseY)){
            if(!place_meeting(x,y+sign(caseY),oWall)){
                y += sign(caseY)
            }else break;
        }
        dir = dir + 180;
        bounced = true;
        bounceReset = 1;
    }
}


if(bounced && bounceReset == 0){
    bounced = false;
}

x += caseX;
y += caseY;
 
C

Catastrophe

Guest
Well I was saying all you need is:


if(!bounced){
x += caseX;
y += caseY;
}

and at the very top

if (bounced = true) {
bounced = false
}

Your code will stop continual bouncing, but it never fixed the issue of the it getting stuck in the wall in the first place as that bottom code always runs.

As a side note: While your code will work it will have a visual side effect of the bounce causing it to teleport to the wall sideways for a frame. If you want a more optimal code, then I would combine your two x and y bounce code into one, and instead of stepping along x/y step one point at a time, step one point at a time into length_dirx and y at the same time and do your collision checks.
 
Last edited by a moderator:
R

robproctor83

Guest
While we are on the topic of bounces here, does anyone have a good solution for circular collision bounces? With normal collisions when a circle mask collides with the corner of a square mask the bounce direction is unexpected because even though it's a "circle" collision it's still basing collisions off two square colliding. I've done some looking online and haven't been able to really find a good clear answer. Another issue, though not as big, is that when two squares are side by side making a flush edge between the two it still sometimes bounces off it's corner even though you would expect a nice flat bounce.
 
Well I was saying all you need is:


if(!bounced){
x += caseX;
y += caseY;
}

and at the very top

if (bounced = true) {
bounced = false
}

Your code will stop continual bouncing, but it never fixed the issue of the it getting stuck in the wall in the first place as that bottom code always runs.

As a side note: While your code will work it will have a visual side effect of the bounce causing it to teleport to the wall sideways for a frame. If you want a more optimal code, then I would combine your two x and y bounce code into one, and instead of stepping along x/y step one point at a time, step one point at a time into length_dirx and y at the same time and do your collision checks.

Indeed i thought doing just like that but i was also trying to optimize in a way that it won't go throught the wall (OBS: just some of the bulletcases go through the wall).

But how i can combine the code for the x and y bouce collision

sorry if im asking dumb questions
 
C

Catastrophe

Guest
Well all you'd do is something like (pseudocode)

for loop on i = 0 to vsp
check place_meeting(x + lengthdir_x(i,dir), y + lengthdir_y(i,dir), wall)

and then when it hits a wall, add x + lengthdir_x(i,dir) etc.and add 180 degrees.

Edit: note: this is just using your style of coding for the same effect. I would personally use collision_line for your initial check, or you will potentially go through corners/thin walls
 
Last edited by a moderator:
Yes thanks now i understand, now i'm using the collision line and it's working, also i've found another mistake in the dir = dir + 180, i should have used dir = -dir + 180, because it was giving non existing angles
 
While we are on the topic of bounces here, does anyone have a good solution for circular collision bounces? With normal collisions when a circle mask collides with the corner of a square mask the bounce direction is unexpected because even though it's a "circle" collision it's still basing collisions off two square colliding. I've done some looking online and haven't been able to really find a good clear answer. Another issue, though not as big, is that when two squares are side by side making a flush edge between the two it still sometimes bounces off it's corner even though you would expect a nice flat bounce.
About that im not sure, i guess you have to do collision checks for diagonal movement
like:
Code:
if(place_meeting(x+hspd,y+vspd,oCollsion)){
   //your collision code here

   
if(place_meeting(x-hspd,y-vspd,oCollsion)){
   //your collision code here
}
but it's just an idea, i havent tested it.
 
Top