Skip to main content
more info
Source Link
ChrisF
  • 696
  • 1
  • 9
  • 20

What you need to do is reset the sprite's position back to it's greatest extent.

You can do this by taking the vector from the centre to the sprites position, normalising it and then multiplying that by the allowed distance. However, that involves a square root which may be too slow for your game.

Another approach is to limit the sprite to a box around the centre position so your test would become:

if (spritePosition.x > centrePosition.x + allowedDistance)
{
    spritePosition.x = centrePosition.x + allowedDistance;
}
else if (spritePosition.x < centrePosition.x - allowedDistance)
{
    spritePosition.x = centrePosition.x - allowedDistance;
}

and the same for the y.

This doesn't give the same effectThis obviously doesn't give the same effect but may be quicker. You'd have to profile them both to see and also check the behaviour to see if was acceptable.

What you need to do is reset the sprite's position back to it's greatest extent.

You can do this by taking the vector from the centre to the sprites position, normalising it and then multiplying that by the allowed distance. However, that involves a square root which may be too slow for your game.

Another approach is to limit the sprite to a box around the centre position so your test would become:

if (spritePosition.x > centrePosition.x + allowedDistance)
{
    spritePosition.x = centrePosition.x + allowedDistance;
}
else if (spritePosition.x < centrePosition.x - allowedDistance)
{
    spritePosition.x = centrePosition.x - allowedDistance;
}

and the same for the y.

This doesn't give the same effect but may be quicker. You'd have to profile them both to see.

What you need to do is reset the sprite's position back to it's greatest extent.

You can do this by taking the vector from the centre to the sprites position, normalising it and then multiplying that by the allowed distance. However, that involves a square root which may be too slow for your game.

Another approach is to limit the sprite to a box around the centre position so your test would become:

if (spritePosition.x > centrePosition.x + allowedDistance)
{
    spritePosition.x = centrePosition.x + allowedDistance;
}
else if (spritePosition.x < centrePosition.x - allowedDistance)
{
    spritePosition.x = centrePosition.x - allowedDistance;
}

and the same for the y.

This obviously doesn't give the same effect but may be quicker. You'd have to profile them both to see and also check the behaviour to see if was acceptable.

Source Link
ChrisF
  • 696
  • 1
  • 9
  • 20

What you need to do is reset the sprite's position back to it's greatest extent.

You can do this by taking the vector from the centre to the sprites position, normalising it and then multiplying that by the allowed distance. However, that involves a square root which may be too slow for your game.

Another approach is to limit the sprite to a box around the centre position so your test would become:

if (spritePosition.x > centrePosition.x + allowedDistance)
{
    spritePosition.x = centrePosition.x + allowedDistance;
}
else if (spritePosition.x < centrePosition.x - allowedDistance)
{
    spritePosition.x = centrePosition.x - allowedDistance;
}

and the same for the y.

This doesn't give the same effect but may be quicker. You'd have to profile them both to see.