如何获取屏幕原点(左上角)



我正在构建一个游戏,我想找到窗口的左上角。

不是 (0,0),因为角色正在移动并且摄像机正在跟随 HIM。那么,我必须找到另一种方法,根据角色计算点。

也许XNA中已经内置了一些东西?

我添加了一些照片:

1)https://i.stack.imgur.com/ytpV9.png

2)https://i.stack.imgur.com/rx71u.png

我需要另一种方法来做到这一点。请告诉我您是否有想法或可能。

编辑:相机代码:

public class Camera
{
    public Matrix Mat { get; private set; }
    V2 pos;
    IFocus focus { get; set; }
    F zoom;
    Viewport vp;
    //ctor
    public Camera(IFocus focus , Viewport vp , F zoom)
    {
        this.focus = focus;
        this.zoom = zoom;
        this.vp = vp;
        this.pos = V2.Zero;
    }
    public void update()
    {
        Mat = Matrix.CreateTranslation(-pos.X, -pos.Y, 0) *
              Matrix.CreateRotationZ(0) * //-focus.Rot 
              Matrix.CreateScale(zoom , zoom , 1) * 
              Matrix.CreateTranslation(vp.Width / 2 , vp.Height / 2 , 0);
        this.pos = V2.Lerp(this.pos, focus.Pos, 0.97f); 
    }

绘图代码:

class Drawing : IFocus
{
    #region Data
    protected T2 tex;
    protected V2 org;
    protected Rec? rec;
    public V2 Pos { get; set;}
    C color;
    public F Rot { get; set;}      
    protected V2 scale;
    protected SE se;
    F layer; 
    #endregion
    //Rec? - can be NULL
    #region ctor
    public Drawing(T2 tex, V2 pos, Rec? rec, C color, F rot, V2 org, V2 scale, SE se, F layer)
    {
        this.tex = tex;
        this.Pos = pos;
        this.rec = rec;
        this.color = color;
        this.Rot = rot;
        this.org = org;
        this.scale = scale;
        this.se = se;
        this.layer = layer;
        Game1.CallDraw+=new SigDraw(Draw);
    } 
    #endregion
    #region Drawing
    public virtual void Draw()
    {
        Game1.spriteBatch.Draw(tex, Pos, rec, color, Rot, org, scale, se, layer);
    }

如果它的控件然后你传递控件获取点对象

private static Point FindLocation(Control ctrl)
{
Point p;
for (p = ctrl.Location; ctrl.Parent != null; ctrl = ctrl.Parent)
    p.Offset(ctrl.Parent.Location);
return p;
}

当涉及到 0,0 个原点时,你有 3 种类型,你有模型、世界观和视口,这取决于你试图到达它的代码部分。

如果您在模型上

查找 0,0,它将指向模型的左上角。如果您在世界观上寻找 0,0,它将位于您所在区域(地图)的左下角,如果您正在寻找视口 0,0,它将位于左上角。

您当前正在使用模型来查找您在世界观中的位置。相反,请尝试在世界上存储和使用视口位置,并相对于该位置设置模型/角色位置。

如果不知道任何代码,任何人都很难直接提供帮助。

最新更新