Re: [Flashcoders] Time adjusted / Framerate independent animation…
Ok - i needed variable speed per frame (so animation keeps in time with possible frame lag) . I guess where I get confused is: how would i structure this if I wanted the follower to for example follow the mouse (with an eased delay)?… Here is the code for now ( which is currently not really adjusting for frame delay / variable speed ) private function enterFrame( event : Event ) : void { // Update Time dt = 0.001 * (getTimer() - t0); time+= dt; // // TIME CORRECTED var lSpeedPerSecond : Number = 30; var lSpeedPerFrameVariable : Number = lSpeedPerSecond * dt; var targetX : Number = ( ( spc.x - stage.mouseX ) / lSpeedPerSecond ); spc.x -= targetX; if ( 0 > spc.x|| ( spc.x + spc.width ) > stage.stageWidth ) spcDir = -spcDir; t0 = getTimer(); } Karim Beyrouti t: +44 (0) 7977 997 629 e: ka...@kurst.co.uk w: http://kurst.co.uk skype: karimbeyrouti 109 Timber Yard, Drysdale Street, Hoxton, London, N1 6ND On 26 Oct 2012, at 17:07, Hans Wichman wrote: > Hi Karim, > > i put them both in there, the one you are referring to is simpler: > ofcourse this value can be kept out of the enterframe loop, i have a utility > class for it (although it is a bit overkill;)) > > private function enterFrame( event : Event ) : void { > sp.x += speed * spDir; > if ( 0 > sp.x|| ( sp.x + sp.width ) > stage.stageWidth ) spDir = > -spDir; > > var lSpeedPerSecond:Number = 300; > var lSpeedPerFrameConstant:Number = lSpeedPerSecond / > stage.frameRate; > > spc.x += lSpeedPerFrameConstant* spcDir; > > if ( 0 > spc.x|| ( spc.x + spc.width ) > stage.stageWidth ) spcDir > = -spcDir; > > t0 = getTimer(); > } > > Anyway, basically frameindependent value is simply > SPEED_PER_SECOND/FRAMES_PER_SECOND. > > regards > Hans > > > On 26-10-2012 16:58, Karim Beyrouti wrote: >>> are you trying to do frame rate independent animation based on a fixed >>> frame rate (eg you switch from 30 to 60 and everything should move at the >>> same pace but more fluid >> Yep - that is the one… all my game physics is independent of framerate (time >> based)… however, I am trying to get other non-physics based animated >> objects/code >> to work the same way (i.e. the camera), and I am finding it a little >> difficult. >> >> Will try your solution - although it looks like it's for the other option >> (variable framerate)…. >> >> Thank you for helping…. >> >> >> >> Karim Beyrouti >> >> t: +44 (0) 7977 997 629 >> e: ka...@kurst.co.uk >> w: http://kurst.co.uk >> skype: karimbeyrouti >> >> 109 Timber Yard, Drysdale Street, Hoxton, London, N1 6ND >> >> On 26 Oct 2012, at 15:21, Hans Wichman wrote: >> >>> Hi, >>> >>> are you trying to do frame rate independent animation based on a fixed >>> frame rate (eg you switch from 30 to 60 and everything should move at the >>> same pace but more fluid), or trying to make up a variable framerate due to >>> running code? >>> >>> Anyway since you are keeping y constant I compressed the code a bit, is >>> this what you are looking for? >>> >>>private function enterFrame( event : Event ) : void { >>>sp.x += speed * spDir; >>>if ( 0 > sp.x|| ( sp.x + sp.width ) > stage.stageWidth ) spDir = >>> -spDir; >>> >>>var lSpeedPerSecond:Number = 300; >>>var lSpeedPerFrameConstant:Number = lSpeedPerSecond / >>> stage.frameRate; >>>var lSpeedPerFrameVariable:Number = lSpeedPerSecond * >>> (getTimer()-t0)/1000; >>> >>>spc.x += lSpeedPerFrameConstant* spcDir; >>>//spc.x += lSpeedPerFrameVariable * spcDir; >>> >>>if ( 0 > spc.x|| ( spc.x + spc.width ) > stage.stageWidth ) >>> spcDir = -spcDir; >>> >>>t0 = getTimer(); >>>} >>> >>> hth >>> jc >>> >>> >>> On 26-10-2012 15:29, Karim Beyrouti wrote: Hello Flash coders, It's been a little quite here of late… So, hope it's good with with you all. … I have been trying to figure this out for a day or so, and wondering if you can help. I am trying to time correct my animation to be independent of framerate, which is a little easier when dealing with physics (scaling velocity / position) than non physics animation… In my app physics is time corrected, however I have some code that is not physics based an am trying to have that running independently of framerate (and not really succeding). In the following demo: http://kurst.co.uk/transfer/timecorrection/TimeCorrection.swf I have two rects ( green and red ). The green one is being time corrected, and I would expect it to follow the red one ( as there is no lag in framerate )… ho
Re: [Flashcoders] Time adjusted / Framerate independent animation…
Thank you! That make sense. Will integrate this into my code ( now looking at my camera class ) … This is quite important, as if there is any frame lag, physics and other objects go out of sync. Karim Beyrouti t: +44 (0) 7977 997 629 e: ka...@kurst.co.uk w: http://kurst.co.uk skype: karimbeyrouti 109 Timber Yard, Drysdale Street, Hoxton, London, N1 6ND On 26 Oct 2012, at 17:07, Hans Wichman wrote: > Hi Karim, > > i put them both in there, the one you are referring to is simpler: > ofcourse this value can be kept out of the enterframe loop, i have a utility > class for it (although it is a bit overkill;)) > > private function enterFrame( event : Event ) : void { > sp.x += speed * spDir; > if ( 0 > sp.x|| ( sp.x + sp.width ) > stage.stageWidth ) spDir = > -spDir; > > var lSpeedPerSecond:Number = 300; > var lSpeedPerFrameConstant:Number = lSpeedPerSecond / > stage.frameRate; > > spc.x += lSpeedPerFrameConstant* spcDir; > > if ( 0 > spc.x|| ( spc.x + spc.width ) > stage.stageWidth ) spcDir > = -spcDir; > > t0 = getTimer(); > } > > Anyway, basically frameindependent value is simply > SPEED_PER_SECOND/FRAMES_PER_SECOND. > > regards > Hans > > > On 26-10-2012 16:58, Karim Beyrouti wrote: >>> are you trying to do frame rate independent animation based on a fixed >>> frame rate (eg you switch from 30 to 60 and everything should move at the >>> same pace but more fluid >> Yep - that is the one… all my game physics is independent of framerate (time >> based)… however, I am trying to get other non-physics based animated >> objects/code >> to work the same way (i.e. the camera), and I am finding it a little >> difficult. >> >> Will try your solution - although it looks like it's for the other option >> (variable framerate)…. >> >> Thank you for helping…. >> >> >> >> Karim Beyrouti >> >> t: +44 (0) 7977 997 629 >> e: ka...@kurst.co.uk >> w: http://kurst.co.uk >> skype: karimbeyrouti >> >> 109 Timber Yard, Drysdale Street, Hoxton, London, N1 6ND >> >> On 26 Oct 2012, at 15:21, Hans Wichman wrote: >> >>> Hi, >>> >>> are you trying to do frame rate independent animation based on a fixed >>> frame rate (eg you switch from 30 to 60 and everything should move at the >>> same pace but more fluid), or trying to make up a variable framerate due to >>> running code? >>> >>> Anyway since you are keeping y constant I compressed the code a bit, is >>> this what you are looking for? >>> >>>private function enterFrame( event : Event ) : void { >>>sp.x += speed * spDir; >>>if ( 0 > sp.x|| ( sp.x + sp.width ) > stage.stageWidth ) spDir = >>> -spDir; >>> >>>var lSpeedPerSecond:Number = 300; >>>var lSpeedPerFrameConstant:Number = lSpeedPerSecond / >>> stage.frameRate; >>>var lSpeedPerFrameVariable:Number = lSpeedPerSecond * >>> (getTimer()-t0)/1000; >>> >>>spc.x += lSpeedPerFrameConstant* spcDir; >>>//spc.x += lSpeedPerFrameVariable * spcDir; >>> >>>if ( 0 > spc.x|| ( spc.x + spc.width ) > stage.stageWidth ) >>> spcDir = -spcDir; >>> >>>t0 = getTimer(); >>>} >>> >>> hth >>> jc >>> >>> >>> On 26-10-2012 15:29, Karim Beyrouti wrote: Hello Flash coders, It's been a little quite here of late… So, hope it's good with with you all. … I have been trying to figure this out for a day or so, and wondering if you can help. I am trying to time correct my animation to be independent of framerate, which is a little easier when dealing with physics (scaling velocity / position) than non physics animation… In my app physics is time corrected, however I have some code that is not physics based an am trying to have that running independently of framerate (and not really succeding). In the following demo: http://kurst.co.uk/transfer/timecorrection/TimeCorrection.swf I have two rects ( green and red ). The green one is being time corrected, and I would expect it to follow the red one ( as there is no lag in framerate )… however as you can see - it's not really working. Using the following formula to correct it: var spcTargetPosition : Point = new Point() spcTargetPosition.x = ( spc.x + ( spc.x + 10 ) ) * speed * spcDir; spcTargetPosition.y = spc.y; dt = 0.001 * (getTimer() - t0); time += dt; t0 = getTimer(); spc.x = spc.x + ( dt * spcTargetPosition.x ); Here is the full code: http://kurst.co.uk/transfer/timecorrection/TimeCorrection.as I would love it if you can point out where I am going wrong... Best regards Karim Beyrouti t: +44 (0) 7977 997 629 e: ka...@kurst.co.uk w: http://kurst.co.uk skype: karimbeyrouti
Re: [Flashcoders] Time adjusted / Framerate independent animation…
Hi Karim, i put them both in there, the one you are referring to is simpler: ofcourse this value can be kept out of the enterframe loop, i have a utility class for it (although it is a bit overkill;)) private function enterFrame( event : Event ) : void { sp.x += speed * spDir; if ( 0 > sp.x|| ( sp.x + sp.width ) > stage.stageWidth ) spDir = -spDir; var lSpeedPerSecond:Number = 300; var lSpeedPerFrameConstant:Number = lSpeedPerSecond / stage.frameRate; spc.x += lSpeedPerFrameConstant* spcDir; if ( 0 > spc.x|| ( spc.x + spc.width ) > stage.stageWidth ) spcDir = -spcDir; t0 = getTimer(); } Anyway, basically frameindependent value is simply SPEED_PER_SECOND/FRAMES_PER_SECOND. regards Hans On 26-10-2012 16:58, Karim Beyrouti wrote: are you trying to do frame rate independent animation based on a fixed frame rate (eg you switch from 30 to 60 and everything should move at the same pace but more fluid Yep - that is the one… all my game physics is independent of framerate (time based)… however, I am trying to get other non-physics based animated objects/code to work the same way (i.e. the camera), and I am finding it a little difficult. Will try your solution - although it looks like it's for the other option (variable framerate)…. Thank you for helping…. Karim Beyrouti t: +44 (0) 7977 997 629 e: ka...@kurst.co.uk w: http://kurst.co.uk skype: karimbeyrouti 109 Timber Yard, Drysdale Street, Hoxton, London, N1 6ND On 26 Oct 2012, at 15:21, Hans Wichman wrote: Hi, are you trying to do frame rate independent animation based on a fixed frame rate (eg you switch from 30 to 60 and everything should move at the same pace but more fluid), or trying to make up a variable framerate due to running code? Anyway since you are keeping y constant I compressed the code a bit, is this what you are looking for? private function enterFrame( event : Event ) : void { sp.x += speed * spDir; if ( 0 > sp.x|| ( sp.x + sp.width ) > stage.stageWidth ) spDir = -spDir; var lSpeedPerSecond:Number = 300; var lSpeedPerFrameConstant:Number = lSpeedPerSecond / stage.frameRate; var lSpeedPerFrameVariable:Number = lSpeedPerSecond * (getTimer()-t0)/1000; spc.x += lSpeedPerFrameConstant* spcDir; //spc.x += lSpeedPerFrameVariable * spcDir; if ( 0 > spc.x|| ( spc.x + spc.width ) > stage.stageWidth ) spcDir = -spcDir; t0 = getTimer(); } hth jc On 26-10-2012 15:29, Karim Beyrouti wrote: Hello Flash coders, It's been a little quite here of late… So, hope it's good with with you all. … I have been trying to figure this out for a day or so, and wondering if you can help. I am trying to time correct my animation to be independent of framerate, which is a little easier when dealing with physics (scaling velocity / position) than non physics animation… In my app physics is time corrected, however I have some code that is not physics based an am trying to have that running independently of framerate (and not really succeding). In the following demo: http://kurst.co.uk/transfer/timecorrection/TimeCorrection.swf I have two rects ( green and red ). The green one is being time corrected, and I would expect it to follow the red one ( as there is no lag in framerate )… however as you can see - it's not really working. Using the following formula to correct it: var spcTargetPosition : Point = new Point() spcTargetPosition.x = ( spc.x + ( spc.x + 10 ) ) * speed * spcDir; spcTargetPosition.y = spc.y; dt = 0.001 * (getTimer() - t0); time+= dt; t0 = getTimer(); spc.x = spc.x + ( dt * spcTargetPosition.x ); Here is the full code: http://kurst.co.uk/transfer/timecorrection/TimeCorrection.as I would love it if you can point out where I am going wrong... Best regards Karim Beyrouti t: +44 (0) 7977 997 629 e: ka...@kurst.co.uk w: http://kurst.co.uk skype: karimbeyrouti 109 Timber Yard, Drysdale Street, Hoxton, London, N1 6ND Karim Beyrouti t: +44 (0) 7977 997 629 e: ka...@kurst.co.uk w: http://kurst.co.uk skype: karimbeyrouti 109 Timber Yard, Drysdale Street, Hoxton, London, N1 6ND ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Time adjusted / Framerate independent animation…
> are you trying to do frame rate independent animation based on a fixed frame > rate (eg you switch from 30 to 60 and everything should move at the same pace > but more fluid Yep - that is the one… all my game physics is independent of framerate (time based)… however, I am trying to get other non-physics based animated objects/code to work the same way (i.e. the camera), and I am finding it a little difficult. Will try your solution - although it looks like it's for the other option (variable framerate)…. Thank you for helping…. Karim Beyrouti t: +44 (0) 7977 997 629 e: ka...@kurst.co.uk w: http://kurst.co.uk skype: karimbeyrouti 109 Timber Yard, Drysdale Street, Hoxton, London, N1 6ND On 26 Oct 2012, at 15:21, Hans Wichman wrote: > Hi, > > are you trying to do frame rate independent animation based on a fixed frame > rate (eg you switch from 30 to 60 and everything should move at the same pace > but more fluid), or trying to make up a variable framerate due to running > code? > > Anyway since you are keeping y constant I compressed the code a bit, is this > what you are looking for? > >private function enterFrame( event : Event ) : void { >sp.x += speed * spDir; >if ( 0 > sp.x|| ( sp.x + sp.width ) > stage.stageWidth ) spDir = > -spDir; > >var lSpeedPerSecond:Number = 300; >var lSpeedPerFrameConstant:Number = lSpeedPerSecond / > stage.frameRate; >var lSpeedPerFrameVariable:Number = lSpeedPerSecond * > (getTimer()-t0)/1000; > >spc.x += lSpeedPerFrameConstant* spcDir; >//spc.x += lSpeedPerFrameVariable * spcDir; > >if ( 0 > spc.x|| ( spc.x + spc.width ) > stage.stageWidth ) spcDir > = -spcDir; > >t0 = getTimer(); >} > > hth > jc > > > On 26-10-2012 15:29, Karim Beyrouti wrote: >> Hello Flash coders, >> >> It's been a little quite here of late… So, hope it's good with with you all. >> … I have been trying to figure this out for a day or so, and wondering if >> you can help. I am trying to time correct my animation to be independent of >> framerate, which is a little easier when dealing with physics (scaling >> velocity / position) than non physics animation… In my app physics is time >> corrected, however I have some code that is not physics based an am trying >> to have that running independently of framerate (and not really succeding). >> >> In the following demo: >> http://kurst.co.uk/transfer/timecorrection/TimeCorrection.swf >> >> I have two rects ( green and red ). The green one is being time corrected, >> and I would expect it to follow the red one ( as there is no lag in >> framerate )… however as you can see - it's not >> really working. >> >> Using the following formula to correct it: >> >> var spcTargetPosition : Point = new Point() >> spcTargetPosition.x = ( spc.x + ( spc.x + 10 ) ) * speed * spcDir; >> spcTargetPosition.y = spc.y; >> >> dt = 0.001 * (getTimer() - t0); >> time += dt; >> t0 = getTimer(); >> >> spc.x = spc.x + ( dt * spcTargetPosition.x ); >> >> >> Here is the full code: >> http://kurst.co.uk/transfer/timecorrection/TimeCorrection.as >> I would love it if you can point out where I am going wrong... >> >> >> Best regards >> >> >> Karim Beyrouti >> >> t: +44 (0) 7977 997 629 >> e: ka...@kurst.co.uk >> w: http://kurst.co.uk >> skype: karimbeyrouti >> >> 109 Timber Yard, Drysdale Street, Hoxton, London, N1 6ND >> >> >> Karim Beyrouti >> >> t: +44 (0) 7977 997 629 >> e: ka...@kurst.co.uk >> w: http://kurst.co.uk >> skype: karimbeyrouti >> >> 109 Timber Yard, Drysdale Street, Hoxton, London, N1 6ND >> >> ___ >> Flashcoders mailing list >> Flashcoders@chattyfig.figleaf.com >> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > > ___ > Flashcoders mailing list > Flashcoders@chattyfig.figleaf.com > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Time adjusted / Framerate independent animation…
Hi, are you trying to do frame rate independent animation based on a fixed frame rate (eg you switch from 30 to 60 and everything should move at the same pace but more fluid), or trying to make up a variable framerate due to running code? Anyway since you are keeping y constant I compressed the code a bit, is this what you are looking for? private function enterFrame( event : Event ) : void { sp.x += speed * spDir; if ( 0 > sp.x|| ( sp.x + sp.width ) > stage.stageWidth ) spDir = -spDir; var lSpeedPerSecond:Number = 300; var lSpeedPerFrameConstant:Number = lSpeedPerSecond / stage.frameRate; var lSpeedPerFrameVariable:Number = lSpeedPerSecond * (getTimer()-t0)/1000; spc.x += lSpeedPerFrameConstant* spcDir; //spc.x += lSpeedPerFrameVariable * spcDir; if ( 0 > spc.x|| ( spc.x + spc.width ) > stage.stageWidth ) spcDir = -spcDir; t0 = getTimer(); } hth jc On 26-10-2012 15:29, Karim Beyrouti wrote: Hello Flash coders, It's been a little quite here of late… So, hope it's good with with you all. … I have been trying to figure this out for a day or so, and wondering if you can help. I am trying to time correct my animation to be independent of framerate, which is a little easier when dealing with physics (scaling velocity / position) than non physics animation… In my app physics is time corrected, however I have some code that is not physics based an am trying to have that running independently of framerate (and not really succeding). In the following demo: http://kurst.co.uk/transfer/timecorrection/TimeCorrection.swf I have two rects ( green and red ). The green one is being time corrected, and I would expect it to follow the red one ( as there is no lag in framerate )… however as you can see - it's not really working. Using the following formula to correct it: var spcTargetPosition : Point = new Point() spcTargetPosition.x = ( spc.x + ( spc.x + 10 ) ) * speed * spcDir; spcTargetPosition.y = spc.y; dt = 0.001 * (getTimer() - t0); time+= dt; t0 = getTimer(); spc.x = spc.x + ( dt * spcTargetPosition.x ); Here is the full code: http://kurst.co.uk/transfer/timecorrection/TimeCorrection.as I would love it if you can point out where I am going wrong... Best regards Karim Beyrouti t: +44 (0) 7977 997 629 e: ka...@kurst.co.uk w: http://kurst.co.uk skype: karimbeyrouti 109 Timber Yard, Drysdale Street, Hoxton, London, N1 6ND Karim Beyrouti t: +44 (0) 7977 997 629 e: ka...@kurst.co.uk w: http://kurst.co.uk skype: karimbeyrouti 109 Timber Yard, Drysdale Street, Hoxton, London, N1 6ND ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders