Skip to main content
deleted 516 characters in body
Source Link
Kyy13
  • 740
  • 3
  • 10

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;
}

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;
}

Your value of movespeed=10 is a special case where each block collides with the wall at the same distance into the wall.

In order to keep your spacing the same, the boxes must each travel a distance of movespeed every frame. 

Think of a 1D problem with a single point on a line that goes from 0 to 100. Using your algorithm lets start at P1 = 97 and go through the steps.

P1 = 97 + 10 = 107

Next frame

P1 = 100 - 10 = 90

Since the point was brought back to 100, then you subtract 10, the point traveled 17 in one frame, which makes it desync from other boxes that align more accurately with the wall.

In order to fix the problem, you need to make sure the point always travels the same distance i.e.

If P1 = 97 and you add 10, then P1 = 100 + (100-(97+10)) = 93

Meaning that P1 traveled a distance of 3 towards the wall and then 7 away from the wall for a total of 10.

A 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;
}
added 1 character in body
Source Link
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;
}

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;

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;
}
added 1 character in body
Source Link
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 = 95100

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;

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 = 95

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;

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;
Source Link
Kyy13
  • 740
  • 3
  • 10
Loading