How to create your own clock

This forum is meant for people to share their knowledge by writing their own tutorials for Adventure Maker.

Moderators: time-killer-games, Vengeance66, Candle, reneuend, GM-Support

How to create your own clock

Postby Lyberodoggy » Wed Nov 11, 2009 9:56 pm

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
Attachments
Clock.rar
fixed one tiny bug
(69.66 KiB) Downloaded 443 times
Last edited by Lyberodoggy on Thu Nov 12, 2009 5:13 pm, edited 2 times in total.
User avatar
Lyberodoggy
Administrator
 
Posts: 2526
Joined: Sat Feb 17, 2007 3:31 pm
Location: Athens

Postby Candle » Thu Nov 12, 2009 1:53 am

Nice one.
Please don't PM me questions, ask here in the forums!
``````````````
Between grand theft and a legal fee, there only stands a law degree.
User avatar
Candle
Administrator
 
Posts: 3077
Joined: Sat Feb 07, 2004 2:21 am
Location: Rudy's Bar

Postby Imari » Thu Nov 12, 2009 2:42 pm

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.
User avatar
Imari
VIP
 
Posts: 872
Joined: Fri Jun 20, 2003 4:49 pm
Location: Virginia, USA

Postby ShadowHunter » Thu Nov 12, 2009 4:14 pm

Sweet :D
User avatar
ShadowHunter
Forum Admin and Games Page admin
 
Posts: 1304
Joined: Fri Jun 06, 2003 10:37 pm
Location: Belgium

Postby Lyberodoggy » Thu Nov 12, 2009 5:07 pm

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 :)
User avatar
Lyberodoggy
Administrator
 
Posts: 2526
Joined: Sat Feb 17, 2007 3:31 pm
Location: Athens

Postby reneuend » Fri Nov 13, 2009 12:27 am

Nice work!
cool way to build a clock.
---


Image
Image
User avatar
reneuend
Administrator
 
Posts: 2762
Joined: Sat Nov 22, 2008 8:37 pm
Location: Midwest Cornfield, USA

Postby mercedes » Sun Nov 15, 2009 9:59 pm

Niiiice one....really cool...:D..way to go..very inventive!~
User avatar
mercedes
VIP
 
Posts: 2460
Joined: Sun Mar 09, 2008 10:43 pm
Location: Canada..~


Return to Post Your Own Tutorials

Who is online

Users browsing this forum: No registered users and 0 guests

cron