#Ball bouncing wierdly

9 messages · Page 1 of 1 (latest)

tawdry radish
#

Right now my game has a ball that you can hit and bounce around the room, The problem is that at certain angles, the ball doesn't bounce as you would expect. I've attached a diagram of what's happening, and here is my code:


if(place_meeting(x+hspeed,y,oWall)){
    //while(!place_meeting(x+sign(hspeed),y,oWall)){
    //    x += sign(hspeed)
    //}
    hspeed *= -1;
    hor_bounce = 1;
    speed = max(speed*bounce_coefficient,2);
    trigger_event(event_ball_bounce)
    direct_hit = 0;
}

if(place_meeting(x+hspeed,y+vspeed,oWall)){
    //while(!place_meeting(x+hspeed,y+sign(vspeed),oWall)){
    //    y += sign(vspeed)
    //}
        speed = max(speed*bounce_coefficient,2);
        vspeed *= -1;
    trigger_event(event_ball_bounce)
    direct_hit = 0;
}

image_angle += speed*5;
last_speed = speed;```

I assume what's happening here is that there's a problem with the horizontal speed and vertical speed being reversed within the same frame, but I'm not exactly sure why that's happening. I've tried all sorts of fixes and permutations of this code, but they all seem to break in one way or another. Appreciate any help
#

You can see the glitch happening in the last few seconds of the video attached

nocturne helm
tawdry radish
#

Doesn't actually work, it just makes the ball "drill" into the wall

nocturne helm
lapis hare
#

just check if it's hitting vertically, if so, invert vspd, if horizontally, invert hspd. You're prob inverting both speeds

tawdry radish
#

in any case, I managed to brute force solve the problem by making the walls on the side a different object than the walls on the top and bottom, so the ball only inverts its hspeed when touching a horizontal collision type wall and same thing for the bottom-top collisions

#

works better than I thought