Skip to content

refactor: add softening and reuse Vector3 for gravity calculations#18

Open
faithia-anastasia wants to merge 1 commit intomainfrom
refactor/gravityUtils
Open

refactor: add softening and reuse Vector3 for gravity calculations#18
faithia-anastasia wants to merge 1 commit intomainfrom
refactor/gravityUtils

Conversation

@faithia-anastasia
Copy link
Contributor

・softeningを導入(微小量を加えて発散を防止)→距離が半径の和未満になったら力をゼロにするという内容の改善
・変数forceをuseMemo()を用いて宣言し再利用
・calc_gravity_forceをキャメルケースにした

if (distance < minDistance) {
return new THREE.Vector3(0, 0, 0);
// 距離が0の場合、力は0として扱う(ゼロ除算を避ける)
if (distance === 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EPS ** 2 を足しているため、distanceSq が0になることはなく、distance が0になることもないと思うので、このチェックは削除して、EPS の計算箇所に「ゼロ除算を避ける」という意図をコメントで説明するのがいいかと思います。

const direction = new THREE.Vector3().subVectors(sourcePos, targetPos);
const distanceSq = direction.lengthSq();
resultVec: THREE.Vector3,
): void {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

パフォーマンス上の理由(アロケーションを避ける)であれば納得できるのですが、そうでない場合は Vector3 を返す純粋な関数として定義した方が読みやすいかと思います。関数を純粋に保つことで、後々テストも書きやすくなります。React でもその思想が取り入れられてますね。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants