-
Notifications
You must be signed in to change notification settings - Fork 0
feat: 最初の衝突までの軌道を予測する線を追加 #11
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
Rn86222
wants to merge
4
commits into
main
Choose a base branch
from
feat/trajectory-pred
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| import { useFrame } from "@react-three/fiber"; | ||
| import { useMemo, useRef } from "react"; | ||
| import * as THREE from "three"; | ||
| import { BALL_RADIUS } from "../constants/physics"; | ||
|
|
||
| const DOT_Y = 0.05; | ||
| const MAX_DISTANCE = 100; | ||
| const COLLISION_RADIUS = BALL_RADIUS * 2; | ||
| const DOT_SPACING = 0.08; | ||
| const DOT_SIZE = 3; | ||
| const MAX_DOTS = 128; | ||
| const DOT_START_OFFSET = BALL_RADIUS * 1.5; | ||
|
|
||
| /** | ||
| * レイ(origin, direction)と円(center, radius)の交差判定。 | ||
| * 交差する場合、最初の交差点までの距離tを返す。交差しない場合はnullを返す。 | ||
| */ | ||
| function rayCircleIntersect( | ||
| ox: number, | ||
| oz: number, | ||
| dx: number, | ||
| dz: number, | ||
| cx: number, | ||
| cz: number, | ||
| r: number, | ||
| ): number | null { | ||
| const lx = cx - ox; | ||
| const lz = cz - oz; | ||
| const tca = lx * dx + lz * dz; | ||
| if (tca < 0) return null; | ||
|
|
||
| const d2 = lx * lx + lz * lz - tca * tca; | ||
| const r2 = r * r; | ||
| if (d2 > r2) return null; | ||
|
|
||
| const thc = Math.sqrt(r2 - d2); | ||
| const t = tca - thc; | ||
| return t > 1e-6 ? t : null; | ||
| } | ||
|
|
||
| type TrajectoryLineRaycastProps = { | ||
| ballPositionRef: React.RefObject<Record<string, [number, number, number]>>; | ||
| cueBallId: string; | ||
| visibleBallIds: string[]; | ||
| visible: boolean; | ||
| }; | ||
|
|
||
| export function TrajectoryLineRaycast({ | ||
| ballPositionRef, | ||
| cueBallId, | ||
| visibleBallIds, | ||
| visible, | ||
| }: TrajectoryLineRaycastProps) { | ||
| const pointsRef = useRef<THREE.Points>(null); | ||
| const raycaster = useMemo(() => new THREE.Raycaster(), []); | ||
| const workDirection = useMemo(() => new THREE.Vector3(), []); | ||
| const workOrigin = useMemo(() => new THREE.Vector3(), []); | ||
|
|
||
| const pointsObject = useMemo(() => { | ||
| const geom = new THREE.BufferGeometry(); | ||
| const positions = new Float32Array(MAX_DOTS * 3); | ||
| geom.setAttribute("position", new THREE.BufferAttribute(positions, 3)); | ||
| geom.setDrawRange(0, 0); | ||
| const mat = new THREE.PointsMaterial({ | ||
| color: 0xffffff, | ||
| size: DOT_SIZE, | ||
| sizeAttenuation: false, | ||
| transparent: true, | ||
| opacity: 0.9, | ||
| }); | ||
| const points = new THREE.Points(geom, mat); | ||
| points.raycast = () => {}; | ||
| return points; | ||
| }, []); | ||
|
|
||
| useFrame(({ camera, scene }) => { | ||
| const points = pointsRef.current; | ||
| if (!points) return; | ||
|
|
||
| if (!visible) { | ||
| points.visible = false; | ||
| return; | ||
| } | ||
|
|
||
| const ballPos = ballPositionRef.current?.[cueBallId]; | ||
| if (!ballPos) { | ||
| points.visible = false; | ||
| return; | ||
| } | ||
|
|
||
| const dirX = ballPos[0] - camera.position.x; | ||
| const dirZ = ballPos[2] - camera.position.z; | ||
| const len = Math.sqrt(dirX * dirX + dirZ * dirZ); | ||
| if (len < 1e-6) { | ||
| points.visible = false; | ||
| return; | ||
| } | ||
|
|
||
| const dx = dirX / len; | ||
| const dz = dirZ / len; | ||
| workDirection.set(dx, 0, dz); | ||
| workOrigin.set(ballPos[0], ballPos[1], ballPos[2]); | ||
|
|
||
| let minDist = MAX_DISTANCE; | ||
|
|
||
| // 1. 壁・クッション・障害物はレイキャストで検出 | ||
| // ヒット距離からBALL_RADIUSを引いて、キュー球の半径分を補正 | ||
| raycaster.set(workOrigin, workDirection); | ||
| raycaster.far = MAX_DISTANCE; | ||
|
|
||
| // 距離昇順でソート済み(Three.js仕様)の結果から、 | ||
| // ボール(SphereGeometry)をスキップして最初の壁・障害物を探す | ||
| const intersects = raycaster.intersectObjects(scene.children, true); | ||
| for (const hit of intersects) { | ||
|
Rn86222 marked this conversation as resolved.
|
||
| if (hit.object.name === cueBallId) continue; | ||
| if ( | ||
| hit.object instanceof THREE.Mesh && | ||
| hit.object.geometry instanceof THREE.SphereGeometry | ||
| ) { | ||
| continue; | ||
| } | ||
| const adjusted = hit.distance - BALL_RADIUS; | ||
| if (adjusted > 0 && adjusted < minDist) { | ||
| minDist = adjusted; | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| // 2. ボール同士の衝突はballPositionRefから直接判定 | ||
| for (const id of visibleBallIds) { | ||
| if (id === cueBallId) continue; | ||
| const pos = ballPositionRef.current?.[id]; | ||
| if (!pos) continue; | ||
|
|
||
| const t = rayCircleIntersect( | ||
| workOrigin.x, | ||
| workOrigin.z, | ||
| dx, | ||
| dz, | ||
| pos[0], | ||
| pos[2], | ||
| COLLISION_RADIUS, | ||
| ); | ||
| if (t !== null && t < minDist) { | ||
| minDist = t; | ||
| } | ||
| } | ||
|
|
||
| // キュー球から衝突点まで等間隔にドットを配置 | ||
| const geom = pointsObject.geometry; | ||
| const attr = geom.getAttribute("position"); | ||
| const arr = attr.array; | ||
|
|
||
| let dotCount = 0; | ||
| for ( | ||
| let d = DOT_START_OFFSET; | ||
| d < minDist && dotCount < MAX_DOTS; | ||
| d += DOT_SPACING | ||
| ) { | ||
| const idx = dotCount * 3; | ||
| arr[idx] = ballPos[0] + dx * d; | ||
| arr[idx + 1] = DOT_Y; | ||
| arr[idx + 2] = ballPos[2] + dz * d; | ||
| dotCount++; | ||
| } | ||
|
|
||
| attr.needsUpdate = true; | ||
| geom.setDrawRange(0, dotCount); | ||
| points.visible = true; | ||
| }); | ||
|
|
||
| return <primitive object={pointsObject} ref={pointsRef} />; | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.