Why, instead of doing a spinning effect, the DrawStar function in this code makes it stretch and skew. Could anyone work this out for me?
Printable View
Why, instead of doing a spinning effect, the DrawStar function in this code makes it stretch and skew. Could anyone work this out for me?
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
I've forgotten to mention a (literally!) side effect.
Since the starfield is rotating now, there will be some black spaces, depending on the rotation angle. If the angle is 45 degrees (or 135, 225, 315), there are four opposite black triangles in the corners. At angles of 90 and 270 degrees, there are two black sidebars. You'll need to remember the position of those stars as well. (A star might be able to rotate out and into the screen.)
The distance which you'll need to remember is sqr(2) * (longest side of your screen). In this case no black shapes will show at even square screens.
Hrm thanks, yeah it hasn't been optimised, I made this in like an hour, so what did you expect :p :rolleyes:
But thanks for that...
I still don't know why mine didn't work. Oh well.
Works like a charm. However, I can get the top and left destinations to work, but not the bottom and right. I'll figure it out, it only happens when I use the Sqr(2) thing.
You were scaling the starfield itself, not repositioning every individual star.
Because the position of all stars were modified the same way (by Cos(Angle * (3.1415926 / 180) or Sin(...)) the modification was the same for all stars.
This resulted in the rescaling and swapping of the entire starfield.
I dunno... okay I guess you're right, but it works and thats what I care :D