Argh, another cos/sin problem

Blitz3D Forums/Blitz3D Beginners Area/Argh, another cos/sin problem

Sokurah(Posted 2006) [#1]
Ok, I'm sure this has been covered before, but I can't find a solution, so here's my question;

I want to rotate some points around a fixed central point. If I use angle & distance I can make it work easily - like this;

Graphics 640,480,16,2
SetBuffer BackBuffer()
SeedRnd (MilliSecs())
Origin 320,240

Type StarField
	Field distance
	Field angle
End Type


For a=1 To 100	;Generate a bunch of positions
	b.StarField		= New StarField
	b\angle 		= Rand(0,359)
	b\distance 		= Rand(20,700)
Next

Function Draw()
	For a.StarField= Each StarField
		x# = a\distance * Cos( a\angle )
		y# = a\distance * Sin( a\angle )
		Oval x#,y#,10,10,0
	Next 
End Function

;Mainloop
While Not KeyHit(1)
Cls
For b.StarField= Each StarField
b\angle=b\angle-1
Next 
Draw()
Oval 0,0,10,10,1	;This is what we rotate around
Flip
Wend


...but I don't want to use angle & distance - I want to use x & y coordinates, like in the following example. I realised I needed distance, so I'm inserting it into the type, to help me rotate the points, but I only need the x & y coordinates for the rest of the program.

Well, it's rotating - but not as it should. Something is wrong - I can't get the circles to rotate around the centre and I can't figure out what's wrong.

Can someone help?

Graphics 640,480,16,2
SetBuffer BackBuffer()
Origin 320,240

Type StarField
	Field x#
	Field y#
	Field Distance#
End Type

Function Dist(x1#, y1#, x2#, y2#)
	Return Sqr( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))
End Function

For a=1 To 100	;Generate a bunch of positions
	b.StarField= New StarField
		x0 = Rand(-500,500)
		y0 = Rand(-500,500)
		b\x# = x0
		b\y# = y0
		b\Distance# = Dist(0,0,x0,y0)	;Store distance from centerpoint.
Next

Function Draw()
	For s.StarField= Each StarField
		dx# = s\x#
		dy# = s\y#
		x# =  dx#+(Cos#(angle) * s\Distance#)
		y# =  dy#+(Sin#(angle) * s\Distance#)
		Oval x#,y#,10,10,0
		Plot dx,dy			;Positions stored in types (for comparison)
	Next
End Function

;Mainloop
Global Angle=0
While Not KeyHit(1)
Cls
Draw()
Oval 0,0,10,10,1	;This is our center spot
angle=angle+1
If angle=360 Then angle=0
Flip
Wend


Thanks.


Stevie G(Posted 2006) [#2]
No need to the distance functon ....


Graphics 640,480,16,2
SetBuffer BackBuffer()
Origin 320,240

Type StarField
	Field x#
	Field y#
End Type

For a=1 To 100	;Generate a bunch of positions
	b.StarField= New StarField
	b\x = Rand(-500,500)
	b\y = Rand(-500,500)
Next

;Mainloop
Global Angle=0
While Not KeyHit(1)
Cls
Draw()
Oval 0,0,10,10,1	;This is our center spot
angle=angle+1
If angle=360 Then angle=0
Flip
Wend


Function Draw()
	
	For s.StarField= Each StarField
		x# =  s\x * Cos( angle ) + s\y * Sin( angle ) 
		y# =  s\y * Cos( angle ) - s\x * Sin( angle )
		Oval x#,y#,10,10,0
		Plot x,y			;Positions stored in types (for comparison)
	Next

End Function


Stevie


Sokurah(Posted 2006) [#3]
Cool. Thanks Stevie.

I knew I was close, but I'd been trying for so long that it was making me frustrated and not thinking. :-)

It's not the first time I've used Sin/Cos btw.
I've made this; http://tardis.dk/omegarace.htm so I do 'get it' - just not this time though. :-)

- so but this sure helps.

Thanks again.
/S