Class: TonMath
ゲーム開発に役立つ数学・乱数などのユーティリティ関数群です。
Enums
EasingType
イージング関数の種類を表す列挙体です。
| 値 | 説明 |
|---|---|
Linear | 等速の直線運動 |
Quad | 2次方程式補間(なめらか) |
Cubic | 3次方程式補間(少し急な加減速) |
Quart | 4次方程式補間(急な加減速) |
Quint | 5次方程式補間(非常に急な加減速) |
Sine | サイン波(きわめて自然な加減速) |
Expo | 指数関数(動き出しが遅く、後半一気に加速) |
Back | 少し後ろに沈み込んでから前に飛び出す動き |
Circ | 円形補間(硬質で機械的な加減速) |
Elastic | 弾性のあるバネ振動のような動き |
Bounce | ボールが跳ね返るようなバウンド運動 |
EasingMode
イージングの方向(モード)を表す列挙体です。
| 値 | 説明 |
|---|---|
In | 開始時にイージング(最初はゆっくり、徐々に加速) |
Out | 終了時にイージング(最初は速く、徐々に減速) |
InOut | 開始と終了の両方でイージング |
Methods
int Rand(int min, int max)
指定範囲の整数の乱数を返します。
min: 最小値max: 最大値(排他的)
float RandF(float min, float max)
指定範囲の浮動小数点の乱数を返します。
min: 最小値max: 最大値
float GetDistance(float x1, float y1, float x2, float y2)
二点間の距離を計算します。
float GetAngle(float x1, float y1, float x2, float y2)
二点間の角度(ラジアン)を計算します。
bool HitCheckRect(Rectangle rect1, Rectangle rect2)
二つの矩形の当たり判定(交差判定)を行います。
rect1: 判定対象の矩形1rect2: 判定対象の矩形2
bool HitCheckCircle(Vector2 pos1, float r1, Vector2 pos2, float r2)
二つの円の当たり判定(交差判定)を行います。
pos1: 円1の中心座標r1: 円1の半径pos2: 円2の中心座標r2: 円2の半径
bool IsPointInRect(float x, float y, Rectangle rect)
点が矩形内に含まれているかを判定します。
x, y: 判定する点の座標rect: 判定対象の矩形
float Ease(float t, EasingType type, EasingMode mode = EasingMode.In)
指定したイージングタイプとモードで時間割合 (0.0〜1.0) を補間した値を返します。
t: 時間割合 (0.0~1.0)type:EasingTypemode:EasingMode
Sample Code
// Random Integer [0, 100)
int r = Ton.Math.Rand(0, 100);
// Random Float [0.0, 1.0]
float f = Ton.Math.RandF(0f, 1f);
// Distance between (x1,y1) and (x2,y2)
float dist = Ton.Math.GetDistance(x1, y1, x2, y2);
// Angle in radians
float angle = Ton.Math.GetAngle(playerX, playerY, mouseX, mouseY);
// Rectangle vs Rectangle
Rectangle playerRect = new Rectangle((int)player.X, (int)player.Y, 32, 32);
Rectangle bulletRect = new Rectangle((int)bullet.X, (int)bullet.Y, 8, 8);
bool isHitRect = Ton.Math.HitCheckRect(playerRect, bulletRect);
// Circle vs Circle
bool isHitCircle = Ton.Math.HitCheckCircle(
new Vector2(player.X, player.Y), 16f,
new Vector2(enemy.X, enemy.Y), 24f
);
// Point in Rectangle
bool isMouseOnButton = Ton.Math.IsPointInRect(mouseX, mouseY, buttonRect);
// Easing (Cubic EaseIn)
float easedT = Ton.Math.Ease(t, EasingType.Cubic, EasingMode.In);