Click to See Complete Forum and Search --> : Rotation 360 degrees
JaredM
Nov 6th, 2001, 08:01 PM
Could anyone help me out with this, I can't seem to put the math together for it. Let's say you have a line object on a VB form. It has the properties X1, X2, Y1, Y2 for it's two points coordinates. Based on a timer how could you get the two points to rotate in a perfect circle by incrementing and decrementing the coordinate values? I really need to see it in code, because I haven't been able to apply anything involving geometry yet. Thanks.
kedaman
Nov 6th, 2001, 08:54 PM
I recommend you learn the geometry part first.
the cartesian coordinate (x,y) for a polar coordinate (a,r)
(a=angle and r=radius) is
r*cos(a),r*sin(a)
the angles used with the vb trigonometry functions are in radians, 2pi making a full revolution, pi/180 a degree.
write angle a as a function of the time a(t)=k*t and you can increment it as a=a+k;
JaredM
Nov 7th, 2001, 07:30 AM
Could you explain that a little better?
All the timer needs to do is increment or decrement by a factor which will be a variable that will be recalculated.
I want the points to move around the circumference of the cirlce. Maybe like a pixel or two around the circumference every timer_event.
What kind of things do I need to determine? Do I need the diameter, how do I get the angle you're referring to?
kedaman
Nov 7th, 2001, 08:13 AM
You need to think systematically...
Your situation is this:
You have a function of time, the spinning line is your function output and time is it's input. depending on what time it is, your line will be at a different state.
The two endpoints of the line needs to be determined, and from the center of the line (or circle as you see it) you draw the line trough it from one endpoint to the other. The angle at which the line is passing trough the center is a function of the time: a(t)=k*t
The radius is as you define it constant, it's up to you how big circle you want.
JaredM
Nov 7th, 2001, 11:37 AM
What is K? in k * t.
And this doesn't neccessarily have to be on a timer.
kedaman
Nov 7th, 2001, 11:56 AM
k is angular speed for the spin, what else would you like to apply this with?
jenk
Nov 7th, 2001, 04:17 PM
Hi ,see the atttached file , it's a early version of a 2D game
engine I wrote and has all those tricky maths stuff as functions
you will need your own grahics of course!
HTH
JaredM
Nov 8th, 2001, 07:47 AM
I appreciate the example, but it would take a week or two to review your code and understand every inch of it enough to even begin to figure out the geometry used.
How about when I get home I post the form I'm using and someone add the code to it. I'm sure it's not really extensive, it's just a matter of knowing the right math. (Which I don't know, I have gaps in my geometry knowledge.)
Thanks.
Nirces
Nov 8th, 2001, 10:39 AM
Coming Up, some the exact code needed (once i figure out where i left that draw circle with lines code i did 2 years ago)
ok (5min later)
asummings Line1 is your line Object.
Sub UpdateLineRotation(line1 as line, Speed as Double)
dim CenterX as integer
dim CenterY as Integer
if line.x2 > line.x1 then
CenterX = (line1.x2 - line1.x1) /2
else
CenterX = (line1.x1 - line1.x2) /2
end if
if line.y2 > line.y1 then
CenterY = (line1.y2 - line1.y1) /2
else
CenterY = (line1.y1 - line1.y2) /2
end if
CurrentAngle = 'some one fill in the trig here, i can't remember how to do it off hand
if currentAngle < 180 then
CurrentReverseAngle = 180 + CurrentAngle
else
CurrentReverseAngle = 180 - CurrentAngle
end if
Radius = line1.length /2
ftemp = currentAngle + speed '(in degree's)
line1.x1 = CentreX + (Radius * Cos(fTemp * (3.1415926 / 180)))
line2.y1 CentreY + (Radius * Sin(fTemp * (3.1415926 / 180)))
ftemp = currentReverseAngle + speed '(in degree's)
line1.x2 = CentreX + (Radius * Cos(fTemp * (3.1415926 / 180)))
line2.y2 CentreY + (Radius * Sin(fTemp * (3.1415926 / 180)))
Doupt if that will work, but should give you an idea
JaredM
Nov 8th, 2001, 11:35 AM
I hope some one does fill in the trig there, but anyway you've been extremely helpful. That is what I'm looking for. Let me know when you figure out that CurrentAngle algorithm, because I forgot my Trig like 3 years ago.
jenk
Nov 8th, 2001, 02:06 PM
Dim x1 As Long
Dim y1 As Long
Dim x2 As Long
Dim y2 As Long
Private Sub Form_Load()
x1 = Line1.x1
y1 = Line1.y1
x2 = Line1.x2
y2 = Line1.y2
Me.Show
End Sub
Private Function Track(ByRef X As Long, ByRef Y As Long, Angle As Integer, Optional Speed As Long, Optional Oppersite As Boolean)
'defaults
If Speed = 0 Then Speed = 1
'Track a point along a tragectory line
If Oppersite Then
X = (X - Sin(Angle / 57.2958) * Speed)
Y = (Y - -Cos(Angle / 57.2958) * Speed)
Else
X = (X + Sin(Angle / 57.2958) * Speed)
Y = (Y + -Cos(Angle / 57.2958) * Speed)
End If
End Function
Private Sub Timer1_Timer()
Track x1, y1, (Timer * 100) Mod 360, 100
Track x2, y2, (Timer * 100) Mod 360, 100
Line1.x1 = x1
Line1.y1 = y1
Line1.x2 = x2
Line1.y2 = y2
Line1.Refresh
End Sub
set timer1 interval to 1
x1 to 1800
x2 to 2000
y1 to 2500
y2 to 2500
:)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.