Help: Implementing Wave's FPS and DeltaTime

BlitzMax Forums/BlitzMax Beginners Area/Help: Implementing Wave's FPS and DeltaTime

WarpZone(Posted 2005) [#1]
Okay, I'm having trouble implementing Wave's DeltaTime type. I thought I followed his instructions exactly, but I'm very new at this so who knows?

If my implementation is correct, then maybe the type itself is buggy?

main program:


Pretty much the only lines that refer to DeltaTime are these two lines from the update method of TypeActor:

x = x + (xMove*Delta.Time())
y = y + (yMove*Delta.Time())

For some reason, the turtle moves but the snake does not. Also, when the turtle hits the left side of the screen, it freezes. The player never moves. If I take out the reference to DeltaTime(), everything works as expected.

Here are the contents of FPS.bmx and DeltaTime.bmx, just in case I screwed something up while transcribing them:

FPS.bmx:


Delta Time


(Yes, I'm probabaly going to keep asking questions every step of the way on this simple little demo game until I figure out what the heck I'm doing.)


QuietBloke(Posted 2005) [#2]
I think the problem is that variables default to be integers.
If you change the type sprite x and y variables to be floats

ie.

Field x:Float
Field y:Float

it should work.


WarpZone(Posted 2005) [#3]
Let's see...

Yep! :)

But, MAN, that sucks that int/float issues like that can sneak up on you like that. :x How can you tell when an issue like that is going to occur? And what about Strict? I thought the whole point of Strict was that it would force you to remove ambiguities and declare everything precisely?

Strangely enough, it doesn't seem to care whether xMove and yMove are declared floats or not. Why is that?

My int score is too low for programming. :( I'm gonna go drink a float. :)


WarpZone(Posted 2005) [#4]
If anyone cares, here is Wave's DeltaTime code, documented a bit more concisely (in my opinion):




QuietBloke(Posted 2005) [#5]
You set the xmove and ymove to and integer values so unless you want to use fractions for them they are fine.

The reason is :
When you calculate the new values for x and y you have have the calculation

x = x + (xMove*Delta.Time())
y = y + (yMove*Delta.Time())

Delta.Time() is a floating point ( The # is the same as declaring it as :Float )
because you are multiplying an integer value with a floating point blitzbasic makes the result a floating point which you then add to x ( which you have now made into a floating point value ).

There have been various threads in these forums regarding the definition of Strict and personally I agree with you that strict should 'force' you to declare the type of every variable and it should not default to integer. Or at least it should compile with a warning message for every variable that it defaults on you behalf.

Once you are aware that the default is an integer then if things go wrong you know where to look. Either that or get into the habit of fully specifying every variable you declare.


WarpZone(Posted 2005) [#6]
Okay. So int x float = float, but int + float = ERROR.

Gotcha. Thanks. :)

Is that how it works in Blitz3D, too?


Curtastic(Posted 2005) [#7]

Okay. So int x float = float, but int + float = ERROR.

Gotcha. Thanks. :)

Is that how it works in Blitz3D, too?

works almost the same it blitz3d and C++.



I made a table on float and int :)

int + float = a nice new float
int=float ' =possible loss of data
therefore int=int+float ' =loss of data probably!

a:int = c:float 'loss of data after c's decimal
d:float = c:float 'fine

a:int = b:int * c:float 'loss of data after the result's decimal
d:float = b:int * c:float 'fine

a:int = b:int + c:float 'loss of data after the result's decimal
d:float = b:int + c:float 'fine


Jay Kyburz(Posted 2005) [#8]
I declare everything so that i dont run into these errors.


Tibit(Posted 2005) [#9]
Jay, Hope it works now =)

Also don't miss that when using deltatime you need to apply it to everything that you increase every frame. Like X:+ XMove *Delta.Time() but also XMove:+ XAcceleration or Sheild:+ SheildRecharge*Delta.Time()

If you multiply a int with a float you get a float.
Ex:
local X# = 15/7 ' This will always return a int (=2) even though X is a float and the result is a float, because both values are ints. Compared to:
local X# = 15/7.0 Will make it work the right way. Because one of the values is a float - 7.0, Same goes with varables.

I declare everything so that i dont run into these errors.
That's what you have to do when you use Strict. Because:
Local X Will declare X as an Int, it's the default.
Local X# Will delcare it as a float.
Local X:Int - Declare as an Int (Same as first)
Local X:Float - Delcare as a Float (text style) same as X#

WarZone I'll add those comment to the delta function next time I update the guide, if that's ok. I'll take a look at the DeltaTime example too, perhaps I can make it more clear.

Also the Delta Function (Any code, not text, you find in the guide) is Public so you don't need to refer to me, you may also change it however you want and then sell it ;)


WarpZone(Posted 2005) [#10]
Wave, you are quite welcome to use my comments when you update your tutorial. :) Your long comments are great for users just learning how to write code, so it might be a good idea to include both versions. My brief comments are just for lazy people who just want your DeltaTime solution in all their games right now. :p

Now there is truly no excuse not to use DeltaTime! XD


Tibit(Posted 2005) [#11]
Perfect, though I won't update the guide until mid september.