Page 1 of 1

How to create your own clock

PostPosted: Wed Nov 11, 2009 9:56 pm
by Lyberodoggy
Lots of AM users use the techniques described in the manual to display a timer or the current time to a frame, so I decided to help, by creating an analog clock simulator.

If you were reading BbB, then you may have read my article about Physics and Math in game development. It describes the techniques we are going to use with more details.

We are going to use two main formulas
position in a simple harmonic oscillation without starting phase at a certain time, which is given by this function:
posx(t)=Xmax*sin(2*PI/Period)
and the equation between the coordinates of a circle:
x^2+y^2=Ray^2
if you solve y, you get this: y=(Ray^2-x^2)^(1/2)
but the ray of the circle equals xmax.
Let's call them both A
Now, we 'll also need this one to determine the sign of y: sin(2*PI/Period)
The Period should be 60 seconds (1 minute)
Skiping further mathematical analysis we get these functions:

Code: Select all
function xpos(t)
PI=3.14159265
xpos=A*sin(2*PI*t/60)
End Function

function ypos(x,t)
PI=3.14159265
ypos=(A^2-x^2)^(1/2)
If cos(2*PI*t/60)>0 Then ypos=-ypos
End Function

for the coordinates of our clock's pointer.
We also have to somehow move the pointer. This can be done using these subs (the last one isn't actually needed for this step but we 'll need it later):

Code: Select all
Sub ClockTick
sec=sec+1
If sec=60 then sec=0 : min=min+1
If min=60 then min=0 : hr=hr+1
If hr=24 then hr=0
xps=xpos(sec)
yps=ypos(xps,sec)
UpdateCo xps, yps
End Sub

Sub UpdateCo(xp,yp)
LineObject(1).x2=LineObject(1).x1+xp
LineObject(1).y2=LineObject(1).y1+yp
End Sub

Sub UpdateTime
middle=" "
If sec mod 2=1 then middle=":"
If min <10 then mindig="0"
If hr <10 then hrdig="0"
Text(1).Caption=hrdig+cstr(hr)+middle+mindig+cstr(min)
End Sub
.

So we have all of these procedure's in our project's vbs procedures. Now what? Now we must actually create the clock. This can be done by simply using a LineObject with one statical point and one dynamical.

Create the integers: A, sec, min, hr
These are the only global variables we are going to use, however only A is necessary if you change the code a bit.
Go to the frame's properties, then paste this code (explained with comments):

Code: Select all
LoadControl LineObject(1) 'create the line
LineObject(1).x1=GetProjectWidth/2*15   'place the statical point to the center
LineObject(1).y1=GetProjectHeight/2*15 ' you can change this with whatever you wish
LineObject(1).visible=True
A=100*15 'how long the pointer is going to be. Just change 100 with whatever
sec=second(now) 'this gets the current time. You can set it to 0 if you want
min=minute(now) 'to count the game's time instead of the real life's time
hr=hour(now)
UpdateCo xpos(sec),ypos(xpos(sec),sec) 'this one sets the starting position
Action.CreateTimedEvent 1, "ClockTick", True 'creates the clock
Action.CreateTimedEvent 0.1, "UpdateTime", True 'creates the updater

Guess what... That's it! :)
You can customize this as much as you wish :)

Hope you liked this tutorial
The attachment is a sample project

PostPosted: Thu Nov 12, 2009 1:53 am
by Candle
Nice one.

PostPosted: Thu Nov 12, 2009 2:42 pm
by Imari
Lyberodoggy, that was pretty impressive. You're actually creating the "line object"/clock hand with your code? Amazing. It picked up my local time and scaled perfectly.

PostPosted: Thu Nov 12, 2009 4:14 pm
by ShadowHunter
Sweet :D

PostPosted: Thu Nov 12, 2009 5:07 pm
by Lyberodoggy
Thanks guys :D
glad you liked it

Imari wrote:You're actually creating the "line object"/clock hand with your code?


Yep the code is loading a lineobject control in the control array and then anchors the first point in the center of the frame and uses the other to simulate the clock hand

You can actually use more similar code to simulate the other two clock hands too. Also you can change the color and width of the line using the vbline properties of the LineObject:

LineObject(1).bordercolor=value
some possible values: vbred, vbyellow, vbblack
LineObject(1).borderwidth=value
some possible values: 1 (default), 2, 5


EDIT: probably because I was writting the code around midnight there was a tiny bug, which I fixed. Actually due to mistyping the first 0 digit of the hours was going to the minutes and vice versa so when it was 18:06 it read 018:6

Anyway, fixed now :)

PostPosted: Fri Nov 13, 2009 12:27 am
by reneuend
Nice work!
cool way to build a clock.

PostPosted: Sun Nov 15, 2009 9:59 pm
by mercedes
Niiiice one....really cool...:D..way to go..very inventive!~