Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have an image I want to move like it's hovering up and own in a cycle. For that image I have the Y

float _sMY = 10;

My cycle is this:

if (!_hoveredDown)
{
   _sMY -= 0.1f;
}
else
{
   _sMY += 0.1f;
}   

if (_sMY < -10)
   _hoveredDown = true;
else if (_sMY > 10)
   _hoveredDown = false;

But during this cycle, the image stutters in certain positions and debugging told me that _smY goes way to -5.677342E-06.

Why do you think this is happening?

Fix, which won't work:

float hoverSpeed = 0.1f;
hoverSpeed += (float)(GameTime.ElapsedGameTime.TotalSeconds);

if (_hoveredDown)
{
   _sMY += hoverSpeed;
}
else if (!_hoveredDown)
{
   _sMY -= hoverSpeed;
}


if (_sMY < -10)
    _hoveredDown = true;
else if (_sMY > 10)
    _hoveredDown = false;
share|improve this question
3  
-5.677342E-06 is approx zero, well within your range. –  Kelly Thomas Jun 20 '13 at 12:11
    
Why does it stutter then? It goes like up, up, up, stutter, up, down down down, stutter, down –  IMX Jun 20 '13 at 12:14
    
Is the time delta per frame constant? If your code is based on a fixed timestep (as is appears to be) then any variation can make it appear to pause/slow down. –  Kelly Thomas Jun 20 '13 at 12:18
    
I think I changed that (see code) but still no changes –  IMX Jun 20 '13 at 12:36
1  
Usually for this kind of stuff I just do something like _sMY = 10 * sin( GameTime.ElapsedGameTime.TotalSeconds ) –  Tetrad Jun 20 '13 at 16:17

1 Answer 1

I noticed a number of strange things in your code. For one, you were adding the game time in seconds to the movement speed of the object. Since the game time is incremental, that would make the object move ridiculously fast after even just a few seconds, (possibly explaining the stutter)

Corrected code follows :

//definitions
float hoverSpeed = 0.1f;
//i removed the increment on the hover speed from here. 
//from what i saw, it made no sense and would make the object move more and more erratic.

bool _hoveredDown = false;
float _smY = 10;


//this is in your game update loop (i suppose)...
if (_hoveredDown)
{
    _sMY += hoverSpeed;
    // a boolean only has two states, so there's no need to check if it really
    // really really really is the other state if it is false.
    // so just a single if/else will do.
}
else
{
    _sMY -= hoverSpeed;
}

if (_sMY < -10)
{
   _hoveredDown = true;
}
else if(_sMY > 10)
{
  _hoveredDown = false;
}
share|improve this answer
    
That game time update gives the amount of time elapsed since the last tick in the game loop, not the total number of seconds the game has been active. –  ssb Jun 20 '13 at 16:30
    
Anyhow, this, unfortunately, doesn't not solve the problem –  IMX Jun 20 '13 at 17:41
    
@ssb Ok, but it still should not be added to the velocity of the object. So Imx, the jitter still happens if you don't do this? –  Timothy Groote Jun 20 '13 at 18:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.