Am I missing something?

BlitzMax Forums/BlitzMax Programming/Am I missing something?

Yahfree(Posted 2008) [#1]
I'm trying to add in some sound on my little project... and my ears start bleeding because its playing the sound every loop!

Why does the code below print "Hello" every loop it seems? The actions inside the collision IF statement should only occur when the balls collide:

	Method Update()
		'update positions
		point.x:+Vec.x
		point.y:+Vec.y
		
		If point.x<radius Then
			point.x=radius
			vec.x:*-1.0
			PlaySound sfxballwall
		End If
		If point.x>GraphicsWidth()-radius Then
			point.x=GraphicsWidth()-radius
			vec.x:*-1.0
			PlaySound sfxballwall
		End If
		If point.y<radius Then
			point.y=radius
			vec.y:*-1.0
			PlaySound sfxballwall
		End If
		If point.y>GraphicsHeight()-radius Then
			point.y=GraphicsHeight()-radius
			vec.y:*-1.0
			PlaySound sfxballwall
		End If
		For Local ball2:Enemyball = EachIn BallList
		
			Local collisionDistance# = 20
			Local actualDistance# = Sqr((ball2.point.x-point.x)^2+(ball2.point.y-point.y)^2)
			
			If actualDistance<collisionDistance
				Local collNormalAngle#=ATan2(ball2.point.y-point.y, ball2.point.x-point.x)
				
				Local moveDist1#=(collisionDistance-actualDistance)*(ball2.mass/Float((mass+ball2.mass)))
				Local moveDist2#=(collisionDistance-actualDistance)*(mass/Float((ball2.mass+mass)))
				point.x=point.x + moveDist1*Cos(collNormalAngle+180)
				point.y=point.y + moveDist1*Sin(collNormalAngle+180)
				ball2.point.x=ball2.point.x + moveDist2*Cos(collNormalAngle)
				ball2.point.y=ball2.point.y + moveDist2*Sin(collNormalAngle)
				
				
				Local n:TVec2 = New TVec2.Init(Cos(collNormalAngle),Sin(collNormalAngle))

				Local a1# = vec.DotProduct(n)
				Local a2# = ball2.vec.DotProduct(n)

				Local optimisedP# = (2.0 * (a1-a2)) / (mass + ball2.mass)
				
				vec.x:- (optimisedP*ball2.mass*n.x)
				vec.y:- (optimisedP*ball2.mass*n.y)
				
				ball2.vec.x:+ (optimisedP*mass*n.x)
				ball2.vec.y:+ (optimisedP*mass*n.y)
				'Why is this being printed every loop it seems?
				Print "Hello"
			End If
		Next
		If Sqr((PlayerX-point.x)^2+(PlayerY-point.y)^2) < 20
			SetColor 0,0,0
			DrawText "GAME OVER",0,0
			SetColor 255,255,255
		End If
	End Method


The balls rarely collide, most of the time they just bounce off the walls (the wall sound works correctly)


markcw(Posted 2008) [#2]
Comment out each playsound to see which one is misbehaving.


Jur(Posted 2008) [#3]
Cant say. When my code is not working as intended and the cause is not obvious I print problematic variables to see what is going on.


Brucey(Posted 2008) [#4]
'Why is this being printed every loop it seems?


Because, this
If actualDistance<collisionDistance

is true :-)


Yahfree(Posted 2008) [#5]
Thats my point brucey... It shouldn't be every loop... Only when any two ball centers are less then 20 pixels apart.


_JIM(Posted 2008) [#6]
A long shot, but are you by any means testing one of the balls with itself?

On a side note, try setting collisionDistance to 20*20 and get rid of the "sqr". It's faster :)


plash(Posted 2008) [#7]
A long shot, but are you by any means testing one of the balls with itself?
Oh snap. I bet thats it.


Yahfree(Posted 2008) [#8]
Spot on Jim. Looks like Plash gets his money ;)

Thanks for the help guys!