何时归一化向量? [英] When to Normalize a Vector?

查看:38
本文介绍了何时归一化向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 XNA 和几乎所有在 http://creators.xna 上找到的教育工具包.com/en-US/.我总是在向量上看到对 Normalize() 的调用.我知道 normalize 基本上将向量转换为单位长度,所以它给出的只是方向.

I am learning XNA and in almost all of the educational kits found on http://creators.xna.com/en-US/. I always see a call to Normalize() on a vector. I understand that normalize basically converts the vector into unit length, so all it gives is direction.

现在我的问题是什么时候进行规范化以及它到底对我有什么帮助.我在做 2D 编程,所以请用 2D 概念而不是 3D 来解释.

Now my question is When to normalize and what exactly does it help me in. I am doing 2D programming so please explain in 2D concepts and not 3D.

这是 XNA 套件中的代码,那么为什么要调用 Normalize?

Here is code in the XNA kit, so why is the Normalize being called?

if (currentKeyboardState.IsKeyDown(Keys.Left) ||
            currentGamePadState.DPad.Left == ButtonState.Pressed)
        {
            catMovement.X -= 1.0f;
        }
        if (currentKeyboardState.IsKeyDown(Keys.Right) ||
            currentGamePadState.DPad.Right == ButtonState.Pressed)
        {
            catMovement.X += 1.0f;
        }
        if (currentKeyboardState.IsKeyDown(Keys.Up) ||
            currentGamePadState.DPad.Up == ButtonState.Pressed)
        {
            catMovement.Y -= 1.0f;
        }
        if (currentKeyboardState.IsKeyDown(Keys.Down) ||
            currentGamePadState.DPad.Down == ButtonState.Pressed)
        {
            catMovement.Y += 1.0f;
        }


        float smoothStop = 1;


        if (catMovement != Vector2.Zero)
        {
            catMovement.Normalize();
        }

        catPosition += catMovement * 10* smoothStop;

}

推荐答案

在您的示例中,键盘按下会使您在 X 或 Y 或两者上移动.在 X 和 Y 的情况下,当您同时按下向右和向下时,您的移动是对角线的.但是,仅在 X 或 Y 中的运动会给您一个长度为 1 的向量,而对角线向量则比 1 长.也就是说,大约是 1.4(2 的平方根).

In your example, the keyboard presses give you movement in X or Y, or both. In the case of both X and Y, as when you press right and down at the same time, your movement is diagonal. But where movement just in X or Y alone gives you a vector of length 1, the diagonal vector is longer than one. That is, about 1.4 (the square root of 2).

如果不对移动向量进行归一化,那么对角线移动将比 X 或 Y 移动更快.使用归一化后,所有 8 个方向的速度都相同,我猜这就是游戏所要求的.

Without normalizing the movement vector, then diagonal movement would be faster than just X or Y movement. With normalizing, the speed is the same in all 8 directions, which I guess is what the game calls for.

这篇关于何时归一化向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆