refactor: add softening and reuse Vector3 for gravity calculations#18
Open
faithia-anastasia wants to merge 1 commit intomainfrom
Open
refactor: add softening and reuse Vector3 for gravity calculations#18faithia-anastasia wants to merge 1 commit intomainfrom
faithia-anastasia wants to merge 1 commit intomainfrom
Conversation
tknkaa
reviewed
Mar 11, 2026
| if (distance < minDistance) { | ||
| return new THREE.Vector3(0, 0, 0); | ||
| // 距離が0の場合、力は0として扱う(ゼロ除算を避ける) | ||
| if (distance === 0) { |
Contributor
There was a problem hiding this comment.
EPS ** 2 を足しているため、distanceSq が0になることはなく、distance が0になることもないと思うので、このチェックは削除して、EPS の計算箇所に「ゼロ除算を避ける」という意図をコメントで説明するのがいいかと思います。
| const direction = new THREE.Vector3().subVectors(sourcePos, targetPos); | ||
| const distanceSq = direction.lengthSq(); | ||
| resultVec: THREE.Vector3, | ||
| ): void { |
Contributor
There was a problem hiding this comment.
パフォーマンス上の理由(アロケーションを避ける)であれば納得できるのですが、そうでない場合は Vector3 を返す純粋な関数として定義した方が読みやすいかと思います。関数を純粋に保つことで、後々テストも書きやすくなります。React でもその思想が取り入れられてますね。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
・softeningを導入(微小量を加えて発散を防止)→距離が半径の和未満になったら力をゼロにするという内容の改善
・変数forceをuseMemo()を用いて宣言し再利用
・calc_gravity_forceをキャメルケースにした