Odd bug with my circle movement, plus some questions

Hello. Just bought this course on Udemy and I’m at the last chapter of the Axe Game. Now, I’m not sure if there is a bug of some sort since I’m using Raylib 5.0, but there is an issue where my circle will move in one direction for a small distance, but then change distance and then move a short distance before going in the original direction…over and over. At times even ending up off screen. My code does not look much different from the finished code on gitlab, so I’m not sure where this bug comes from.

EDIT: I just saw what the issue was with my circle movement thanks to another post in which the user was having the same issue. (I originally didn’t open that thread thinking it wasn’t related). The answer is both the circle and the rectangle need their own movement speeds. Using the same for both makes the circle change direction when the square does. I can’t believe I didn’t see it when I was testing it out.

Also, shouldn’t we be using || instead of && when looking for collision? wouldn’t && be looking for every parameter to be true before declaring that the bool is now true? Why not initialize the bool as false first, then update it in the while loop?

Wouldn’t it make more sense to use an if - elseif branch for collision? If it collides then assign the bool to be true, or would that slow the program down constantly checking those conditions?

To answer the first two questions in one go, we need to use the && to ensure that we that the two collision boxes are colliding. If we used || instead of && then we would get odd behaviour where we collide even though the player and the axe are nowhere near each other (this is a neat idea to try yourself and see what’s going on).

As for the third question, there’s no specific reason to initialize one way or the other, and I would argue it would make more sense to initialize with false first and update in the while-loop. But the overall effect is the same.

You would need nested if-statements in order to get a similar result (if-elseif wouldn’t work here), and for readability of your code it’s good practice to leave nesting to as few as possible.

1 Like

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

Privacy & Terms