Skip to content

Commit d51d444

Browse files
salim4nclaude
andcommitted
feat(demo-car-circuit): 3D car learns to drive on an oval circuit
The hero demo of IgnitionAI: - OvalTrack: parametric oval with straights + semicircles, nearest-point search - CircuitEnv: implements TrainingEnv — car physics, track-relative observations, reward shaping (+1 on track, -10 off, +50 per lap) - 3D scene: R3F with track surface, edge glow, car mesh (box+wheels), sunset environment, contact shadows, top-down camera - Same proven UI: code panel, reward chart, Train/Inference/Stop/Reset + DQN/PPO - 18 tests passing (track geometry + circuit env) Server runs clean on port 3020. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 99699c6 commit d51d444

24 files changed

Lines changed: 1148 additions & 1 deletion

‎.specify/feature.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"feature_directory": "specs/011-demo-cartpole-3d"
2+
"feature_directory": "specs/012-demo-car-circuit"
33
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "demo-car-circuit",
3+
"private": true,
4+
"version": "0.0.0",
5+
"type": "module",
6+
"scripts": { "dev": "vite", "build": "vite build", "test": "vitest run" },
7+
"dependencies": {
8+
"@ignitionai/backend-tfjs": "workspace:*",
9+
"@ignitionai/core": "workspace:*",
10+
"@react-three/drei": "^10.0.6",
11+
"@react-three/fiber": "^9.1.2",
12+
"three": "^0.162.0",
13+
"react": "^19.0.0",
14+
"react-dom": "^19.0.0",
15+
"recharts": "^2.15.3",
16+
"zustand": "^5.0.3"
17+
},
18+
"devDependencies": {
19+
"@types/react": "^19.0.10",
20+
"@types/react-dom": "^19.0.4",
21+
"@types/three": "^0.162.0",
22+
"@vitejs/plugin-react": "^4.3.4",
23+
"typescript": "~5.7.2",
24+
"vite": "^6.3.1",
25+
"vitest": "^3.1.1"
26+
}
27+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import { useRef, useCallback } from 'react';
2+
import { IgnitionEnvTFJS } from '@ignitionai/backend-tfjs';
3+
import { CircuitEnv } from './circuit-env';
4+
import { Scene3D } from './Scene3D';
5+
import { RewardChart } from './RewardChart';
6+
import { CodePanel } from './CodePanel';
7+
import { Controls } from './Controls';
8+
import { useDemoStore } from './store';
9+
10+
export default function App() {
11+
const envRef = useRef<IgnitionEnvTFJS | null>(null);
12+
const circuitRef = useRef<CircuitEnv>(new CircuitEnv());
13+
const episodeRewardRef = useRef(0);
14+
const origStepRef = useRef<typeof CircuitEnv.prototype.step | null>(null);
15+
const origResetRef = useRef<typeof CircuitEnv.prototype.reset | null>(null);
16+
17+
const { setMode, updateCar, recordEpisode, resetStats } = useDemoStore();
18+
19+
const syncState = useCallback(() => {
20+
const c = circuitRef.current;
21+
updateCar({ carX: c.carX, carY: c.carY, carAngle: c.carAngle, stepCount: c.stepCount, laps: c.laps });
22+
}, [updateCar]);
23+
24+
const createEnv = useCallback(() => {
25+
const circuit = circuitRef.current;
26+
if (!origStepRef.current) {
27+
origStepRef.current = circuit.step.bind(circuit);
28+
origResetRef.current = circuit.reset.bind(circuit);
29+
circuit.step = (action: number | number[]) => {
30+
origStepRef.current!(action);
31+
episodeRewardRef.current += circuit.reward();
32+
syncState();
33+
};
34+
circuit.reset = () => {
35+
recordEpisode(episodeRewardRef.current);
36+
episodeRewardRef.current = 0;
37+
origResetRef.current!();
38+
syncState();
39+
};
40+
}
41+
const env = new IgnitionEnvTFJS(circuit);
42+
envRef.current = env;
43+
syncState();
44+
return env;
45+
}, [syncState, recordEpisode]);
46+
47+
const handleStart = useCallback(() => {
48+
if (!envRef.current) createEnv();
49+
envRef.current!.train(useDemoStore.getState().algorithm);
50+
setMode('training');
51+
}, [createEnv, setMode]);
52+
53+
const handleInfer = useCallback(() => {
54+
if (!envRef.current?.agent) return;
55+
envRef.current.stop();
56+
envRef.current.infer();
57+
setMode('inference');
58+
}, [setMode]);
59+
60+
const handleStop = useCallback(() => {
61+
envRef.current?.stop();
62+
setMode('stopped');
63+
}, [setMode]);
64+
65+
const handleReset = useCallback(() => {
66+
envRef.current?.stop();
67+
envRef.current?.agent?.dispose?.();
68+
envRef.current = null;
69+
origResetRef.current?.();
70+
episodeRewardRef.current = 0;
71+
resetStats();
72+
setMode('stopped');
73+
syncState();
74+
}, [resetStats, setMode, syncState]);
75+
76+
return (
77+
<div style={{ minHeight: '100vh', background: '#0a0a1a', color: '#e2e8f0', fontFamily: 'system-ui, sans-serif' }}>
78+
{/* Header */}
79+
<header style={{ textAlign: 'center', padding: '20px 0 4px' }}>
80+
<h1 style={{ margin: 0, fontSize: 28, fontWeight: 800, letterSpacing: '-0.5px' }}>
81+
Ignition<span style={{ color: '#6366f1' }}>AI</span> — Car Circuit
82+
</h1>
83+
<p style={{ margin: '4px 0 0', color: '#888', fontSize: 14 }}>
84+
Train RL agents in the browser. Deploy anywhere.
85+
</p>
86+
</header>
87+
88+
{/* 3D Scene — hero area */}
89+
<div style={{ height: '50vh', minHeight: 350, maxHeight: 500, margin: '0 auto', maxWidth: 1000 }}>
90+
<Scene3D />
91+
</div>
92+
93+
{/* Controls */}
94+
<Controls onStart={handleStart} onStop={handleStop} onReset={handleReset} onInfer={handleInfer} />
95+
96+
{/* Bottom: code + chart */}
97+
<div style={{ display: 'flex', gap: 24, padding: '8px 32px 24px', maxWidth: 1000, margin: '0 auto' }}>
98+
<div style={{ flex: '0 0 380px' }}>
99+
<CodePanel />
100+
</div>
101+
<div style={{ flex: 1, minWidth: 250 }}>
102+
<RewardChart />
103+
</div>
104+
</div>
105+
</div>
106+
);
107+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { RoundedBox } from '@react-three/drei';
2+
3+
interface Car3DProps {
4+
x: number;
5+
y: number;
6+
angle: number;
7+
}
8+
9+
export function Car3D({ x, y, angle }: Car3DProps) {
10+
return (
11+
<group position={[x, 0.15, y]} rotation={[0, -angle, 0]}>
12+
{/* Car body */}
13+
<RoundedBox args={[0.6, 0.15, 0.35]} radius={0.04} smoothness={4} castShadow receiveShadow>
14+
<meshStandardMaterial color="#3b82f6" metalness={0.8} roughness={0.2} />
15+
</RoundedBox>
16+
17+
{/* Wheels */}
18+
{/* Front-left */}
19+
<mesh position={[0.2, -0.06, 0.2]} rotation={[Math.PI / 2, 0, 0]} castShadow>
20+
<cylinderGeometry args={[0.05, 0.05, 0.04, 12]} />
21+
<meshStandardMaterial color="#1a1a2e" metalness={0.6} roughness={0.4} />
22+
</mesh>
23+
{/* Front-right */}
24+
<mesh position={[0.2, -0.06, -0.2]} rotation={[Math.PI / 2, 0, 0]} castShadow>
25+
<cylinderGeometry args={[0.05, 0.05, 0.04, 12]} />
26+
<meshStandardMaterial color="#1a1a2e" metalness={0.6} roughness={0.4} />
27+
</mesh>
28+
{/* Rear-left */}
29+
<mesh position={[-0.2, -0.06, 0.2]} rotation={[Math.PI / 2, 0, 0]} castShadow>
30+
<cylinderGeometry args={[0.05, 0.05, 0.04, 12]} />
31+
<meshStandardMaterial color="#1a1a2e" metalness={0.6} roughness={0.4} />
32+
</mesh>
33+
{/* Rear-right */}
34+
<mesh position={[-0.2, -0.06, -0.2]} rotation={[Math.PI / 2, 0, 0]} castShadow>
35+
<cylinderGeometry args={[0.05, 0.05, 0.04, 12]} />
36+
<meshStandardMaterial color="#1a1a2e" metalness={0.6} roughness={0.4} />
37+
</mesh>
38+
</group>
39+
);
40+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { useDemoStore } from './store';
2+
3+
export function CodePanel() {
4+
const { algorithm } = useDemoStore();
5+
const code = `import { IgnitionEnvTFJS } from 'ignitionai';
6+
import { CircuitEnv } from './circuit-env';
7+
8+
const circuit = new CircuitEnv();
9+
10+
const env = new IgnitionEnvTFJS(circuit);
11+
env.train('${algorithm}');
12+
13+
// Switch to inference after training:
14+
// env.infer();`;
15+
16+
return (
17+
<pre style={{ background: '#0d1117', borderRadius: 8, padding: 14, border: '1px solid #333', fontSize: 12, lineHeight: 1.7, color: '#e2e8f0', fontFamily: "'Fira Code', monospace", margin: 0, overflow: 'auto' }}>
18+
<span style={{ color: '#888', fontSize: 10 }}>// That's it. 7 lines.</span>{'\n'}
19+
{code}
20+
</pre>
21+
);
22+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import type { AlgorithmType } from '@ignitionai/core';
2+
import { useDemoStore } from './store';
3+
4+
interface ControlsProps { onStart: () => void; onStop: () => void; onReset: () => void; onInfer: () => void; }
5+
6+
export function Controls({ onStart, onStop, onReset, onInfer }: ControlsProps) {
7+
const { mode, algorithm, setAlgorithm, episodeCount, stepCount, laps } = useDemoStore();
8+
9+
return (
10+
<div style={{ display: 'flex', gap: 12, alignItems: 'center', justifyContent: 'center', padding: '12px 0', flexWrap: 'wrap' }}>
11+
{mode === 'stopped' && <button onClick={onStart} style={btn('#22c55e')}>Train</button>}
12+
{mode === 'training' && <button onClick={onStop} style={btn('#ef4444')}>Stop</button>}
13+
{mode === 'training' && <button onClick={onInfer} style={btn('#3b82f6')}>Inference</button>}
14+
{mode === 'inference' && <button onClick={onStart} style={btn('#22c55e')}>Train</button>}
15+
{mode === 'inference' && <button onClick={onStop} style={btn('#ef4444')}>Stop</button>}
16+
<button onClick={onReset} style={btn('#6366f1')}>Reset</button>
17+
<select value={algorithm} onChange={(e) => setAlgorithm(e.target.value as AlgorithmType)}
18+
style={{ padding: '8px 12px', background: '#1a1a2e', color: '#e2e8f0', border: '1px solid #555', borderRadius: 6, fontSize: 14 }}>
19+
<option value="dqn">DQN</option>
20+
<option value="ppo">PPO</option>
21+
</select>
22+
<span style={{ fontSize: 12, color: '#888' }}>Ep: {episodeCount} | Steps: {stepCount} | Laps: {laps}</span>
23+
{mode !== 'stopped' && (
24+
<span style={{ fontSize: 12, fontWeight: 700, color: mode === 'training' ? '#22c55e' : '#3b82f6' }}>
25+
{mode.toUpperCase()}
26+
</span>
27+
)}
28+
</div>
29+
);
30+
}
31+
32+
function btn(c: string): React.CSSProperties {
33+
return { padding: '8px 24px', background: c, color: '#fff', border: 'none', borderRadius: 6, fontSize: 14, fontWeight: 600, cursor: 'pointer' };
34+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer } from 'recharts';
2+
import { useDemoStore } from './store';
3+
4+
export function RewardChart() {
5+
const { rewardHistory } = useDemoStore();
6+
const data = rewardHistory.slice(-300).map((r, i) => ({ ep: rewardHistory.length - 300 + i + 1, steps: r }));
7+
8+
if (data.length === 0) return <div style={{ color: '#666', fontSize: 13, textAlign: 'center', paddingTop: 40 }}>Press Train to begin</div>;
9+
10+
return (
11+
<ResponsiveContainer width="100%" height={180}>
12+
<LineChart data={data}>
13+
<CartesianGrid strokeDasharray="3 3" stroke="#1e1e3a" />
14+
<XAxis dataKey="ep" stroke="#666" fontSize={10} />
15+
<YAxis stroke="#666" fontSize={10} />
16+
<Tooltip contentStyle={{ background: '#1a1a2e', border: '1px solid #333', fontSize: 11 }} />
17+
<Line type="monotone" dataKey="steps" stroke="#22c55e" dot={false} strokeWidth={2} />
18+
</LineChart>
19+
</ResponsiveContainer>
20+
);
21+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { Canvas } from '@react-three/fiber';
2+
import { OrbitControls, Environment, ContactShadows } from '@react-three/drei';
3+
import { Track3D } from './Track3D';
4+
import { Car3D } from './Car3D';
5+
import { useDemoStore } from './store';
6+
7+
function SceneContent() {
8+
const { carX, carY, carAngle, mode } = useDemoStore();
9+
10+
const glowColor = mode === 'training' ? '#22c55e' : mode === 'inference' ? '#3b82f6' : '#666';
11+
12+
return (
13+
<>
14+
{/* Lighting */}
15+
<ambientLight intensity={0.4} />
16+
<directionalLight
17+
position={[5, 12, 5]}
18+
intensity={1.2}
19+
castShadow
20+
shadow-mapSize={[2048, 2048]}
21+
shadow-camera-far={40}
22+
shadow-camera-left={-20}
23+
shadow-camera-right={20}
24+
shadow-camera-top={20}
25+
shadow-camera-bottom={-20}
26+
/>
27+
<pointLight position={[-5, 6, -5]} intensity={0.3} color="#6366f1" />
28+
29+
{/* Environment */}
30+
<Environment preset="sunset" />
31+
<ContactShadows position={[0, -0.01, 0]} opacity={0.4} scale={30} blur={2} />
32+
33+
{/* Scene */}
34+
<Track3D />
35+
<Car3D x={carX} y={carY} angle={carAngle} />
36+
37+
{/* Mode indicator: subtle ground glow */}
38+
<mesh position={[0, -0.004, 0]} rotation={[-Math.PI / 2, 0, 0]}>
39+
<planeGeometry args={[30, 20]} />
40+
<meshStandardMaterial color={glowColor} transparent opacity={0.1} emissive={glowColor} emissiveIntensity={0.4} />
41+
</mesh>
42+
43+
{/* Camera controls */}
44+
<OrbitControls
45+
autoRotate={false}
46+
enablePan={false}
47+
minDistance={8}
48+
maxDistance={35}
49+
minPolarAngle={Math.PI / 8}
50+
maxPolarAngle={Math.PI / 2.5}
51+
/>
52+
</>
53+
);
54+
}
55+
56+
export function Scene3D() {
57+
return (
58+
<Canvas
59+
shadows
60+
camera={{ position: [0, 20, 15], fov: 45 }}
61+
style={{ borderRadius: 12 }}
62+
>
63+
<SceneContent />
64+
</Canvas>
65+
);
66+
}

0 commit comments

Comments
 (0)