Skip to main content
3 of 4
added 1 character in body
Kyy13
  • 740
  • 3
  • 10

The blocks lose traveled distance when they enter the wall based on how far they entered the wall, because you reset the position to the bounds of the rectangle. Think about the problem in 1D. If you have a line from 0 to 100 and 2 points:

P1 = 90

P2 = 100

If you take movespeed = 7 and apply one frame of your algorithm, then

P1 = 90+1*7 = 97

P2 = 100+1*7 = 107

Then apply 1 more frame for the direction switch

P1 = 97+1*7 = 104

P2 = 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) - 1*7 = 97

P2 = 93-1*7 = 86

... So the problem is that the distance traveled in reduced when a cube leaves and enters the bounding rectangle. Remove the lines that clip the position.

block->sprite->position.x = viewportWidth - block->sprite->width;

and

block->sprite->position.x = 0;

EDIT:

This won't completely fix your 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 is to buffer the position after it hits the wall, so the spacing is still the same. i.e.

If P1 = 95 and you add the 7, then P1 = 100 + (100-(95+7)) = 98

Meaning that P1 traveled a distance of 7. 5 towards the wall and 2 away from the wall.

Away 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;
}
Kyy13
  • 740
  • 3
  • 10