Add parameter condition & dynamic behavior limit option

This commit is contained in:
MrKBear 2022-04-10 17:46:24 +08:00
parent 3351769106
commit 9979203fda
3 changed files with 43 additions and 16 deletions

View File

@ -7,7 +7,8 @@ type IDynamicsBehaviorParameter = {
mass: "number",
maxAcceleration: "number",
maxVelocity: "number",
resistance: "number"
resistance: "number",
limit: "boolean"
}
type IDynamicsBehaviorEvent = {}
@ -26,9 +27,16 @@ class Dynamics extends Behavior<IDynamicsBehaviorParameter, IDynamicsBehaviorEve
public override parameterOption = {
mass: { name: "$Mass", type: "number", defaultValue: 1, numberStep: .01, numberMin: .001 },
maxAcceleration: { name: "$Max.Acceleration", type: "number", defaultValue: 5, numberStep: .1, numberMin: 0 },
maxVelocity: { name: "$Max.Velocity", type: "number", defaultValue: 10, numberStep: .1, numberMin: 0 },
resistance: { name: "$Resistance", type: "number", defaultValue: 0.5, numberStep: .1, numberMin: 0 }
resistance: { name: "$Resistance", type: "number", defaultValue: 0.5, numberStep: .1, numberMin: 0 },
limit: { name: "$Limit", type: "boolean", defaultValue: true },
maxAcceleration: {
name: "$Max.Acceleration", type: "number", defaultValue: 5,
numberStep: .1, numberMin: 0, condition: { key: "limit", value: true }
},
maxVelocity: {
name: "$Max.Velocity", type: "number", defaultValue: 10,
numberStep: .1, numberMin: 0, condition: { key: "limit", value: true }
},
};
public override finalEffect = (individual: Individual, group: Group, model: Model, t: number): void => {
@ -54,24 +62,28 @@ class Dynamics extends Behavior<IDynamicsBehaviorParameter, IDynamicsBehaviorEve
individual.acceleration[2] = individual.force[2] / this.parameter.mass;
// 加速度约束
const lengthA = individual.vectorLength(individual.acceleration);
if (lengthA > this.parameter.maxAcceleration) {
individual.acceleration[0] = individual.acceleration[0] * this.parameter.maxAcceleration / lengthA;
individual.acceleration[1] = individual.acceleration[1] * this.parameter.maxAcceleration / lengthA;
individual.acceleration[2] = individual.acceleration[2] * this.parameter.maxAcceleration / lengthA;
if (this.parameter.limit) {
const lengthA = individual.vectorLength(individual.acceleration);
if (lengthA > this.parameter.maxAcceleration) {
individual.acceleration[0] = individual.acceleration[0] * this.parameter.maxAcceleration / lengthA;
individual.acceleration[1] = individual.acceleration[1] * this.parameter.maxAcceleration / lengthA;
individual.acceleration[2] = individual.acceleration[2] * this.parameter.maxAcceleration / lengthA;
}
}
// 计算速度
individual.velocity[0] = individual.velocity[0] + individual.acceleration[0] * t;
individual.velocity[1] = individual.velocity[1] + individual.acceleration[1] * t;
individual.velocity[2] = individual.velocity[2] + individual.acceleration[2] * t;
// 速度约束
const lengthV = individual.vectorLength(individual.velocity);
if (lengthV > this.parameter.maxVelocity) {
individual.velocity[0] = individual.velocity[0] * this.parameter.maxVelocity / lengthV;
individual.velocity[1] = individual.velocity[1] * this.parameter.maxVelocity / lengthV;
individual.velocity[2] = individual.velocity[2] * this.parameter.maxVelocity / lengthV;
if (this.parameter.limit) {
const lengthV = individual.vectorLength(individual.velocity);
if (lengthV > this.parameter.maxVelocity) {
individual.velocity[0] = individual.velocity[0] * this.parameter.maxVelocity / lengthV;
individual.velocity[1] = individual.velocity[1] * this.parameter.maxVelocity / lengthV;
individual.velocity[2] = individual.velocity[2] * this.parameter.maxVelocity / lengthV;
}
}
// 应用速度
@ -94,6 +106,10 @@ class Dynamics extends Behavior<IDynamicsBehaviorParameter, IDynamicsBehaviorEve
"ZH_CN": "一切可以运动物体的必要行为,执行物理法则。",
"EN_US": "All necessary behaviors that can move objects and implement the laws of physics."
},
"$Limit": {
"ZH_CN": "开启限制",
"EN_US": "Enable limit"
},
"$Mass": {
"ZH_CN": "质量 (Kg)",
"EN_US": "Mass (Kg)"

View File

@ -32,9 +32,15 @@ class Parameter<P extends IParameter> extends Component<IParameterProps<P> & IMi
private renderParameter<K extends keyof P>
(key: K, option: IParameterOptionItem<P[K]>, value: IParamValue<P[K]>): ReactNode {
const language = this.props.setting?.language ?? "EN_US";
const indexKey = `${this.props.key}-${key}`;
// 条件检测
if (option.condition && this.props.value[option.condition.key] !== option.condition.value) {
return <Fragment key={indexKey}/>;
}
const type = option.type;
const language = this.props.setting?.language ?? "EN_US";
let keyI18n: string, keyI18nOption: Record<string, string> | undefined;
// Custom I18N

View File

@ -115,6 +115,11 @@ interface IParameterOptionItem<T extends IParamType = IParamType> {
*/
colorNormal?: boolean;
/**
*
*/
condition?: {key: string, value: any};
/**
*
*/