Skip to content

Feature/collision detection#5

Open
faithia-anastasia wants to merge 3 commits intomainfrom
feature/collision-detection
Open

Feature/collision detection#5
faithia-anastasia wants to merge 3 commits intomainfrom
feature/collision-detection

Conversation

@faithia-anastasia
Copy link
Contributor

暫定的に衝突判定を実装
・とりあえず惑星2つを配列に入れて処理
・Simulation関数をいったん作り、内部で並進の処理と一緒に衝突判定を実施
・爆発のエフェクトを仮で実装

Copy link
Contributor

@tknkaa tknkaa left a comment

Choose a reason for hiding this comment

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

細かい点についてはレビューを見てほしいのですが、それとは別でシミュレーションゲームにするには衝突をシミュレートするためのボタンがあるといいかと思います。

))}
<Simulation planets={planets} setExplosions={setExplosions} />
{explosions.map((exp, idx) => (
<Explosion key={idx} planet={exp} />
Copy link
Contributor

Choose a reason for hiding this comment

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

ここも key に配列のインデックスを使ってますね。

return (
<group ref={groupRef}>
{fragments.map((f, i) => (
<primitive key={i} object={f.mesh} />
Copy link
Contributor

Choose a reason for hiding this comment

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

別ファイルのレビューで指摘したように key に配列のインデックスを指定するのは一般的に React のアンチパターンとされています。ただ、この場合は追加削除がなさそうなので、バグの原因はならない可能性があります。

}
interface SimulationProps {
planets: Planet[];
setExplosions: React.Dispatch<React.SetStateAction<Planet[]>>;
Copy link
Contributor

Choose a reason for hiding this comment

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

これもついやりがちなんですけど、基本的に状態管理は親コンポーネントでやりたいので、できれば setter を props で渡すのは避けてほしいです。今回なら、以下のような書き方ができます。

// 子
if (isColliding(a, b)) {
  if (!collidedPairsRef.current.has(key)) {
    collidedPairsRef.current.add(key)
    onExplosion(a, b)  // 「衝突した」という事実だけ伝える
  }
}

// props の型
interface SimulationProps {
  planets: Planet[]
  onExplosion: (a: Planet, b: Planet) => void
}

// 親
<Simulation
  planets={planets}
  onExplosion={(a, b) => setExplosions(prev => [...prev, a, b])}
/>


setFragments((prev) => {
const alive: Fragment[] = [];
prev.forEach((f) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

別ファイルのレビューで指摘したように for 文に統一するのがいいと思います。


type ExplosionProps = {
planet: Planet;
fragmentCount?: number;
Copy link
Contributor

Choose a reason for hiding this comment

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

fragmentCount が undefined になる状況がよく分からなかったのですが、もし undefined になる状況がないなら ? は外すと後で undefined かどうかチェックをする必要がなくなるかと思います。

fragmentCount = 50,
onComplete,
}: ExplosionProps) => {
const groupRef = useRef<THREE.Group>(null);
Copy link
Contributor

Choose a reason for hiding this comment

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

これは型エラーになるかと思います。<THREE.Group | null> のように型を広げる必要がありそうです。

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

Comments