Hello all,

I'm writing an 'Asteroids' clone for an assignment at school. I'm trying to figuire out an algorithm to 'wrap' the asteroids/bullets/ship when it collides with the edge of the screen. I've written an algorithm and it works alright, but has some flaws. It will always wrap to the edge opposite to the one collided with, which makes things look stupid when you collide at a sharp angle and appear on the opposite side of the screen rather than the window edge perpendicular to the one collided with. I did some google searching and couldnt find any good graphics wrapping algorithms :( . Any help is appreciated.

Here's the original algorithm, and my 'under construction one' commented out (in c#):

Note - all angles are in radians (all the decimals everywhere)

```
//these are for mirroring
//if the ship is not traveling in a vertical straight line...
if (!((_fHead >= -0.2 && _fHead <= 0.2) || (_fHead >= 3.1 && _fHead <= 3.2)))
{
//if the ship is past the Y boundaries
if (_pnt.Y < -iShapeSize)
{
_pnt.Y = client.Height + iShapeSize;
//set to the location opposite, relative to the angle of movement
_pnt.X = (float)Math.Abs(client.Width * Math.Sin(_fHead));
}
if (_pnt.Y > client.Height + iShapeSize)
{
_pnt.Y = -iShapeSize;
_pnt.X = (float)Math.Abs(client.Width * Math.Sin(_fHead));
}
}
else
{
//keep the current x coord, since the ship is travelling straight
if (_pnt.Y < -iShapeSize)
_pnt.Y = client.Height;
if (_pnt.Y > client.Height + iShapeSize)
_pnt.Y = -iShapeSize;
}
//if the ship is not travelling in a horizontal line...
if (!((_fHead >= 1.4 && _fHead <= 1.6)
|| (_fHead >= -1.6 && _fHead <= -1.4)
|| (_fHead >= 4.6 && _fHead <= 4.8)
|| (_fHead >= -4.8 && _fHead <= -4.6)))
{
if (_pnt.X < -iShapeSize)
{
_pnt.Y = (float)Math.Abs(client.Height * Math.Cos(_fHead));
_pnt.X = client.Width + iShapeSize;
}
if (_pnt.X > client.Width + iShapeSize)
{
_pnt.Y = (float)Math.Abs(client.Height * Math.Cos(_fHead));
_pnt.X = -iShapeSize;
}
}
else
{
if (_pnt.X < -iShapeSize)
_pnt.X = client.Width + iShapeSize;
if (_pnt.X > client.Width + iShapeSize)
_pnt.X = -iShapeSize;
}
//Better mirror solution...under construction
//the other one doesnt compensate for the fact that the dimensions are uneven (800, 600)
//technically a steep angle entering the left or right border should mirror to the bottom or top
//ie not the opposite side in every case
/*
if (_pnt.Y < -iShapeSize)
{
_pnt.Y = client.Height;
//set to the location opposite, relative to the angle of movement
_pnt.X += ((float)Math.Sin(_fHead + Math.PI) * client.Height);
}
else if (_pnt.Y > client.Height + iShapeSize)
{
_pnt.Y = 0;
_pnt.X += ((float)Math.Sin(_fHead + Math.PI) * client.Height);
}
else if (_pnt.X < -iShapeSize)
{
_pnt.X += (float)Math.Sin(_fHead + Math.PI) * (client.Height - _pnt.Y);
//set to the location opposite, relative to the angle of movement
_pnt.Y += ((float)Math.Cos(_fHead + Math.PI) * client.Width);
}
else if (_pnt.X > client.Width + iShapeSize)
{
_pnt.X += (float)Math.Sin(_fHead + Math.PI) * (client.Height - _pnt.Y);
_pnt.Y += ((float)Math.Cos(_fHead + Math.PI) * client.Width);
}*/
```