Files
jdt-fish-client/assets/FishSingle/script/engine/utils/MathUtils.ts
2024-10-05 04:22:34 +08:00

62 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Vec2 } from 'cc'
export default class MathUtils {
/**
* 2个点之前的直线距离
* @param x1
* @param y1
* @param x2
* @param y2
*/
public static distance(x1: number, y1: number, x2: number, y2: number) {
// 设两点AX1,Y1,BX2,Y2
// 距离D=X2-X1的平方+Y2-Y1平方的和开平方
return Math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
}
/**
* 2点间的向量
* @param p1
* @param p2
*/
public static sub(p1: Vec2, p2: Vec2) {
return new Vec2(p1.x - p2.x, p1.y - p2.y)
}
/**
* 弧度转角度
* @param radians
*/
public static radiansToDegrees(radians: number) {
return (180 / Math.PI) * radians
}
/**
* 角度转弧度
* @param degrees
*/
public static degreesToRadians(degrees: number) {
return (Math.PI * degrees) / 180
}
/**
* 返回2点间的弧度
* @param startP
* @param endP
*/
public static p2pRad(startP: Vec2, endP: Vec2) {
const rad: number = Math.atan2(endP.y - startP.y, endP.x - startP.x)
return rad
}
/**
* 针对捕鱼鱼的方向特定实现的鱼方向转换
* @param rot
*/
public static rotation2Fish(rot: number) {
if (rot >= 0 && rot <= 180) rot = 180 - rot
else rot = -180 - rot
return rot
}
}