   problem with alignment of speed
  Hi folks,At the moment I have the option to make my game 60 frames or 30 frames or no predefined number of frames. With 60 frames it works but when the frame is a little bit longer than 1/60 of a second it slows immediatly to 30 frames a second. With this my speed drops down also by a half. The reason for this is that I do not use framepersecond but just expect it to be in 60 frames per second.Now the third option is to calculate the length of 1 frame in time. This length can be used to let the speed be adjusted correctly. Example: in normal 60 frames I do 1 step per frame, this means 1/60 of a second. but if it is 1/40 a second then the speed step will be 1.5 per frame.the problem which I encounter is the following: I do the above, adjusting the speed to the time spend in a frame. I do this however with the time of the previous frame ( a frame is calculated by getting the time at the beginning and the end of the frame. ) so when 1 frame is very slow and the next is quick then the slow value is used in the quick frame which is not correct behaviour.where am I going wrong or what should I do to correct this ?greets ghotirein   
  Assuming that the frame is constant (60 or 30) isn't correct and ofcourse you will never get the right speed. Also, using the last frame's time to calculate on your current frame has also the defect that you mentioned, that the frames aren't of equal lengths due to some of the internal calculations which could slow down one frame. So if you average the times of each frame, by calculating the FPS of the previous second, you will get a relatively stable frame rate that you can rely on with your calculations   ;  Hi, so I should average it? but then I still get the same result only slightly smaller.Is there not a way of setting the frames per second or something in such a way lets say, setting the time to 1/40 of a second and when at the end this length is not reached, wait untill it does and then on to the next frame? is this a way which can work ?   ;  I think what you are saying can work. You just need to use timer functions to get the current time and then after the update wait until enough time has passed.A more advanced technique is to make the framerate independent of the game updates. By storing the previous state and the current state, you can interpolate between them as time passes without having to compute a new state. This is nice for 3d games to help get higher framerates when the system can handle it. For simple 2d games, it might be better to just use a fixed rate like 30 fps.   ;  Hi can you elaborate more about that advance technique?, it sounds like the way i need :)I am making a 3D racing game on a console so it should fit the purposehope you can elaborate or maybe there is a tutorial or something about it ?   ;  Hi, not 100% sure of what you require, but from what i gather the speed is different at various frame rates, have you tryed getting the frame time set into a variable, then each frame update each speed by multiplying it by this variable, this should mean that the speed is fluent and not reliant on the number of frames per second.hope this helps   ;  This may be a stupid answer, but it maybe something to do with vertical blanking.Probably at driver level. When you draw and it takes less than 1/60 seconds all is fine, but if it goes over, then you miss a vertical blank, and for some reason, you have to wait for the second blank hence the 1/30 frames.Why it does this, I have no idea, but it may be something to do with the driver.;  Hi everyone, thanks for the replies;@azrealhk: I know that it waits for the next virtical blanking and that is why it goes from 60 to 30 and that is not really the problem, the problem is that I want to shut it down so that it does not wait for a vertical blanking. This makes the frame of variable length and thus speed for example has to be aligned correctly to let it move smooth@terry_burns85: Well that is what I did but picture this: I am at Frame T, at the end I retrieve its duration and I found out that it took (fictional value) 2 seconds, so the next Frame T+1 I will multiply(or whatever) the speed by 2 seconds, however at the end of T+1 I retrieved the length as 0.5 seconds. Now I have multiplyed the speed in a Frame of 0.5 second with the factor belonging to 2 seconds and thus it is messed up. See what my problem is? (can be that I am mistaken or so, please let me know.)@all: Maybe the best method is to just get the frame length long enough to let it get to the 30 fps state and then hardcode the speed and stuff, that may be the best approach ?greets ghotirein;Quote:Original post by ghotireinHi everyone, thanks for the replies;@azrealhk: I know that it waits for the next virtical blanking and that is why it goes from 60 to 30 and that is not really the problem, the problem is that I want to shut it down so that it does not wait for a vertical blanking. This makes the frame of variable length and thus speed for example has to be aligned correctly to let it move smoothThen disable it (although you must understand that the end user can enable it again whatever your application says). On D3D this is done by setting the correct presentation interval when you create the device. On OpenGL, I just don't know.Quote:@terry_burns85: Well that is what I did but picture this: I am at Frame T, at the end I retrieve its duration and I found out that it took (fictional value) 2 seconds, so the next Frame T+1 I will multiply(or whatever) the speed by 2 seconds, however at the end of T+1 I retrieved the length as 0.5 seconds. Now I have multiplyed the speed in a Frame of 0.5 second with the factor belonging to 2 seconds and thus it is messed up. See what my problem is? (can be that I am mistaken or so, please let me know.)That sounds a bit strange. Is your simulation time tied to your rendering time? if it's the case then you are (more or less) in trouble, especially in the PC land. In the console land, you can target a fixed framerate - but doing this on PC is way harder because you have to consider the different hardware that your users may have - and thus you can take advantage of the fixed framerate to run the simulation too. For PC games, simulation time is usually not tied to the rendering time. That allows you to get the simulation right independently of the rendering speed (you just render as fast as you can, using the latest simulation result if the next is not yet available).Ideally, your game should run at a "somewhat constant" framerate - on PC. If it's not then try to simplify the scene/geometry or try to identify the bottleneck in your simulation/rendering pipeline that slows everything. If your framerate skyrocket then limit it to an acceptable value which is no higher than 15% of the average framerate. The idea behind this is to give the user the same feeling through the game. If the framerate varies a lot, the user will find this rather disturbing. Quote:@all: Maybe the best method is to just get the frame length long enough to let it get to the 30 fps state and then hardcode the speed and stuff, that may be the best approach ?Hardcode? What a harsh word [grin] (but valid POP1 practice).1: POP is Programming Oriented Programming. Intended for people who don't want to play with better programming paradigm [grin]. POP features cool stuff such as spaghetti code, hardcoded constants, bad but efficient naming scheme (why would one call the player instance "player" when "p" is just enough?) and other niceties that help to speed up development.   ;Quote:Original post by VorpyA more advanced technique is to make the framerate independent of the game updates. By storing the previous state and the current state, you can interpolate between them as time passes without having to compute a new state. This is nice for 3d games to help get higher framerates when the system can handle it. For simple 2d games, it might be better to just use a fixed rate like 30 fps.ghotirein, I would advise you to use the interpolation method quoted. I find it useful because it's simple enough to get the exact location, state, etc of everything that affects the drawing, just as long as you know how it's changing, and the time and state of it at any time. Just think about, say, a car travelling. If you know what time it started and what speed (Presuming a constant speed), you can work out the distance from the starting point (Rotation, of course, is simple enough to implement).One thing which I find very important with this, is that you shouldn't need to worry about rounding errors. With a set framerate method, every time a number gets rounded (If only very slightly) it becomes further and further away from the real value -  since the previously rounded number gets processed, then rounded yet again.Of course, if a set framerate is more relevant to your game, and you can get around the troubles you're having, it makes more sense to do that, and save you the hassle of re-writing code you don't need to.Quote:1: POP is Programming Oriented Programming. Intended for people who don't want to play with better programming paradigm . POP features cool stuff such as spaghetti code, hardcoded constants, bad but efficient naming scheme (why would one call the player instance "player" when "p" is just enough?) and other niceties that help to speed up development.Unless I've overlooked something here, constants can only be hardcoded (or do you mean using literal numbers in multiple places, rather than defining a constant and referencing that?). The other two I am commonly guilty of, but only because my calculator uses BASIC and only supports single letter variables - in addition to up to 6 arrays.   