Skip to content

Commit d7e0938

Browse files
committed
add approx functions
1 parent a0a2e97 commit d7e0938

2 files changed

Lines changed: 54 additions & 2 deletions

File tree

cocos/core/math/utils.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,58 @@ export function approx (a: number, b: number, maxDiff?: number): boolean {
6969
return mathAbs(a - b) <= maxDiff;
7070
}
7171

72+
/**
73+
* @en Tests whether or not the first argument is approximately greater than or equal to the second
74+
* @zh 测试第一个参数是否近似大于或等于第二个参数
75+
* @param a The first number to test
76+
* @param b The second number to test
77+
* @param maxDiff Maximum difference (default: EPSILON)
78+
* @return True if a is approximately greater than or equal to b, false otherwise
79+
*/
80+
export function approxGE (a: number, b: number, maxDiff?: number): boolean {
81+
maxDiff = maxDiff || EPSILON;
82+
return a >= b - maxDiff;
83+
}
84+
85+
/**
86+
* @en Tests whether or not the first argument is approximately less than or equal to the second
87+
* @zh 测试第一个参数是否近似小于或等于第二个参数
88+
* @param a The first number to test
89+
* @param b The second number to test
90+
* @param maxDiff Maximum difference (default: EPSILON)
91+
* @return True if a is approximately less than or equal to b, false otherwise
92+
*/
93+
export function approxLE (a: number, b: number, maxDiff?: number): boolean {
94+
maxDiff = maxDiff || EPSILON;
95+
return a <= b + maxDiff;
96+
}
97+
98+
/**
99+
* @en Tests whether or not the first argument is approximately greater than the second
100+
* @zh 测试第一个参数是否近似大于第二个参数
101+
* @param a The first number to test
102+
* @param b The second number to test
103+
* @param maxDiff Maximum difference (default: EPSILON)
104+
* @return True if a is approximately greater than b, false otherwise
105+
*/
106+
export function approxGT (a: number, b: number, maxDiff?: number): boolean {
107+
maxDiff = maxDiff || EPSILON;
108+
return a > b + maxDiff;
109+
}
110+
111+
/**
112+
* @en Tests whether or not the first argument is approximately less than the second
113+
* @zh 测试第一个参数是否近似小于第二个参数
114+
* @param a The first number to test
115+
* @param b The second number to test
116+
* @param maxDiff Maximum difference (default: EPSILON)
117+
* @return True if a is approximately less than b, false otherwise
118+
*/
119+
export function approxLT (a: number, b: number, maxDiff?: number): boolean {
120+
maxDiff = maxDiff || EPSILON;
121+
return a < b - maxDiff;
122+
}
123+
72124
/**
73125
* @en Clamps a value between a minimum float and maximum float value.<br/>
74126
* @zh 返回最小浮点数和最大浮点数之间的一个数值。可以使用 clamp 函数将不断变化的数值限制在范围内。

cocos/particle/burst.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424

2525
import { ccclass, type, serializable, editable, range } from 'cc.decorator';
26-
import { repeat } from '../core/math';
26+
import { approxGE, approxLT, repeat } from '../core/math';
2727
import CurveRange from './animator/curve-range';
2828
import type { ParticleSystem } from './particle-system';
2929

@@ -108,7 +108,7 @@ export default class Burst {
108108
let preFrameTime = repeat(psys.time - psys.startDelay.evaluate(0, 1), psys.duration) - dt;
109109
preFrameTime = (preFrameTime > 0.0) ? preFrameTime : 0.0;
110110
const curFrameTime = repeat(psys.time - psys.startDelay.evaluate(0, 1), psys.duration);
111-
if (this._curTime >= preFrameTime && this._curTime < curFrameTime) {
111+
if (approxGE(this._curTime, preFrameTime) && approxLT(this._curTime, curFrameTime)) {
112112
psys.emit(this.count.evaluate(this._curTime / psys.duration, 1), dt - (curFrameTime - this._curTime));
113113
this._curTime += this.repeatInterval;
114114
--this._remainingCount;

0 commit comments

Comments
 (0)