Class: TonMath
ゲーム開発に役立つ数学・乱数などのユーティリティ関数群です。
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: 判定対象の矩形
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);