Axe Jitter

HI , i changed the code in the " C++ Fundamentals: Game Programming For Beginners " , where we create an axe and a circle that moves around
here is the code
axe_y += direction;

        if (b_axe_y > height || axe_y < 0)

        {

            direction = -direction;

        }

i’m checking if the lower bound of the rectangle is hitting the edge of the window, but all it does is make the image jitter and not go back in the same direction, i tried changing the syntax of the condition to check and everything works fine but when i write the code in this way it makes the rectangle jitter, anyone have a solution for this???

What is b_axe_y? Maybe you wanted to check axe_y against two different values instead.

I am checking if the bottom edge of a rectangle is colliding with the bottom edge of the screen:
b_axe_y = axe_y + axe_length (b_axe_y is the bottom edge on the y axis)
axe_y : is the position of the rectangle on the y axis
axe_length : is the length of the rectangle

// move the axe

        axe_y += direction;

        if (b_axe_y > height || axe_y < 0)

        {

            direction = -direction;

        }

if the bottom edge of the rectangle is colliding with the bottom edge of the window it should reverse the speed , and it does but it keeps reversing it every frame.

if i change the check to
if ( axe_y + axe_length > height || axe_y < 0)

        {

            direction = -direction;

        }

OR:

axe_y += direction;

        if ( axe_y > height - axe_length || axe_y < 0)

        {

            direction = -direction;

        }

it works just fine … but when i encapsulate the check inside a variable it doesnt …

Are you updating b_axe_y correctly after updating axe_y and before checking it?

i guess the order of execution is not the same , i should change the axe_y then update the b_axe_y and then check it ?

You should definitely do that!

If you instead update b_axe_y before you update axe_y (which the beginning of your longer post seems to indicate), then b_axe_y is not up-to-date when you check it (instead it corresponds to the axe_y value from the previous loop iteration), which would explain the behaviour you initially reported.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms