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.

//edit after running it through the debugger it turned out i had the display function set to x,x...TIL how to use a debugger

I've been trying to teach myself C++ SDL with the lazyfoo tutorial and seem to have run into a roadblock. The code below is the movement function of my Dot class, which controls the ball. The ball seems to ignore yvel and moves with xvel to the bottom right. The code should be pretty readable, the rest of the relevant facts are:

  • All variables are names
  • Constants are in caps
  • dotrad is the radius of my dot
  • yvel and xvel are set to 5 in the constructor
  • The dot is created at x and y equal to 100
  • When I comment out the x movement block it doesn't move, but if i comment out the y movement block, it keeps on going down to the right.

    void Dot::move()
    {
       if(((y+yvel+dotrad) <= SCREEN_HEIGHT) && (0 <= (y-dotrad+yvel)))
        {
          y+=yvel;
        }
      else
        {
           yvel = -1*yvel;
        }
       if(((x+xvel+dotrad) <= SCREEN_WIDTH) && (0 <= (x-dotrad+xvel)))
        {
          x +=xvel;
        }
      else
        {
          xvel = -1*xvel;
        }
    }
    
share|improve this question

closed as too localized by Tetrad Mar 23 '12 at 20:23

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
See my answer to a similar question here. –  Byte56 Mar 23 '12 at 18:00
3  
I suggest you use a debugger to walk through this code. –  stephelton Mar 23 '12 at 18:00
    
What does the caller of Dot::move() look like? –  K.G. Mar 23 '12 at 18:03
1  
Does SCREEN_HEIGHT have a value, or is it < the initial Y? If it was, yvel would just keep getting a sign change, and y would never increment - thus no movement in Y. –  Tim Holt Mar 23 '12 at 18:27
2  
and now i feel like a tosser for not trying a debugger first... after not even half an hour it turns out i had it set do display the dot at x,x...thanks for the help and sorry for wasting your time –  thegermanpole Mar 23 '12 at 19:02

Browse other questions tagged or ask your own question.