Question

This is my Camera's class, I don't use Matrices:

public static class Camera
{
    private static Vector2 position_ = Vector2.Zero;
    private static Vector2 viewPortSize_ = Vector2.Zero;
    private static Rectangle worldRectangle_ = new Rectangle(0, 0, 0, 0);
    //Intended to be a value in [0.0f,2.0f]
    //With 0.0f as 0%
    //With 1.0f as 100%
    //With 2.0f as 200%

    private static float zoom_ = 1.0f; 

    public static Vector2 Position
    {
        get { return position_; }
        set
        {
                position_ = new Vector2(
                MathHelper.Clamp(value.X, worldRectangle_.X, worldRectangle_.Width - ViewPortWidth),
                MathHelper.Clamp(value.Y, worldRectangle_.Y, worldRectangle_.Height - ViewPortHeight)
                );
        }
    }

    public static Rectangle WorldRectangle
    {
        get { return worldRectangle_; }
        set { worldRectangle_ = value; }
    }

    public static int ViewPortWidth
    {
        get { return (int)viewPortSize_.X; }
        set { viewPortSize_.X = value; }
    }

    public static int ViewPortHeight
    {
        get { return (int)viewPortSize_.Y; }
        set { viewPortSize_.Y = value; }
    }

    public static Rectangle ViewPort
    {
        get
        {
            return new Rectangle(
                (int)Position.X, (int)Position.Y,
                ViewPortWidth, ViewPortHeight
                );
        }
    }

    public static float Zoom
    {
        get { return zoom_; }
        set { zoom_ = value; }
    }

    public static void Move(Vector2 offset)
    {
        Position += offset;
    }

    public static bool ObjectIsVisible(Rectangle bounds)
    {
        return (ViewPort.Intersects(bounds));
    }

    public static Vector2 Transform(Vector2 point)
    {
        return point - position_;
    }

    public static Rectangle Transform(Rectangle rectangle)
    {
        return new Rectangle(
            rectangle.Left - (int)position_.X,
            rectangle.Top - (int)position_.Y,
            rectangle.Width,
            rectangle.Height);
    }

    public static Vector2 ScreenToWorld(Vector2 screenLocation)
    {
        return screenLocation + position_;
    }

    public static Point PointScreenToWorld(Vector2 screenLocation)
    {
        Vector2 tmp = screenLocation + position_;
        return new Point(Convert.ToInt32(tmp.X), Convert.ToInt32(tmp.Y));
    }

    public static Rectangle ScreenToWorld(Rectangle screenRectangle)
    {
        return new Rectangle(
        screenRectangle.Left + (int)position_.X,
        screenRectangle.Top + (int)position_.Y,
        screenRectangle.Width,
        screenRectangle.Height);
    }
}

Question 1

Consider that I draw a texture like this:

public override void Draw(spriteBatch spritebatch)
{
  spriteBatch.Draw(
    myTexture, 
    Camera.Transform(texture.position),
    Color.White);
}

What is the right way to make this Draw call to also zoom the texture using Camera's zoom_ value? I really have no idea.

Question 2

Consider that I draw a set of textures like this:

 public static void Draw(SpriteBatch spriteBatch)
 {
    //They draw each inside a personal RenderTarget2D
    DrawBackgroundLayers(spriteBatch);
    DrawInteractiveLayers(spriteBatch);
    DrawForegroundLayers(spriteBatch);

    graphicsDevice_.SetRenderTarget(null);
    graphicsDevice_.Clear(Color.CornflowerBlue);

    spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
    spriteBatch.Draw(background_layers_render_, Vector2.Zero, Color.White);
    spriteBatch.Draw(interactive_layers_render_, Vector2.Zero, Color.White);
    spriteBatch.Draw(foreground_layers_render_, Vector2.Zero, Color.White);
    spriteBatch.End();
}

In this way, the answer of the first question is the same?

Était-ce utile?

La solution

misiMe,

You have references to X and Y, but no reference to Z, the third dimension.

See the following MSDN article:

http://msdn.microsoft.com/en-us/library/ms747437(v=vs.110).aspx

I used to pick a value for Z, and divide (things farther way are smaller and appear to be moving more slowly). I remember adding 0.0001 to prevent divide by zero.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top