This project has moved and is read-only. For the latest updates, please go here.

Pac-Man-like Screen Wrap + Collision Errors

Topics: User Forum
Mar 5, 2009 at 4:41 PM
I've written some code to handle Pac-Man-like screen wrapping (i.e., your character exits on the left side of the screen and reappears on the right), but I occasionally experience collision errors when two objects manage to screen wrap on top of each other. Ideally two objects trying to wrap to the same place would bounce off of each other and there wouldn't be any issues. But, as it stands, with the code I have written, the two objects appear on top of each other and get stuck.

Here's the code I have currently:

            Vector2 newPosition = body.Position;

            if (body.Position.X > WindowWidth + (texture.Width / 2))
                newPosition.X = 0 - (texture.Width / 2);
            else if (body.Position.X < 0 - (texture.Width / 2))
                newPosition.X = WindowWidth + (texture.Width / 2);

            if (body.Position.Y > WindowHeight + (texture.Height / 2))
                newPosition.Y = 0 - (texture.Height / 2);
            else if (body.Position.Y < 0 - texture.Height)
                newPosition.Y = WindowHeight + (texture.Height / 2);

            body.Position = newPosition;

Has anyone solved this before? What could I change to make sure objects don't wrap on top of each other? Thanks!! :)
Mar 6, 2009 at 5:42 PM
just some ideas

you could precheck your new location with:
precheck = new Geom( ... );
foreach( var other in geoms )
    if( other.Collide( precheck ) {...}

you could run multiple instances of the same scene on all sides of your original scene (original scene in the middle, copies on left, right, top, bottom of original scene)
Mar 6, 2009 at 5:52 PM
Thanks yobiv!! I discovered that one of my problems was that I was using an older version of Farseer. Once I started using the newest version, I discovered that the engine automatically pushes overlapping objects out of each other, which is great. The default behaviour still isn't ideal, however, so I'm definitely going to try out your first suggestion. That should be pretty simple since I only have a handful of Geom objects in each of my scenes.

Thanks again!