Making sin/Cos faster for darts aiming.

Blitz3D Forums/Blitz3D Beginners Area/Making sin/Cos faster for darts aiming.

GameCoder(Posted 2004) [#1]
Hi, I have below a small section on my darts engine code. I have tried several methods to get sin/cos rotation to speed up but if I do speed it up it looses its per pixel collision. ie it skips. If I speed up by 2 it skips 2 and if i speed up by 4 it skips 4 pixels.

I need help to speed up the rotation. I will post the full source if need be as its not going to be commercial anymore, but I would like to avoid that if possible.

Here is the molebox packed exe.

http://www.kamikazekrow.com/storage/prodarts.rar


Here is a small section of the engine code that deals with the sin/cos aiming system

Function engine()

Repeat

mh2 = MouseHit(2)

	Cls

	DrawImage fpscr,0,0
	DrawImage boardsbig,dartx,darty,picked_board
	Rect MouseX()+Sin(xangle#)*80,MouseY()+Cos(yangle#)*80,5,5
	xangle# = xangle + 1
	yangle# = yangle + 1
	
	If xangle# >360 Then xangle# = 0
	If yangle# >360 Then yangle# = 0
	mx#=dartx+ix
	my#=darty+ix
	
	dx#=MouseX()+Sin(xangle#)*80-mx
	dy#=MouseY()+Cos(yangle#)*80-my
	ang#=ATan2(dy,dx)
	If ang<0 ang=ang+360
	
	Text 1,1,MouseX()+"-"+MouseY()+" A:"+ang+" L>"+MouseZ()
	Text 1,20,lastScore#
	drawZ(ang,MouseZ())
	ang=ang+100
	If ang<0 ang=360-Abs(Ang)
	If ang>360 ang=ang-360

	zone# =ang#/18.0
	
	lastScore=int2(zone+1.)
	If lastscore>20 
		lastScore=20	
	EndIf
	If lastScore<1
		lastScore=1
	EndIf
	lastScore=score(lastScore)
	td#=dist(MouseX()+Sin(xangle#)*80,MouseY()+Cos(yangle#)*80,dartx+ix,darty+iy)
	
	If td>218
	
	scor = 0
		;foulshot
	EndIf
	
	If td<209
		scor=lastscore*2 ;double
	EndIf
	If td<192
		scor=lastScore ;normal zone
	EndIf
	If td<140
		scor=lastscore*3 ;triple
	EndIf
	If td<116
		scor=lastScore ;normal
	EndIf
	If td<27 
		scor=25 ;25 points
	EndIf
	If td<14
		scor=50; bullseye
	EndIf

	;EndIf
	
	;--Score Check
	If MouseHit(1)And shot < 3 And canthrow = 1
		playScore=playScore-scor
		lastScor=scor
		totScore=totScore+scor
		shot=shot+1
		msg$="Shot Number "+shot

		d.dartt = New dartt
		d\x# = MouseX()-45+(Sin(xangle#)*80)	
		d\y# = MouseY()-42+(Cos(yangle#)*80)
	
	EndIf	

		If shot = 3 
			
			canthrow = 0
			
		EndIf
		
		If mh2 = True And canthrow = 0
				shot=0
				canthrow = 1
			For d.dartt = Each dartt
				Delete d
			Next
						
			totScore=0
	
		EndIf
		
			If totScore=180
				msg$="ONE HUNDRED AND EIGHTY!"
			Else
				msg$=" Player 1 Scored >"+totScore
			EndIf
			

	
	For d.dartt = Each dartt
		
		DrawImage dart,d\x#,d\y#,2
		
	Next
	
	
	Text 1,540,"Player 1 Needs>"+playScore
	If lastScor>0
	Text 200,540,"Player 1 Scored>"+lastscor
	EndIf
	Text 100,560,msg
	Flip
	
Until KeyDown(1)	

	
End Function



tonyg(Posted 2004) [#2]
Would it be quicker to have a precalculated sin/cos lookup table? Check the code archives Algorithm section.


GameCoder(Posted 2004) [#3]
Ill have a look. Thx :)


Abomination(Posted 2004) [#4]
perhaps You could put the calculations and the drawing commands in seperate functions and call the calculate-function a couple of times per frame, before calling the drawing-function?


GameCoder(Posted 2004) [#5]
Thx Abomination. I was just thinking I could seperate them and then calculate the aiming twice with a for/next loop.

Thx for the advice :)


[edit] Done, Thx again. :)[/edit]


Sir Gak(Posted 2004) [#6]
Pre-calculated trig tables are WAY faster than computing them on the fly.


Alberto(Posted 2004) [#7]
I have the same problem
as far as I know lookup tables were useful some time ago when the bottle neck was the CPU rather than the memory.
Nowadays I do not think they are much faster.
Probably the best solution is to use an approx evaluation (Taylor's series ? ) of the sin\cos , unless accuracy is a must which is unlikely in game programming.
This question has been arisen already here on blitz3d forum.
I do not remember wether the blitz comand use a fast \ approx formula


Warren(Posted 2004) [#8]
Depends on your use as to whether it matters or not. If you're making thousands of sin/cos calls a frame, it's probably worth it. Otherwise, the difference is negligible.

I mean, yes, the look up tables are almost always faster but it doesn't really become relevant until you start getting into thousands of calls per frame.