Having trouble with a custom move_bounce_all

W

WimpyLlama

Guest
I need the functionality of move_bounce_all, but better. I would like to be able to control the bounce and use the proper angle of reflection to bounce in the correct direction. I've been trying to figure this out for a few days now, and this is all I got.

GML:
//X and y collision
var collideInst = instance_place(x + lengthdir_x(speed, direction), y + lengthdir_y(speed, direction), oWall);
if (collideInst != noone) {
    //Get wall angle
    var wallAngle = collideInst.image_angle;

    //Get wall bounding box
    var wallLeft = collideInst.x;
    var wallRight = collideInst.x + sprite_get_width(collideInst.sprite_index);

    var wallTop = collideInst.y;
    var wallBottom = collideInst.y + sprite_get_height(collideInst.sprite_index);

    //Add to wall angle if hitting the sides
    if ((bbox_right >= wallLeft or bbox_left <= wallRight) and
        (y > wallTop and y < wallBottom)) wallAngle += 90;

    //Bounce away
    motion_set((2 * wallAngle) - direction, speed);
}
The problem with this is a lot of things. 1: Bouncing doesn't work on the corners of the walls. You just phase through. 2: If you hold yourself up to a wall long enough, you clip into it. And 3: Setting down walls is very restricting now. The bouncing only works on walls that haven't been stretched or only ones that have been stretched horizontally. If they're stretched vertically, you just phase through.

Does anyone have a fix for this? If so, that'd be amazing! Thanks.
 
W

WimpyLlama

Guest
Thanks, y'all for allllll the enormous amount of spectacular help that totally fixed all my problems. Sarcasm aside, I finally got it working well. While it doesn't feel like it's the most optimized method, it does still work well and doesn't seem to be a huge effect on performance. If anyone has any other ideas, that'd be great.

GML:
    //X and y collision
    //Define some variables
    var hitX = 0;
    var hitY = 0;
    
    var length = (sprite_get_width(sprite_index) / 2) + 1;
    
    var collideInst = noone;
    
    //Look for a collidable on every direction of the player and if one is found, bounce
    for (b = 0; b < 360; b++) {
        hitX = x + lengthdir_x(length, direction + b);
        hitY = y + lengthdir_y(length, direction + b);
    
        collideInst = instance_position(hitX, hitY, inst);
        
        if (collideInst != noone) {
            //Get the direction the ball used to hit the collidable
            var dir = point_direction(x, y, hitX, hitY);
        
            //Bounce away
            motion_set((2 * (dir - 90)) - dir, speed);
            
            //Break out of the loop
            break;
        }
    }
 
Top