Skip to content

Instantly share code, notes, and snippets.

@synctam
Created October 4, 2022 08:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save synctam/d938cb30f5248baceba51986829bd130 to your computer and use it in GitHub Desktop.
Save synctam/d938cb30f5248baceba51986829bd130 to your computer and use it in GitHub Desktop.
■ Vector2D 版の Normalized 関数
AGKには、ベクトルの正規化関数がないみたいなので、Vector2D版のNormalized関数作りました。
これを利用すると、縦・横・斜めの移動速度を一定にすることができます。
//
// Vector2d型
//
Type Vector2d
x as float
y as float
EndType
//
// Vector2dNormalized
// 引数で受け取った Vector2d 型のデータを正規化した値に変換する。
// (斜め移動で速度が速くならないようにするときに便利)
//
// 引数
// vec: 変換したいVector2d型
//
// 戻り値:なし
//
// 参考資料:
// 「Normalize a vector - GameCreators Forum」
// https://forum.thegamecreators.com/thread/226079
//
Function Vector2dNormalized( vec ref as Vector2D )
local length as float
length = Sqrt( vec.x * vec.x + vec.y * vec.y )
vec.x = vec.x / Length
vec.y = vec.y / Length
EndFunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment