Bouncing Object

BlitzPlus Forums/BlitzPlus Programming/Bouncing Object

aab(Posted 2004) [#1]
I've cut the code ive got a problem with into this little program:

I Want this image (key) to Bounce as it lands, but when the object hits the ground,
it either dissapears, or moves rigidly up:::why?


My Parabola is perfect (as is proven by this little program):




aab(Posted 2004) [#2]
*Bump


MSW(Posted 2004) [#3]
Do you mean something like this:
Graphics 320,240,16,2
Global thebuffer=BackBuffer()
SetBuffer thebuffer
AutoMidHandle True

Type dkey 
	Field kx#
	Field ky#
	Field v#
	Field age
End Type
Global drop.dkey

Function DropKey(x#,y#) ;pay attention to this here too
	drop.dkey=New dkey
	drop\kx#=x#
	drop\ky#=y#-180
	drop\v#=0
	drop\age=y#
End Function


;I've replaced the keys with Red Blocks for this (they have blue shadows)
Global keys_shadow=CreateImage(4,3)		;Loadimage("key_shadow.png")
	SetBuffer ImageBuffer(keys_shadow)
	Color 0,0,255
	Rect 0,0,4,3
Global chest_keys=	CreateImage(4,8)		;Loadimage("key.png")
	SetBuffer ImageBuffer(chest_keys)
	Color 255,0,0
	Rect 0,0,4,3
SetBuffer thebuffer



Const RETURN_key=28
While Not KeyHit(1)
	Cls
	
	;press Return to Create a key object
	If KeyHit(RETURN_key) DropKey(Rand(20,300),200+Rand(-20,30))
	update_Key_items()
	
	VWait:Flip False
	Wend
End

Function update_Key_items()
	For drop.dkey=Each dkey
		DrawImage keys_shadow,drop\kx#,drop\age
		
;I rewrote your whole routine added another variable (v#) to the type
; age = location of shadow
; kx = X location on screen
; ky = current Y location on screen
; v = vector amount object is to fall

		 drop\ky#=drop\ky#+drop\v#
		
		  If drop\ky#>drop\age Then
		   drop\v#=(drop\v#)*-.3 ;change this number for more/less bounceing
		   drop\ky#=drop\age
		  Else
		   drop\v=drop\v+.1
		  End If
		 
		 DrawImage chest_keys,drop\kx#,drop\ky#


	
	Next
End Function


Change that -.3 to something else between -1 and 0 for different bounce hieghts and such.


MSW(Posted 2004) [#4]
but I also fixed your function:
Function update_Key_items()
	For drop.dkey=Each dkey
		DrawImage keys_shadow,drop\kx#,drop\ky#
		If drop\age<0
			drop\age=drop\age+1
			DrawImage chest_keys,drop\kx#,drop\ky#+3*drop\age
		ElseIf drop\age<60

;you had this
;			DrawImage chest_keys,drop\kx#,drop\ky#+(Float#((drop\ky#-60)*(drop\ky#))/45)

;when you should have had this:
			DrawImage chest_keys,drop\kx#,drop\ky#+(((drop\age-60)*drop\age)/45)

			drop\age=drop\age+1
		Else
			DrawImage chest_keys,drop\kx#,drop\ky#
			If rectsoverlap(Charx-8,Chary-8,16,16,drop\kx-8,drop\ky-8,16,16) inkeys=inkeys+1 Delete drop.dkey
		EndIf
		
		
	Next
End Function


You were useing the drop\ky# instead of drop\age