在2D相机中实现放大/缩小

时间:2014-01-02 18:41:41

标签: c# xna camera game-engine xna-4.0

这是我的相机课程,我不使用矩阵

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);
    }
}

问题1

考虑我画一个这样的纹理:

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

使用Camera的Draw值进行此zoom_调用以缩放纹理的正确方法是什么?我真的不知道。

问题2

考虑我画一组这样的纹理:

 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();
}

这样,第一个问题的答案是一样的吗?

1 个答案:

答案 0 :(得分:1)

misiMe,

您有对X和Y的引用,但没有引用第三维Z.

请参阅以下MSDN文章:

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

我曾经为Z选择一个值,并且(更远的东西更小,看起来移动速度更慢)。我记得加上0.0001来防止除以零。