bearcreeknerd
bearcreeknerd

Reputation: 115

Windows Phone XNA: Real Time Strategy Camera

We are developing a real time strategy varient for WP7. At the momment, we require some direction/instruction on how to build an effective camera system. In other words, we would like a camera that can pan around a flat surface (2d or 3d level map). We have been experimenting with a 2d tile map, while our unit/characters are all 3d models.

At first glance, it appears we need to figure out how to calculate a bounding box around the camera and its entire view perspective. Or, restrict the movement of the camera to what it can see, to the bounds of a 2d map.

Any help would be greatly appreciated!!

Cheers

Upvotes: 0

Views: 330

Answers (1)

user155407
user155407

Reputation:

If you're doing true 2D scrolling, it's pretty simple:

  • Scroll.X must be between 0 and level.width - screen.width
  • Scroll.Y must be between 0 and level.height - screen.height

(use MathHelper.Clamp to help you with this)

As for 3D it's a little trickier but almost the same principle.

All you really need is to define TWO Vector3 points, one is the lower left back corner and the other the upper right front (or you could do upper left front / lower right back, etc., up to you). These would be your bounding values.

The first one you could define as a readonly with just constant values that you tweak the camera bounds exactly as you wish for that corner. There IS a way of computing this, but honestly I prefer to have more control so I typically choose the route of value tweaking instead.

The second one you could start off with a "base" that you could manually tweak (or compute) just like before but this time you have to add the map width and length (to X and Z) so you know the true bounds depending on the map you have loaded.

Once you have these values, clamp them just as before:

    //pans the camera but caps at bounds
    public void ScrollByCheckBounds(Vector3 scroll, Vector3 bottomLeftFront, Vector3 topRightBack)
    {
        Vector3 newScroll = Scroll + scroll;

        //clamp each dimension
        newScroll.X = MathHelper.Clamp(newScroll.X, topRightBack.X, bottomLeftFront.X);
        newScroll.Y = MathHelper.Clamp(newScroll.Y, topRightBack.Y, bottomLeftFront.Y);
        newScroll.Z = MathHelper.Clamp(newScroll.Z, bottomLeftFront.Z, topRightBack.Z);
        Scroll = newScroll;
    }

Upvotes: 1

Related Questions