How would one implement the effect of a sprite disappearing on one side of the screen and appearing on the other using AndEngine? Like in Kid Icarus, seen here at around 1:22.


2 Answers 2


As we discussed in the comments, you don't want to draw two sprites. So we go to the shader level.

I've updated this for using with the fragment shader. You just need to pass in the screen min and max.

uniform int screenMax;
uniform int screenMin;


if(gl_FragCoord.x > screenMax) {
    gl_FragCoord.x = screenMin + (gl_FragCoord.x - screenMax);
if(gl_FragCoord.x < screenMin) {
    gl_FragCoord.x = screenMax + (screenMin - gl_FragCoord.x);

If you haven't done anything with shaders yet, you can read up about them here.


Thinking about this more, I have no idea if it will even work. I don't know if things get culled off screen before or after this. You should test it out!

  • \$\begingroup\$ That approach works but the sprite just disappears on one side and appears on the other, it doensn't show part of the sprite on one side of the screen and the rest on the other, which is what I'm looking for... \$\endgroup\$
    – patresi
    Mar 22, 2012 at 20:37
  • \$\begingroup\$ Well, you can either do as I edited allowing the sprite to walk just off screen before it walks on screen on the other side. Or you can draw two sprites using a similar check, if you want them to show half and half. \$\endgroup\$
    – House
    Mar 22, 2012 at 20:44
  • \$\begingroup\$ I thought of that solution of drawing two sprites but was trying to avoid it... In other 2d non-opengl engines where the "screen" is represented by a two-dimensional you can do this without recurring to sprite duplication... \$\endgroup\$
    – patresi
    Mar 22, 2012 at 20:47
  • \$\begingroup\$ Likely a shader then. I believe AndEngine supports those, and it would essentially be the same as above, except you'd be shifting things around in your fragment shader. \$\endgroup\$
    – House
    Mar 22, 2012 at 20:54
  • \$\begingroup\$ Yes I was talking about lower level stuff where you have access to the pixels \$\endgroup\$
    – patresi
    Mar 22, 2012 at 20:56

Unfortunately I've never used AndEngine before so I can't give you a specific answer to your problem, but I've asked a similar question before for XNA and got a great answer out of it. Here's a link to it:

How to create a 2D region where sprites are automatically wrapped?

That approach which uses a few render targets is great when you want wrapping to happen to everything in your game world automatically. That's because wrapping is done for the entire screen at once, instead of having to handle them individually for each object. Also notice that the example in that link is a lot more complicated, with complete 4-way wrapping in every direction.

But if on the other hand only your main character will be doing the wrapping, and especially since you only need horizontal wrapping, then it's probably a lot easier to just draw your character two times when needed on each side of the screen. For instance something like the following pseudocode:

// On update wrap character position
if(character.X < camera.Left)
    character.X += camera.Width;
else if(character.X > camera.Right)
    character.X -= camera.Width;

// On draw decide how many times to draw and where
DrawCharacterAt(character.X, character.Y);
if(character.Left < camera.Left)
    DrawCharacterAt(character.X + camera.Width, character.Y);
else if(character.Right > camera.Right)
    DrawCharacterAt(character.X - camera.Width, character.Y);

Where X is the center line of your character, character Left / Right are the extremities of the character sprite, and camera Left / Right are the borders of the camera in world space i.e. the same space as your character so that they can be compared.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .