Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a bit of a strange question, I am adapting the existing code base including the tile engine as per the book: XNA 4.0 Game Development by example by Kurt Jaegers, particularly the aspect that I am working on is the part about the 2D platformer in the last couple of chapters.

I am creating a platformer which has a scrolling screen (similar to an old school screen chase), I originally did not have any problems with this aspect as it is simply a case of updating the camera position on the X axis with game time, however I have since added a particle system to allow the players to fire weapons. This particle shot is updated via the world position, I have translated everything correctly in terms of the world position when the collisions are checked.

The crux of the problem is that the collisions only work once the screen is static, whilst the camera is moving to follow the player, the collisions are offset and are hitting blocks that are no longer there.

My collision for particles is as follows (There are two vertical and horizontal):

    protected override Vector2 horizontalCollisionTest(Vector2 moveAmount)
        if (moveAmount.X == 0)
            return moveAmount;

        Rectangle afterMoveRect = CollisionRectangle;
        afterMoveRect.Offset((int)moveAmount.X, 0);
        Vector2 corner1, corner2;

        // new particle world alignment code. 
        afterMoveRect = Camera.ScreenToWorld(afterMoveRect);
        // end.

        if (moveAmount.X < 0)
            corner1 = new Vector2(afterMoveRect.Left,
                                  afterMoveRect.Top + 1);
            corner2 = new Vector2(afterMoveRect.Left,
                                  afterMoveRect.Bottom - 1);
            corner1 = new Vector2(afterMoveRect.Right,
                                  afterMoveRect.Top + 1);
            corner2 = new Vector2(afterMoveRect.Right,
                                  afterMoveRect.Bottom - 1);

        Vector2 mapCell1 = TileMap.GetCellByPixel(corner1);
        Vector2 mapCell2 = TileMap.GetCellByPixel(corner2);

        if (!TileMap.CellIsPassable(mapCell1) ||
            moveAmount.X = 0;
            velocity.X = 0;

        return moveAmount;

And the camera is pretty much the same as the one in the book... with this added (as an early test).

    public static void Update(GameTime gameTime)
        position.X += 1;
share|improve this question
Can you post the code for Camera.ScreenToWorld and TileMap.GetCellByPixel – Joe Apr 24 '12 at 16:12

You mentioned in your original post that particle positions are already in world position, and I'll assume that CollisionRectangle is also in world position. If that's true, then why afterMoveRect = Camera.ScreenToWorld(afterMoveRect);? Collision detection doesn't need the screen coordinates of anything, so if it does, you would be better served to refactor the code so that it doesn't. Your collision detection code will be much easier to debug, maintain, and extend in the future.

Try removing that call to translate the coordinates and see if that fixes your problem. The particle's position only needs to be translated to screen coords by the rendering logic. I haven't read the book you mentioned, but I'll also assume that camera translations are being passed as a matrix as part of a call to SpriteBatch.Begin(...).

Another possible cause of your issue is that the camera position could be getting updated before the collision detection code runs, but after rendering has completed. Try looking at the order of updates and see if there's an issue there.

To avoid issues of this type in the future, you could implement the side-scrolling functionality by adding (if not already present) a Velocity and/or Acceleration property to your Camera class (it goes without saying that it would have a Position property). Create a method or set of methods as needed that will allow you to impart an initial velocity to your camera - in your case, it looks like 1 is a good place to start. In Update, you'll just do standard kinematics calculations to calculate the position of the camera each frame.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.