When to use delta timing...

Monkey Forums/Monkey Programming/When to use delta timing...

therevills(Posted 2011) [#1]
I've got a bit of code and I think I am doing too much delta timing on it.

Here is the code without any delta timing:
if dx > 0
	if height <> 0
		dy += 0.3
		angle += amount
	End
	height += dy
	if height > 0
		height = 0
		amount = 0
		dx *= .5
		dy = 0 - (dy * 0.4)
		if dy >-1
			dy = 0
			height = 0
		End
	End
	if dy = 0
		dx -= 0.010
	End
	
	x += (Sin(angle) * dx)
	y += (Cos(angle) * dx)
end


I had delta timing everytime I added/multipled/subtracted... and now I am confussed even more :(

So where should I use delta timing in this code?


GfK(Posted 2011) [#2]
You should only need it on the two sin and cos lines at the end otherwise you'll be applying delta multiple times and end up with weird results. also you have dx on the cos line and it should be dy.


therevills(Posted 2011) [#3]
Thanks Dave... thats what I thought but it still wrong:



Using Jame's delta timing example, press left and right cursor keys to alter the framerate.

The green ball has delta timing.


therevills(Posted 2011) [#4]
This looks better:



if dx > 0
	if height <> 0
		dy = dy + 0.3 * delta
		angle = angle + amount * delta
	End
	height = height + dy * delta
	if height > 0
		height = 0
		amount = 0
		dx = dx / 2
		dy = 0 - (dy * 0.4)
		if dy >-1
			dy = 0
			height = 0
		End
	End
	if dy = 0
		dx -= 0.030 * delta
	End
	x = x + Sin(angle) * (dx * delta)
	y = y + Cos(angle) * (dx * delta)
end



luggage(Posted 2011) [#5]
I think you only need to do it on the last couple of lines as someone mentioned.

Try removing all the delta's and use something like.

Local velocityX:Float = Sin(angle) * dx
Local velocityY:Float = Cos(angle) * dx ' you use dx here in your code are you sure this isn't supposed to be dy?

x = x + (velocityX * delta)
y = y + (velocityY * delta)


You might need to play with your numbers though. The idea is that only when you move your player do you need to use your delta. So for example, say you wanted him to move 10 pixels per second then velocityX should be 10. You mulitply this value when you add it on to X so if 1 second had passed between the last frame and this one you'd jump 10 pixels. If 0.5 of a second had passed you'd move 5 pixels.


therevills(Posted 2011) [#6]
Thanks luggage, yeah normally it would be fine to add the delta to just the movement variables - but this is slightly different. I am trying to get a ball moving around the screen that looks a bit like Sensible Soccer eg 2d but when the ball is in the "air" you can see the shadow.

Have a play with the code in post #4.


matt(Posted 2011) [#7]
I can dig out the code to my Simple Soccer game if you like?

http://www.gingerbeardman.com/archive/soccer/ try the early bird download (PC only)


therevills(Posted 2011) [#8]
I can dig out the code to my Simple Soccer game if you like?


That would be cool - Cheer Matt!


matt(Posted 2011) [#9]
I'd be interested to know what you have planned! ;)

Type BallType							; The Ball Object Type.
	Field x#,y#							; Ball X and Y co-ords. 
	Field z								; Ball Z-coord.
	Field dx#,dy#						; Ball X and Y direction increments
	Field dz							; Ball Z increments
	Field frame							; The current ball frame.
End Type
properties should be self explanatory

Function draw_ball(posx, posy)
	;sensi style shadow
	DrawImage bmp_ball, posx+ball\z, posy, 4

	;sensi style ball
	DrawImage bmp_ball, posx, posy-ball\z, Abs(Int(ball\frame/5) Mod 4)
End Function
shadow is drawn with z property affecting x co-ordinate, ball is drawn with z affecting y co-ordinate. This gives the effect of "3D" ball movement in 2D.

I guess this means lighting source is exactly West in direction? I know Croteam's football games on the Amiga used multiple lighting sources and therefore had multiple ball shadows (which was a bit OTT and confusing for me). http://hol.abime.net/hol_search.php?N_ref_developer=123

Hope that helps!


therevills(Posted 2011) [#10]
Cool - can you show me how you move the actual ball?


matt(Posted 2011) [#11]
Just checked and I don't use delta timing at all.

;move ball in x and y directions
ball\x=ball\x+ball\dx
ball\y=ball\y+ball\dy
plus some friction/dampening applied each frame, and a check to clamp the speed to zero if it's close enough.


Foppy(Posted 2011) [#12]
This is a topic on blitzbasic.com from 2006 where Grey Alien asks whether to apply delta time to the value by which the acceleration itself is changed:

http://www.blitzbasic.com/Community/posts.php?topic=61530