Log In  
Log In  

I'm learning to make hitbox collisions (instead of flags collisions).
Vertical collisions seem to work just fine but horizontals don't.
How can this be done?
Maybe hitbox collisions are just to complicated and shouldn't be used?
.
.
.

Cart [#hermo-0#] | Code | 2019-01-20 | License: CC4-BY-NC-SA | Embed

.
.
.

function _init()
    pad_x=45
    pad_y=45
    pad_width=30
    pad_height=30
    pad_color=7
    pad_speed=3

    ball_x=80
    ball_y=10
    ball_radius=4
    ball_color=8
    ball_speed_x=2
    ball_speed_y=2
end

function _update60()
    btnprss=false
    cls(1)

   --ball movement
    ball_x+=ball_speed_x
    ball_y+=ball_speed_y 

    --ball-screen collision
    if ball_x<0 or ball_x>128 then
        ball_speed_x=-ball_speed_x
    end

    if ball_y<0 or ball_y>128 then
        ball_speed_y=-ball_speed_y
    end
    --pad movement
    if btn(0) then
        btnprss=true
        pad_speed=-5
    end

    if btn(1) then
        btnprss=true
        pad_speed=5
    end

    pad_x+=pad_speed

    if btnprss==false then
        pad_speed=pad_speed/2
    end

    if pad_x<1 or pad_x>126-pad_width then
        pad_x=mid(1,pad_x,126-pad_width)
    end

    pad_color=7
    if ball_box(pad_x,pad_y,pad_width,pad_height) then
        pad_color=8
        ball_speed_y=-ball_speed_y
    end
end

function _draw()
    rectfill(pad_x,pad_y,pad_x+pad_width,pad_y+pad_height,pad_color)
    circfill(ball_x,ball_y,ball_radius,ball_color)
end

function ball_box(box_x,box_y,box_w,box_h)
    --conditions where there are no collisions
    if ball_y-ball_radius>box_y+box_h then
        return false
    end
    if ball_y+ball_radius<box_y then
        return false
    end
    if ball_x-ball_radius>box_x+box_w then
        return false
    end
    if ball_x+ball_radius<box_x then
        return false
    end
    return true
end
P#61033 2019-01-20 17:33


[Please log in to post a comment]

About | Contact | Updates | Terms of Use
Follow Lexaloffle:        
Generated 2019-05-25 08:11 | 0.021s | 4194k | Q:23