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);
            }*/

Have you drawn this on paper?

Used symbols to represent the various angles and dimensions?

Worked out the actual maths, rather than merely hacking away at some half-attempt?

Have you drawn this on paper?

Used symbols to represent the various angles and dimensions?

Worked out the actual maths, rather than merely hacking away at some half-attempt?

Yeah I did. Thanks for the help... I figuire that if i draw an imaginary line at your ships angle + pi (180 degree turn) look for an intersection with one of the boundaries, that will be the right spot. But my math is rusty, as I havent done trig or calculus in over a year. I cant seem to mathematically determine where it will intersect. It's probably pretty simple, I just need a point in the right direction.

I would suggest you get back up to speed, because this won't be your only trig / geometry problem in this program.

http://www.faqs.org/faqs/graphics/algorithms-faq/
Featuring how to detect various things intersecting with various other things.

Thanks for the link. But this is the last trig problem in this program. The rest is all finished!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.