Function outside loop effecting rotation speed!! ?

BlitzMax Forums/BlitzMax Beginners Area/Function outside loop effecting rotation speed!! ?

Amon(Posted 2005) [#1]
Type ball

	Field x:Float ' Xposition
	Field y:Float 'Yposition
			
	Global Bspeed:Int = 0 ' ball speed
	Global dirLR:Int = 0 ' direction moving
	Global dist:Float = 0 ' distance from center
	Global angle:Float = 0 'angle for sin/cosin
	Global BXpos:Int = 400 'ball XOffset
	Global BYpos:Int = 300 ' BallYOffset
	Global rot:Float = 0 'Ball Roatation
	Global Maxballs = 11 ' Max Number Of balls
	Global tempAngleINC = 32 ' angle increment
	Global tempAngle:Int = 0
	
	Function generateBalls()
		
		For Local x:Int = 0 To 3
		Local b:ball = New ball
		b.x = 0
		b.y = 0
		ListAddLast ballList,(b)
		dist = 100
		Next
				
	End Function
	
	Method rotateBalls()
	
		x = BXpos+Sin(angle)*dist
		y = BYpos+Cos(angle)*dist
		
		angle:+1
		
		If angle >= 360 Then angle = 0
		
				
	End Method
	
	Method drawBalls()
		
		DrawImage bk1,0,0,0
		SetRotation(ballAngle)
		ballAngle:+5
		If ballAngle >= 360 Then ballAngle = 0
		DrawImage balls,x,y,ballFrame
		SetRotation(0)
		
	End Method
	
End Type

ball.generateBalls()

Repeat

	Cls
		
		
		
		For Local b:ball = EachIn ballList
		b.rotateBalls()
		b.drawBalls
		Next
			
	FlushMem
	Flip
	
Until KeyHit(KEY_ESCAPE)


Hi, The generateBalls() function generates 4 balls. I call the function outside the loop yet it still effects the speed at which the balls rotate and spin.

Any Ideas why It would be doing that?

If i remove the loop that creates extra balls:
	Function generateBalls()
		
		
		Local b:ball = New ball
		b.x = 0
		b.y = 0
		ListAddLast ballList,(b)
		dist = 100
		
				
	End Function



the rotation and spin speed remains how i want it but when I put the code below in it speeds everything up. :/

	Function generateBalls()
		
		For Local x:Int = 0 To 3
		Local b:ball = New ball
		b.x = 0
		b.y = 0
		ListAddLast ballList,(b)
		dist = 100
		Next
				
	End Function



Koriolis(Posted 2005) [#2]
You are incrementing the global variable 'angle' every time you call rotateBalls. The more balls there are, the more you call rotateBalls and the more you increment 'angle' per frame.
You'll want to either have 'angle' be a field and not a global variable, or update 'angle' only once per frame (outside the 'rotateBalls' Method.
Side note: 'rotate' is a much better name than rotateBalls. You shouldn't repeat the name of the type as part of the method name, and in addition you're rotatin a single ball, so at the very least that would be rotateBall, not rotateBalls.


Amon(Posted 2005) [#3]
Ahh, Now theres something new learned. I've modified the code and it works. Thanks. :)

One quick question.

I've modified the generateballs function to create more balls. I want the balls to be created in a circle according to the position set for the angle in sin/cos. Am I on the right track because it doesn't appear to be working. Code below:

	Function generateBalls()
		
		dist = 100
		tempAngle = 0
		For Local iter:Int = 0 To 3
		Local b:ball = New ball
		b.x = BXpos+Sin(tempAngle)*dist
		b.y = BYpos+Cos(tempAngle)*dist
		tempAngle:+32
		ListAddLast ballList,(b)
		Next
				
	End Function


Imagine a ring of balls. Thats what I'm trying to create for a weopan.


deps(Posted 2005) [#4]
Do you mean something like this?
Function generateBalls( num=3 )
		angleinc = 360/num ' How many angles between each ball?
		dist = 100
		tempAngle = 0
		For Local iter:Int = 0 To num
		Local b:ball = New ball
		b.x = BXpos+Sin(tempAngle)*dist
		b.y = BYpos+Cos(tempAngle)*dist
		tempAngle:+angleinc 
		ListAddLast ballList,(b)
		Next
End Function



Amon(Posted 2005) [#5]
Hi Deps, Thanks for the code. It's working nicely now. :)