I'll post the changes I've made. I haven't taken into account the performance of the program. Most surely it can be optimized quite a lot. Seeing your programming experience, I'm sure you're capable of it
1) Two important constants:
VB Code:
Public Const PI = 3.1415927 Public Const HALFPI = 1.5707963
2) I've extended your Star UDT:
Dist and ang will hold the distance and angle of the star, as seen from the center of the picture box (polar coordinates). These will be derived from x and y.VB Code:
Public Type Star Phase As Double Colour As wRGB DestX As Long DestY As Long Speed As Double LumCap As Long x As Long y As Long [b] dist As Long ang As Double[/b] End Type
3) Code modifications to make the starfield actually rotate
Dist is derived by simply applying Pythagoras.VB Code:
'COMMENT TO NOT SPIN! '.x = CentreX + Cos(Angle * (3.1415926 / 180)) * (CentreX - .x) '.y = CentreY + Sin(Angle * (3.1415926 / 180)) * (CentreY - .y) .dist = Sqr((CentreX - .x) * (CentreX - .x) + (CentreY - .y) * (CentreY - .y)) .ang = Arctan2(CDbl(CentreY - .y), CDbl(CentreX - .x)) .x = CentreX + Cos(.ang + Angle * (PI / 180)) * .dist .y = CentreY + Sin(.ang + Angle * (PI / 180)) * .dist 'COMMENT TO NOT SPIN!
Ang is derived by a modified Atn function (named Arctan2) (see modification 4)
X and Y are calculated accordingly.
Tip: to reverse the spinning you need to swap either the result of the Cosine or the Sine, not both.
4) The modified Atn function:
The advantage of this way is that, if you pass x and y separately (instead of y/x), you'll get the angle in the right quadrant of your coordinate system (instead of only the first or fourth quadrant).VB Code:
Public Function Arctan2(y As Double, x As Double) As Double If x = 0 Then If y > 0 Then Arctan2 = HALFPI Else Arctan2 = -HALFPI End If ElseIf x > 0 Then Arctan2 = Atn(y / x) Else If y < 0 Then Arctan2 = Atn(y / x) - PI Else Arctan2 = Atn(y / x) + PI End If End If End Function





Reply With Quote