Making a player jump using physics in Visual Basic? -


i have been trying time implement jumping start of platformer game, without success. have managed implement gravity continuously trying move player downwards, , if falls below bottom of screen.

            case game_status.void                 keyboard_controls()                 plyer.moveplayer(0, g)                 if plyer.bottom > height                     plyer.playerstanding(true)                     plyer.moveplayer(0, -g)                 else                     plyer.playerstanding(false)                 end if 

what struggling implementing "jump" based on real-life physics. level student studying physics, aware of suvat , equations, , f=ma, cannot player jump when key pressed. ideas?

key event:

                case keys.w             if me.controls.contains(gamepanel) , gamepanel.controls.contains(plyer)                 u = true             end if 

my failed attempts @ physics:

        if u = true         dim t, v double         t = get_elapsed_time() / 1000         v = 0.5 + (g * t)         plyer.moveplayer(0, v)         'plyer.moveplayer(0, -(g*3))         'if plyer.top > 25         '    plyer.playerstanding(false)         '    plyer.moveplayer(0, g)         'end if     end if 

finally cracked it!

added xvel, yvel player class , re-implemeted gravity adjusting yvel of player in subroutine called every gameloop. doing this, had change yvel make player jump. code below:

in mainloop (in replace of previous gravity code):

                plyer.nextstep()                 if plyer.bottom >= height                     plyer.playerstanding(true)                     plyer.location = new point(plyer.location.x, (height - 10))                 else                     plyer.playerstanding(false)                 end if 

in player class:

    public sub nextstep()     if standing = false         yvel += g     else         yvel = 0     end if     top += yvel end sub  sub trytojump()     if standing         standing = false         dim v, s double         v = 0         s = 75         yvel = -(math.sqrt((v * v) - (2 * -g * s)))     end if end sub 

now, when jump key pressed, calls trytojump(), , jumps if player standing!


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -