Psychho Posted October 4, 2013 Share Posted October 4, 2013 requestAnimationFrame, setTimeout and setInterval all run the function you give them asynchronously, does anyone know of a simple workaround/alternative?I cant be the only one who ran into problems with this. Quote Link to comment Share on other sites More sharing options...
YellowAfterlife Posted October 4, 2013 Share Posted October 4, 2013 Synchronous timing functions? Why would you want those? Of course you can insert an old-fashioned "while (Date.now() < start + delay);" loop, but this is not a good strategy - since JavaScript code is ran in single thread, doing so will efficiently block either tab or whole browser for the time of waiting. Quote Link to comment Share on other sites More sharing options...
Psychho Posted October 4, 2013 Author Share Posted October 4, 2013 Synchronous timing functions? Why would you want those?Of course you can insert an old-fashioned "while (Date.now() < start + delay);" loop, but this is not a good strategy - since JavaScript code is ran in single thread, doing so will efficiently block either tab or whole browser for the time of waiting.Gotta always run the physics simulation before/after rendering but instead its running them asynchronously creating a really bad shaking effect on moving objects.Also I tried using a while loop close to what you described that breaks every 10 updates to call a requestAnimationFrame(so the tab doesn't freeze) it just caused random extreme lag and even worse shaking/jittering . Jittering/shaking is normally not a problem except for when the browser keeps rendering at 30fps and is running physics at around 4fps -__-.No clue how this is happening when my code is justfunction Update() { Physics(); Render(); requestAnimationFrame(Update);}Video of the jittering aswell, just cause http://www.youtube.com/watch?v=FPJvA7NHolg Quote Link to comment Share on other sites More sharing options...
YellowAfterlife Posted October 4, 2013 Share Posted October 4, 2013 Having code structured likerender()handle_physics()Would have ensured that physics-related code is ran -after- rendering code. Although, provided that part of code remains synchronous, this most likely would not solve your problem.Perhaps a setTimeout(handler, 0) would help at running code "just after" rendering.I heavily doubt that this is the source of problem though. Psychho 1 Quote Link to comment Share on other sites More sharing options...
Psychho Posted October 4, 2013 Author Share Posted October 4, 2013 Having code structured likerender()handle_physics()Would have ensured that physics-related code is ran -after- rendering code. Although, provided that part of code remains synchronous, this most likely would not solve your problem.Perhaps a setTimeout(handler, 0) would help at running code "just after" rendering.I heavily doubt that this is the source of problem though. Nope didn't make a difference, also uploaded a video above of the jittering.Just tried the code below as-well and it didn't make a difference function Update() { setTimeout(Physics,0); Render(); requestAnimationFrame(Update);}EDIT: Found a fix, put physics and rendering in two seperate requestAnimationFrame calls lol Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.