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