【发布时间】:2012-08-10 13:16:33
【问题描述】:
我做了这个相机类来“调试”我的图形,它工作正常,但我不明白为什么它最终会缩小而不是缩小。
我实现了一个缩放功能,它通过用鼠标右键(RMB)拖动屏幕来工作(用左边你真的拖动屏幕),它就像Unity编辑器缩放(当你按住alt并使用RMB时,它具有缩放功能)
问题是,如果我拿着人民币像个白痴一样摇晃它,放大会比放大更多,最终让一切变得微不足道……我知道我的担心有点愚蠢,但意味着有那里的东西不是很精确..在统一编辑器中,像白痴一样摇晃它不会使事情最终变得超级缩小...
谁能告诉我是什么导致了这种行为?有点棘手..这是我的课,不要介意简单,我只是把它扔给调试:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Shooter
{
/// <summary>
/// Provides a camera controllable by mouse.
/// Controls include dragging the "screen"...
/// </summary>
class MouseCamera2D
{
private Matrix m_mView; // stores the camera state (position, scale/zoom, rotation)
public Matrix ViewMatrix { get { return m_mView; } }
public Vector3 GetPosition() { return m_mView.Translation; }
public float m_scaleProportion = 0.01f; // the "speed" to scale, multiplies the offset from mouse drag
// input data
private MouseState m_mouseStateRef;
private MouseState m_mouseStatePreviows = new MouseState();
private KeyboardState m_kbStateRef;
private KeyboardState m_kbStatePreviows = new KeyboardState();
public Keys m_resetZoomKey = Keys.Z;
//private GraphicsDevice m_graphicsDeviceRef;
// ctor
public MouseCamera2D(MouseState mouseState_p, KeyboardState kbState_p/*, GraphicsDevice graphicsDevice_p*/){
if (mouseState_p == null ||kbState_p == null /*|| graphicsDevice_p == null*/) throw new Exception("cant be null"); // needs the reference
//m_graphicsDeviceRef = graphicsDevice_p;
m_mouseStateRef = mouseState_p; // init the reference
m_kbStateRef = kbState_p;
m_mView = Matrix.Identity; // set a valid matrix
//zoomPivot = new Vector2(m_graphicsDeviceRef.Viewport.Width * 0.5f, m_graphicsDeviceRef.Viewport.Height * 0.5f);
}
public void InputUpdate()
{
m_mouseStatePreviows = m_mouseStateRef;
m_mouseStateRef = Mouse.GetState();
m_kbStatePreviows = m_kbStateRef;
m_kbStateRef = Keyboard.GetState();
InputDragControl();
InputScaleControl();
InputZoomOriginal();
}
private void InputDragControl()
{
//mouseStatePreviows = mouseStateRef;
// mouseStateRef = Mouse.GetState();
if (m_mouseStateRef.LeftButton == ButtonState.Pressed)
{
// check if its a drag or a new pivot point
if (m_mouseStatePreviows.LeftButton == ButtonState.Pressed)
{
m_mView.M41 += (m_mouseStateRef.X - m_mouseStatePreviows.X);
m_mView.M42 += (m_mouseStateRef.Y - m_mouseStatePreviows.Y);
}
}
}
Vector3 zoomPivot;
private void InputScaleControl()
{
if (m_mouseStateRef.RightButton == ButtonState.Pressed)
{
// check if its a drag or a new pivot point
if (m_mouseStatePreviows.RightButton == ButtonState.Pressed)
{
float scale = ((m_mouseStateRef.X - m_mouseStatePreviows.X) + (m_mouseStateRef.Y - m_mouseStatePreviows.Y)); //compute distance with "1d direction"(not abs)
if (scale != 0.0f)
{
scale *= m_scaleProportion;
//center zoom on mouse cursor:
m_mView *=
Matrix.CreateTranslation(-zoomPivot)
*
Matrix.CreateScale(1.0f + scale)
*
Matrix.CreateTranslation(zoomPivot)
;
Console.WriteLine(scale.ToString());
Console.WriteLine(m_mView.M11.ToString());
Console.WriteLine("");
}
}
else
{
// new press, get pivot point:
zoomPivot.X = m_mouseStateRef.X;
zoomPivot.Y = m_mouseStateRef.Y;
}
}
}
private void InputZoomOriginal()
{
if( m_kbStateRef.IsKeyDown(m_resetZoomKey) )
{
m_mView *=
Matrix.CreateTranslation(-zoomPivot)
*
Matrix.CreateScale(1.0f/m_mView.M11)
*
Matrix.CreateTranslation(zoomPivot)
;
Console.WriteLine(m_mView.M11.ToString());
Console.WriteLine("");
}
}
}
}
【问题讨论】: