At last you can have curvevalue and curveangle in your programs without worrying about variable frame rates!!! This is where you tell me I could have done it some really easy way
...
remstart
Time Based Curvevalue and Curveangle functions
By Joseph Thomson
23/7/04
If you've ever used the curvevalue and curveangle functions in a program with variable
frame rate, then you'll be aware that they don't work too well under changes in speed.
These functions act exactly like the DBPro functions, but they are loop speed independent.
You need to have a global variable called loopSpeed, which is linear and raises when the
loop speed slows, and lowers when it speeds up.
I don't guarentee that the equation is the most efficient way to do this, but it works at
least :).
Please include me in your credits where nesecarry. Thanks
remend
`Variables
global loopSpeed as float
global wrapangle as float = 0.0
global syncrate as integer = 60
`Set sync rate
sync on
sync rate 60
autocam off
`Make pointer
make object cone 1,10
xrotate object 1,90
fix object pivot 1
make object cone 2,10
xrotate object 2,90
fix object pivot 2
ghost object on 2
`Camera
position camera 0,15,0
xrotate camera 90
do
if inkey$()="1" then mode = 0
if inkey$()="2" then mode = 1
wrapangle = wrapvalue(wrapangle+(keystate(52)-keystate(51))*2.0)
rotate object 2,0,wrapangle,0
`Rotate pointer with left and right keys
if leftkey() = 1
rotate object 1,0,wrapvalue(object angle y(1)-loopSpeed*2),0
else
if rightkey() = 1
rotate object 1,0,wrapvalue(object angle y(1)+loopSpeed*2),0
else
`Otherwise use curve functions to put it back
if mode = 0 then rotate object 1,0,TCurveAngle(wrapangle,object angle y(1),20),0
if mode = 1 then rotate object 1,0,TCurveValue(wrapangle,object angle y(1),20),0
endif
endif
`Control sync rate
syncrate = syncrate + upkey()-downkey()
if syncrate < 5 then syncrate = 5
sync rate syncrate
`Calculate loopSpeed variable
loopSpeed = 60.0/1000.0 * (timer()-time)
time = timer()
`Text info
if mode = 0 then text 0,0,"CurveAngle Mode. Press 2 to switch to CurveValue mode"
if mode = 1 then text 0,0,"CurveValue Mode. Press 1 to switch to CurveAngle mode"
text 0,20,"Set Sync Rate: "+str$(syncrate)
text 0,40,"Actual FPS: "+str$(screen fps())
sync
loop
`Curvevalue function. Requires loopSpeed variable
function TCurveValue(dest as float, current as float, speed as float)
local diff as float
local value as float
`Find difference between destination and current position
diff = dest - current
`Caluclate new value
value = current + (diff * ((speed^loopSpeed-(speed-1)^loopSpeed)/(speed^loopSpeed)))
endfunction value
`Curveangle function. Requires loopSpeed variable
function TCurveAngle(dest as float, current as float, speed as float)
local diff as float
local value as float
local tempCurrent as float
`Wrap current around so that it is as if destination is 0
tempCurrent = wrapvalue(current - dest)
`See if it's in the first half (<180) or the second half (>180) and assign movement amounts
if tempCurrent <= 180.0
diff = -tempCurrent
else
diff = 360-tempCurrent
endif
`Caluclate new value
value = wrapvalue(current + (diff * ((speed^loopSpeed-(speed-1)^loopSpeed)/(speed^loopSpeed))))
endfunction value
Isn't it? Wasn't it? Marvellous!