'Fixed Timestep' wrap around problem

Started by
5 comments, last by apatriarca 10 years, 11 months ago

I've been working on a personal 2d HTML5 Canvas engine, and I am trying to implement Glenn Fiedlers fixed time step algorithm:

http://gafferongames.com/game-physics/fix-your-timestep/

I have read through these other examples of how Box2D did it:

http://www.unagames.com/blog/daniele/2010/06/fixed-time-step-implementation-box2d

http://blog.allanbishop.com/box-2d-2-1a-tutorial-part-10-fixed-time-step/

The problem I am having is that objects don't wrap around the edges of the screen properly.

For example, if I have a scrolling background, and the position gets to greater than 800, and I want to wrap it back around to 0, you can see it visually moving there, rather than instantly appearing.

I am thinking, perhaps i don't know the proper way to implement this awesome algorithm.

Here is a basic pseudo code of what I am doing...


function update(){
 
// code to determine time passed, etc
 
 while (accumulated >= TIC) {
  resetSmooth();
  fixedUpdate( TIC );
  accumulated -= TIC;
 }
 ticLeft = accumulated / TIC;
 render(ticLeft)
}
 
function resetSmooth(){
 renderPos = oldPos = currentPos;
}
 
function fixedUpdate( dt ){
 oldPos = currentPos;
 currentPos += velocity;
}
 
function render (ticLeft){
 smooth = 1.0 - ticLeft;
 renderPos = (currentPos * ticLeft) + (oldPos * smooth)
}

The smoothing is awesome, but it is just the wrap around warping that I need help figuring out. Also, in my collision detection and what not, do I use currentPos, oldPos, or renderPos ? biggrin.png

Thank you for any help, and here is a quick demo of what I am talking about:

http://www.buff.bunzaga.com

It works in IE 10.0.4, Moz 20.0.1, and Chrome 26.0.1410.64 m

Advertisement

You need to set the position to the new location without changing the velocity. Dunno if that would cause problems with the physics though, since the object basically "teleports" to a new position...

If you want to see an object which straddles the edges when it wraps around the screen (like the ship in asteroids) you can do that by having 4 objects placed at the corners of the screen distance apart that move in tandem (that way you get to see half of the ship on each side of the screen while it is moving off the edges). Objects which are more than one screen size distance away from the edge of the screen need to be repositioned so they are less than one screen distance away though, using the same method as above.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Hey thank you for the reply Paradigm Shifter. Yes I wanted a similar effect, such as in Asteroids, only I don't need it to show the remaining ship as it wraps. Just a simple...

if > screen width, put back to 0. The problem is that due to the interpolation, you can actually SEE the re-positioning, rather than it just appearing instantly on the other side.

In my fixed update, it just checks...

if arrowPos.x > screen width, set it to 0. If arrowPos.x < 0 set it to screen width.

I am new to using an interpolation such as this, so I haven't had to deal with this sort of behavior before.

When the ship gets to the screen width, do I set ALL of them to 0? Current Position, Previous Position, and Rendered(interpolated) Position?

If you don't care about showing part of the ship on the other side, you can simply test the ship position with each edge and then teleport both the new position and the old one to the other side of the screen. In this way the ship seem to enter from the edge. Alternatively you can implement the wraparound logic after the interpolation step, i.e. storing an "absolute" ship position and then computing the modulus when computing the ship position on the screen.

....teleport both the new position and the old one to the other side of the screen. In this way the ship seem to enter from the edge. Alternatively you can implement the wraparound logic after the interpolation step, i.e. storing an "absolute" ship position and then computing the modulus when computing the ship position on the screen

I currently have implemented the first suggestion, but would like more information on how I would go about doing your second suggestion properly. biggrin.png I don't expect you guys to do the work for me, but just a keyword or two to look up in google would be highly appreciated.

So in my demo, I have 'previous', 'current', and 'rendered'(a better word would be interpolated). In your second idea, I would use the interpolated position AFTER the physics calculation, not DURING it. I like how that sounds. Something like...


 while (accumulated >= TIC) {
  interpolated = oldPos = newPos
  newPos += TIC * Velocity
  accumulated -= TIC
 }
 ticLeft = accumulated / TIC

 smooth = 1.0 - ticLeft
 interpolated = (newPos * ticLeft) + (oldPos * smooth)
// handle the wrap around logic here?
// if interpolated > screen extents, set interpolated & oldPos & newPos to the teleported position?

The alternative suggested is keep all world coordinates in absolute terms (i.e. don't wrap them around by teleporting the ship), but then when it comes to drawing the ship map the coordinates into the screen width/height range using the modulo operator or fmodf.

Problem with that method is keeping the other objects near to the player so the collisions etc. still work.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

It's the wraparoud logic itself which complicates collision detection. I don't think the use of absolute coordinates adds much more complexity than managing several copies of each object. I have however to admit I have never implement an asteroid close using this strategy. The use of several copies is necessary* if one want to show parts of the object on the other side.

* when using shaders is probably possible to pass the absolute position and then create the different copies on the GPU but I'm not sure if it's worth the effort.

This topic is closed to new replies.

Advertisement