The blocks lose traveled distance when they enterYour value of movespeed=10 is a special case where each block collides with the wall based on how far they enteredat the wall, because you resetsame distance into the positionwall.
In order to keep your spacing the bounds ofsame, the rectangleboxes must each travel a distance of movespeed every frame.
Think about the problem inof a 1D. If you have problem with a single point on a line that goes from 0 to 100 and 2 points:
P1 = 90
P2 = 100
If you take movespeed = 7 and apply one frame of. Using your algorithm, then
lets start at P1 = 90+1*7 = 97
P2 = 100+1*7 = 107
Then apply 1 more frame for and go through the direction switchsteps.
P1 = 97+1*7 = 104
P297 + 10 = min(100, 107)-1*7 = 93
Now you can see the points have a separation of 11 instead of 10
Next frame
P1 = min(100, 104)100 - 1*7 = 97
P2 = 93-1*710 = 8690
... So Since the problem is thatpoint was brought back to 100, then you subtract 10, the distancepoint traveled 17 in reduced when a cube leaves and enters the bounding rectangle. Remove the linesone frame, which makes it desync from other boxes that clipalign more accurately with the positionwall.
block->sprite->position.x = viewportWidth - block->sprite->width;
and
block->sprite->position.x = 0;
EDIT:
This won't completelyIn order to fix yourthe problem though. Let's say, you have 2 points again:
P1 = 92
P2 = 93
After 1 frame :
P1 = 92 + 7 = 99
P2 = 93 + 7 = 100
Next frame
P1 = 106
P2 = 93
So they still end up with different spacing. The solution isneed to buffer the position after it hits the wall, somake sure the spacing is stillpoint always travels the same. distance i.e.
If P1 = 9597 and you add the 710, then P1 = 100 + (100-(95+797+10)) = 9893
Meaning that P1 traveled a distance of 7. 53 towards the wall and 2then 7 away from the wall for a total of 10.
AwayA way to implements this would be to change the previous lines to the following...
block->sprite->position.x = 2*(viewportWidth - block->sprite->width) - (block->sprite->position.x+movespeed);
and
block->sprite->position.x = -(block->sprite->position.x-movespeed);
and include the normal movement code in an else bracket
else
{
block->sprite->position.x += speed * block->dir;
}