UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: LemonWizard on November 23, 2009, 08:38:11 AM

Title: Bouncing Physics
Post by: LemonWizard on November 23, 2009, 08:38:11 AM
OKay so , I got some help from a friend with some math stuff.. and I think i got something here


if anyone here knows how I could improve the math in this let me know

it's just a simple demo, hold the mouse to reset the square and drop it
I tinkered with the values here it's actually pretty fun... not really stable though

you can use the arrow keys to build up horizontal momentum.
hold the keys before you let go of the mouse, to build even more.


[pbcode]
; PROJECT : Physics Thing
; AUTHOR  : Na
; CREATED : 11/23/2009
; EDITED  : 11/23/2009
; ---------------------------------------------------------------------
gravity#=0.10
square_velocity#=10
squarex#=100
squarey#=100
bouncetime#=0
cc#=5
falling=true
square=newfximage(32, 32)
rendertoimage square
cls rndrgb()
rendertoscreen
image=newfximage(getscreenwidth(), getscreenheight() )


main:
squarex#=squarex#+left_velocity#
squarex#=squarex#+right_velocity#
if leftkey()

right_velocity#=0
left_velocity#=left_velocity#-0.1
endif



if rightkey()

left_velocity#=0
right_velocity#=right_velocity#+0.1
endif



rendertoimage image
if leftmousebutton()
   squarex#=mousex()
   squarey#=mousey()
   falling=true
   square_velocity#=0
   gravity#=0.10
   left_velocity#=0
   right_velocity#=0
endif   
circle squarex#, squarey#, cc#, 1
rendertoscreen
drawimage image, 0, 0, 0
print "Gravity" : print gravity#
print "Square Velocity" : print square_velocity#
print "Bounce time" : print bouncetime#
drawimage square, squarex#, squarey#, 0
if falling=true then square_velocity#=square_velocity#+gravity#
if falling=false
   bouncetime#=bouncetime#+gravity#
   square_velocity#=square_velocity#-gravity#
endif

if bouncetime#>0
   falling=true
   bouncetime#=0
   endif   
   
squarey#=squarey#+square_velocity#

if squarey# >getscreenheight()-32
   falling=false
   square_velocity#=neg(square_velocity#/2)+neg(right_velocity#*0.25)+(left_velocity#*0.25)
   bouncetime#=square_velocity#*0.25
   squarey#=getscreenheight()-33
endif   

if squarex# <0
   squarex#=0
right_velocity#=neg(left_velocity#/4)
left_velocity#=0
endif

if squarex# >getscreenwidth()-32
   squarex#=getscreenwidth()-31
   left_velocity#=neg(right_velocity#/4)
   right_velocity#=0
   endif


   


sync
cls rgb(0,0,0)
goto main


[/pbcode]

Title: Re: Bouncing Physics
Post by: ATLUS on November 24, 2009, 04:35:26 AM
he he
http://www.underwaredesign.com/forums/index.php?topic=3195.0