-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/collision detection #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
faithia-anastasia
wants to merge
5
commits into
main
Choose a base branch
from
feature/collision-detection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+191
−3
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
787d0b2
feat(collision): add function to detect collisions
faithia-anastasia 9ca1049
feat(collision): add temporary explosion effect for planets
faithia-anastasia 8dabb41
feat(collision): add temporary collision detection
faithia-anastasia 525b6b1
chore: run npm check
faithia-anastasia e74c484
fix: address some review comments
faithia-anastasia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import { useFrame } from "@react-three/fiber"; | ||
| import { useEffect, useRef, useState } from "react"; | ||
| import * as THREE from "three"; | ||
| import type { Planet } from "@/types/planet"; | ||
|
|
||
| type Fragment = { | ||
| mesh: THREE.Mesh; | ||
| velocity: THREE.Vector3; | ||
| rotationAxis: THREE.Vector3; | ||
| lifetime: number; | ||
| }; | ||
|
|
||
| type ExplosionProps = { | ||
| planet: Planet; | ||
| fragmentCount?: number; | ||
| onComplete?: () => void; | ||
| }; | ||
|
|
||
| export const Explosion: React.FC<ExplosionProps> = ({ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. コンポーネント関数は無名関数ではなく |
||
| planet, | ||
| fragmentCount = 50, | ||
| onComplete, | ||
| }: ExplosionProps) => { | ||
| const groupRef = useRef<THREE.Group>(null); | ||
| const [fragments, setFragments] = useState<Fragment[]>([]); | ||
|
|
||
| // 爆発初期化 | ||
| useEffect(() => { | ||
| const newFragments: Fragment[] = []; | ||
| for (let i = 0; i < fragmentCount; i++) { | ||
| const size = Math.random() * (planet.radius * 0.2) + 0.05; | ||
| const geometry = new THREE.SphereGeometry(size, 6, 6); | ||
| const material = new THREE.MeshStandardMaterial({ | ||
| color: 0xffaa33, | ||
| emissive: 0xff5500, | ||
| }); | ||
| const mesh = new THREE.Mesh(geometry, material); | ||
|
|
||
| // 初期位置は惑星中心 | ||
| mesh.position.copy(planet.position); | ||
|
|
||
| // ランダム方向に飛ぶ速度 | ||
| const velocity = new THREE.Vector3( | ||
| (Math.random() - 0.5) * 4, | ||
| (Math.random() - 0.5) * 4, | ||
| (Math.random() - 0.5) * 4, | ||
| ); | ||
|
|
||
| // 回転軸 | ||
| const rotationAxis = new THREE.Vector3( | ||
| Math.random(), | ||
| Math.random(), | ||
| Math.random(), | ||
| ).normalize(); | ||
|
|
||
| newFragments.push({ | ||
| mesh, | ||
| velocity, | ||
| rotationAxis, | ||
| lifetime: Math.random() * 2 + 1, // 1~3秒で消える | ||
| }); | ||
| } | ||
| setFragments(newFragments); | ||
| }, [planet, fragmentCount]); | ||
|
|
||
| // フレームごとの更新 | ||
| useFrame((_, delta) => { | ||
| if (fragments.length === 0) return; | ||
|
|
||
| setFragments((prev) => { | ||
| const alive: Fragment[] = []; | ||
| prev.forEach((f) => { | ||
| // 位置更新 | ||
| f.mesh.position.add(f.velocity.clone().multiplyScalar(delta)); | ||
|
|
||
| // 回転 | ||
| f.mesh.rotateOnAxis(f.rotationAxis, delta * 5); | ||
|
|
||
| // 減速(摩擦的) | ||
| f.velocity.multiplyScalar(0.98); | ||
|
|
||
| // 減衰 | ||
| f.lifetime -= delta; | ||
| if (f.lifetime > 0) alive.push(f); | ||
| else f.mesh.parent?.remove(f.mesh); // Group から削除 | ||
| }); | ||
|
|
||
| // 爆発完了通知 | ||
| if (alive.length === 0 && onComplete) onComplete(); | ||
|
|
||
| return alive; | ||
| }); | ||
| }); | ||
|
|
||
| return ( | ||
| <group ref={groupRef}> | ||
| {fragments.map((f, i) => ( | ||
| <primitive key={i} object={f.mesh} /> | ||
| ))} | ||
| </group> | ||
| ); | ||
| }; | ||
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| import type * as THREE from "three"; | ||
| export type Planet = { | ||
| name: string; | ||
| texturePath: string; | ||
| rotationSpeedY: number; | ||
| radius: number; | ||
| width: number; | ||
| height: number; | ||
| position: THREE.Vector3; | ||
| velocity: THREE.Vector3; | ||
| }; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import type { Planet } from "@/types/planet"; | ||
|
|
||
| export function isColliding(planet1: Planet, planet2: Planet): boolean { | ||
faithia-anastasia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const distanceSquared = planet1.position.distanceToSquared(planet2.position); | ||
|
|
||
| const radiusSum = planet1.radius + planet2.radius; | ||
|
|
||
| return distanceSquared <= radiusSum ** 2; | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
コード全体を通して、interface と type が混在していると思うのですが、どちらかに統一するのがいいと思います。type に統一するべきっていう意見もあれば interface に統一するべきっていう意見もあるのですが、個人的には type の方が直感的でいいかな、と思います。