@iwsdk/create 0.4.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +103 -41
- package/dist/cli.js +1003 -3418
- package/dist/guidance/agents/.agents/skills/iwsdk-debug/SKILL.md +96 -0
- package/dist/guidance/agents/.agents/skills/iwsdk-depth-occlusion/SKILL.md +135 -0
- package/dist/guidance/agents/.agents/skills/iwsdk-grab/SKILL.md +129 -0
- package/dist/guidance/agents/.agents/skills/iwsdk-physics/SKILL.md +92 -0
- package/dist/guidance/agents/.agents/skills/iwsdk-physics/references/component-reference.md +154 -0
- package/dist/guidance/agents/.agents/skills/iwsdk-physics/references/tuning-and-config.md +88 -0
- package/dist/guidance/agents/.agents/skills/iwsdk-physics/references/workflows.md +303 -0
- package/dist/guidance/agents/.agents/skills/iwsdk-planner/SKILL.md +364 -0
- package/dist/guidance/agents/.agents/skills/iwsdk-planner/references/api-reference.md +1489 -0
- package/dist/guidance/agents/.agents/skills/iwsdk-planner/references/build-milestones.md +241 -0
- package/dist/guidance/agents/.agents/skills/iwsdk-planner/references/design-deck.md +101 -0
- package/dist/guidance/agents/.agents/skills/iwsdk-planner/references/grounding.md +176 -0
- package/dist/guidance/agents/.agents/skills/iwsdk-planner/references/ideation.md +156 -0
- package/dist/guidance/agents/.agents/skills/iwsdk-planner/references/verification.md +199 -0
- package/dist/guidance/agents/.agents/skills/iwsdk-ray/SKILL.md +159 -0
- package/dist/guidance/agents/.agents/skills/iwsdk-scene-composer/SKILL.md +255 -0
- package/dist/guidance/agents/.agents/skills/iwsdk-scene-composer/agents/openai.yaml +5 -0
- package/dist/guidance/agents/.agents/skills/iwsdk-scene-composer/references/asset-authoring.md +295 -0
- package/dist/guidance/agents/.agents/skills/iwsdk-scene-composer/references/composition-patterns.md +243 -0
- package/dist/guidance/agents/.agents/skills/iwsdk-scene-composer/references/image-intake.md +191 -0
- package/dist/guidance/agents/.agents/skills/iwsdk-scene-composer/references/review-and-stop.md +99 -0
- package/dist/guidance/agents/.agents/skills/iwsdk-scene-composer/references/scene-format.md +458 -0
- package/dist/guidance/agents/.agents/skills/iwsdk-scene-composer/references/text-intake.md +82 -0
- package/dist/guidance/agents/.agents/skills/iwsdk-ui/SKILL.md +208 -0
- package/dist/guidance/claude/.claude/agents/iwsdk-project-code-reviewer.md +450 -0
- package/dist/guidance/claude/.claude/rules/assets-and-manifest.md +55 -0
- package/dist/guidance/claude/.claude/rules/ecs-api.md +102 -0
- package/dist/guidance/claude/.claude/rules/scene-json.md +64 -0
- package/dist/guidance/claude/.claude/rules/uikitml.md +60 -0
- package/dist/guidance/claude/.claude/settings.json +19 -0
- package/dist/guidance/claude/.claude/skills/iwsdk-debug/SKILL.md +96 -0
- package/dist/guidance/claude/.claude/skills/iwsdk-depth-occlusion/SKILL.md +135 -0
- package/dist/guidance/claude/.claude/skills/iwsdk-grab/SKILL.md +129 -0
- package/dist/guidance/claude/.claude/skills/iwsdk-physics/SKILL.md +92 -0
- package/dist/guidance/claude/.claude/skills/iwsdk-physics/references/component-reference.md +154 -0
- package/dist/guidance/claude/.claude/skills/iwsdk-physics/references/tuning-and-config.md +88 -0
- package/dist/guidance/claude/.claude/skills/iwsdk-physics/references/workflows.md +303 -0
- package/dist/guidance/claude/.claude/skills/iwsdk-planner/SKILL.md +364 -0
- package/dist/guidance/claude/.claude/skills/iwsdk-planner/references/api-reference.md +1489 -0
- package/dist/guidance/claude/.claude/skills/iwsdk-planner/references/build-milestones.md +241 -0
- package/dist/guidance/claude/.claude/skills/iwsdk-planner/references/design-deck.md +101 -0
- package/dist/guidance/claude/.claude/skills/iwsdk-planner/references/grounding.md +176 -0
- package/dist/guidance/claude/.claude/skills/iwsdk-planner/references/ideation.md +156 -0
- package/dist/guidance/claude/.claude/skills/iwsdk-planner/references/verification.md +199 -0
- package/dist/guidance/claude/.claude/skills/iwsdk-ray/SKILL.md +159 -0
- package/dist/guidance/claude/.claude/skills/iwsdk-scene-composer/SKILL.md +255 -0
- package/dist/guidance/claude/.claude/skills/iwsdk-scene-composer/agents/openai.yaml +5 -0
- package/dist/guidance/claude/.claude/skills/iwsdk-scene-composer/references/asset-authoring.md +295 -0
- package/dist/guidance/claude/.claude/skills/iwsdk-scene-composer/references/composition-patterns.md +243 -0
- package/dist/guidance/claude/.claude/skills/iwsdk-scene-composer/references/image-intake.md +191 -0
- package/dist/guidance/claude/.claude/skills/iwsdk-scene-composer/references/review-and-stop.md +99 -0
- package/dist/guidance/claude/.claude/skills/iwsdk-scene-composer/references/scene-format.md +458 -0
- package/dist/guidance/claude/.claude/skills/iwsdk-scene-composer/references/text-intake.md +82 -0
- package/dist/guidance/claude/.claude/skills/iwsdk-ui/SKILL.md +208 -0
- package/dist/guidance/claude/CLAUDE.md +142 -0
- package/dist/guidance/codex/.codex/config.toml +2 -0
- package/dist/guidance/common/AGENTS.md +151 -0
- package/dist/guidance/copilot/.github/instructions/assets-and-manifest.instructions.md +52 -0
- package/dist/guidance/copilot/.github/instructions/ecs-api.instructions.md +100 -0
- package/dist/guidance/copilot/.github/instructions/scene-json.instructions.md +63 -0
- package/dist/guidance/copilot/.github/instructions/uikitml.instructions.md +59 -0
- package/dist/guidance/cursor/.cursor/rules/assets-and-manifest.mdc +57 -0
- package/dist/guidance/cursor/.cursor/rules/ecs-api.mdc +104 -0
- package/dist/guidance/cursor/.cursor/rules/scene-json.mdc +66 -0
- package/dist/guidance/cursor/.cursor/rules/uikitml.mdc +62 -0
- package/dist/guidance/scoped-agents/public/scenes/AGENTS.md +63 -0
- package/dist/guidance/scoped-agents/public/ui/AGENTS.md +59 -0
- package/dist/guidance/scoped-agents/src/AGENTS.md +149 -0
- package/dist/template/common/README.md +13 -0
- package/dist/template/common/gitignore.template +45 -0
- package/dist/template/common/index.html +16 -0
- package/dist/template/common/nvmrc.template +1 -0
- package/dist/template/common/public/audio/chime.mp3 +0 -0
- package/dist/template/common/public/gltf/webxr-banner/banner.gltf +68 -0
- package/dist/template/common/public/textures/webxr.png +0 -0
- package/dist/template/common/public/ui/welcome.uikitml +98 -0
- package/dist/template/common/src/assets.ts +54 -0
- package/dist/template/common/src/components.ts +11 -0
- package/dist/template/common/src/index.ts +19 -0
- package/dist/template/common/src/panel.ts +38 -0
- package/dist/template/common/src/robot-component.ts +10 -0
- package/dist/template/common/src/robot.ts +38 -0
- package/dist/template/common/src/vite-env.d.ts +2 -0
- package/dist/template/common/tsconfig.json +15 -0
- package/dist/template/common/vite.config.ts +27 -0
- package/dist/template/common-js/README.md +13 -0
- package/dist/template/common-js/gitignore.template +45 -0
- package/dist/template/common-js/index.html +16 -0
- package/dist/template/common-js/nvmrc.template +1 -0
- package/dist/template/common-js/public/audio/chime.mp3 +0 -0
- package/dist/template/common-js/public/gltf/webxr-banner/banner.gltf +68 -0
- package/dist/template/common-js/public/textures/webxr.png +0 -0
- package/dist/template/common-js/public/ui/welcome.uikitml +98 -0
- package/dist/template/common-js/src/assets.js +45 -0
- package/dist/template/common-js/src/components.js +9 -0
- package/dist/template/common-js/src/index.js +14 -0
- package/dist/template/common-js/src/panel.js +31 -0
- package/dist/template/common-js/src/robot-component.js +8 -0
- package/dist/template/common-js/src/robot.js +37 -0
- package/dist/template/common-js/vite.config.js +25 -0
- package/dist/template/scenes/ar.iwsdk.scene.json +96 -0
- package/dist/template/scenes/immersive.iwsdk.scene.json +109 -0
- package/package.json +23 -17
- package/scripts/verify-release-ready.mjs +76 -0
- package/LICENSE +0 -21
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
## Material Tuning Guide
|
|
2
|
+
|
|
3
|
+
Adjust `density`, `restitution`, and `friction` on `PhysicsShape` to simulate different materials:
|
|
4
|
+
|
|
5
|
+
| Material | Density | Restitution | Friction |
|
|
6
|
+
| ----------- | ------- | ----------- | -------- |
|
|
7
|
+
| Wood | 0.6 | 0.3 | 0.5 |
|
|
8
|
+
| Metal/Steel | 7.8 | 0.2 | 0.4 |
|
|
9
|
+
| Rubber | 1.1 | 0.8 | 0.9 |
|
|
10
|
+
| Ice | 0.9 | 0.1 | 0.05 |
|
|
11
|
+
| Concrete | 2.4 | 0.1 | 0.7 |
|
|
12
|
+
| Foam/Light | 0.05 | 0.1 | 0.6 |
|
|
13
|
+
| Bouncy ball | 1.0 | 0.95 | 0.5 |
|
|
14
|
+
|
|
15
|
+
## System Priority Order
|
|
16
|
+
|
|
17
|
+
Physics runs in a carefully orchestrated sequence:
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
Priority -5: LocomotionSystem (Player movement)
|
|
21
|
+
Priority -4: InputSystem (Controller/hand input)
|
|
22
|
+
Priority -3: GrabSystem (Grab interactions)
|
|
23
|
+
Priority -2: PhysicsSystem (Physics simulation)
|
|
24
|
+
Priority -1: SceneUnderstanding (AR plane/mesh updates)
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Register custom physics-related systems after the built-in PhysicsSystem (priority > -2) to read updated transforms:
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
world.registerSystem(MyPhysicsLogicSystem, { priority: 5 });
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## PhysicsSystem Configuration
|
|
34
|
+
|
|
35
|
+
The system accepts a `gravity` config (defaults to Earth gravity):
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
import { PhysicsSystem } from '@iwsdk/core';
|
|
39
|
+
|
|
40
|
+
const physicsSystem = world.getSystem(PhysicsSystem);
|
|
41
|
+
physicsSystem.config.gravity.value = [0, -9.81, 0]; // Earth gravity (default)
|
|
42
|
+
physicsSystem.config.gravity.value = [0, -1.62, 0]; // Moon gravity
|
|
43
|
+
physicsSystem.config.gravity.value = [0, 0, 0]; // Zero gravity
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Native Scene JSON Configuration
|
|
47
|
+
|
|
48
|
+
Physics components can be configured declaratively in native scene JSON files:
|
|
49
|
+
|
|
50
|
+
```json
|
|
51
|
+
{
|
|
52
|
+
"id": "dynamic-box",
|
|
53
|
+
"content": { "type": "asset", "asset": "box" },
|
|
54
|
+
"transform": { "position": [0, 1.5, -1] },
|
|
55
|
+
"components": {
|
|
56
|
+
"PhysicsShape": {
|
|
57
|
+
"shape": "Box",
|
|
58
|
+
"dimensions": [1, 1, 1],
|
|
59
|
+
"density": 1,
|
|
60
|
+
"friction": 0.5,
|
|
61
|
+
"restitution": 0
|
|
62
|
+
},
|
|
63
|
+
"PhysicsBody": {
|
|
64
|
+
"state": "DYNAMIC",
|
|
65
|
+
"gravityFactor": 1,
|
|
66
|
+
"linearDamping": 0,
|
|
67
|
+
"angularDamping": 0
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
**State enum values in scene JSON:**
|
|
74
|
+
|
|
75
|
+
- `STATIC`
|
|
76
|
+
- `DYNAMIC`
|
|
77
|
+
- `KINEMATIC`
|
|
78
|
+
|
|
79
|
+
**Shape enum values in scene JSON:**
|
|
80
|
+
|
|
81
|
+
- `Sphere`
|
|
82
|
+
- `Box`
|
|
83
|
+
- `Cylinder`
|
|
84
|
+
- `Capsules`
|
|
85
|
+
- `ConvexHull`
|
|
86
|
+
- `TriMesh`
|
|
87
|
+
- `Auto`
|
|
88
|
+
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
## Common Workflows
|
|
2
|
+
|
|
3
|
+
### Creating a Dynamic Physics Object
|
|
4
|
+
|
|
5
|
+
```typescript
|
|
6
|
+
import {
|
|
7
|
+
Mesh,
|
|
8
|
+
SphereGeometry,
|
|
9
|
+
MeshStandardMaterial,
|
|
10
|
+
Color,
|
|
11
|
+
FrontSide,
|
|
12
|
+
} from 'three';
|
|
13
|
+
import {
|
|
14
|
+
PhysicsShape,
|
|
15
|
+
PhysicsShapeType,
|
|
16
|
+
PhysicsBody,
|
|
17
|
+
PhysicsState,
|
|
18
|
+
PhysicsManipulation,
|
|
19
|
+
} from '@iwsdk/core';
|
|
20
|
+
|
|
21
|
+
// 1. Create Three.js mesh
|
|
22
|
+
const ball = new Mesh(
|
|
23
|
+
new SphereGeometry(0.2),
|
|
24
|
+
new MeshStandardMaterial({ color: new Color(0xff4444), side: FrontSide }),
|
|
25
|
+
);
|
|
26
|
+
ball.position.set(0, 2, -1);
|
|
27
|
+
|
|
28
|
+
// 2. Wrap as ECS entity
|
|
29
|
+
const entity = world.createTransformEntity(ball);
|
|
30
|
+
|
|
31
|
+
// 3. Add physics components
|
|
32
|
+
entity.addComponent(PhysicsShape, {
|
|
33
|
+
shape: PhysicsShapeType.Sphere,
|
|
34
|
+
dimensions: [0.2, 0, 0],
|
|
35
|
+
restitution: 0.6, // Bouncy
|
|
36
|
+
});
|
|
37
|
+
entity.addComponent(PhysicsBody, { state: PhysicsState.Dynamic });
|
|
38
|
+
|
|
39
|
+
// 4. Optional: apply initial impulse
|
|
40
|
+
entity.addComponent(PhysicsManipulation, { force: [5, 2, 0] });
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Creating a Static Environment Collider
|
|
44
|
+
|
|
45
|
+
For walls, floors, and fixed scenery that block dynamic objects but never move:
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
// Ground plane
|
|
49
|
+
const ground = new Mesh(
|
|
50
|
+
new BoxGeometry(10, 0.1, 10),
|
|
51
|
+
new MeshStandardMaterial({ color: 0x888888 }),
|
|
52
|
+
);
|
|
53
|
+
ground.position.set(0, -0.05, 0);
|
|
54
|
+
|
|
55
|
+
const groundEntity = world.createTransformEntity(ground);
|
|
56
|
+
groundEntity.addComponent(PhysicsShape, {
|
|
57
|
+
shape: PhysicsShapeType.Box,
|
|
58
|
+
dimensions: [10, 0.1, 10],
|
|
59
|
+
friction: 0.8,
|
|
60
|
+
});
|
|
61
|
+
groundEntity.addComponent(PhysicsBody, { state: PhysicsState.Static });
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
For complex static geometry (GLTF environments), use `TriMesh` for exact collision:
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
envEntity.addComponent(PhysicsShape, { shape: PhysicsShapeType.TriMesh });
|
|
68
|
+
envEntity.addComponent(PhysicsBody, { state: PhysicsState.Static });
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Creating a Kinematic Moving Platform
|
|
72
|
+
|
|
73
|
+
Kinematic bodies are moved by code and push dynamic objects:
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
// Setup
|
|
77
|
+
const platform = new Mesh(
|
|
78
|
+
new BoxGeometry(3, 0.2, 3),
|
|
79
|
+
new MeshStandardMaterial({ color: 0x4488ff }),
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
const platformEntity = world.createTransformEntity(platform);
|
|
83
|
+
platformEntity.addComponent(PhysicsShape, {
|
|
84
|
+
shape: PhysicsShapeType.Box,
|
|
85
|
+
dimensions: [3, 0.2, 3],
|
|
86
|
+
});
|
|
87
|
+
platformEntity.addComponent(PhysicsBody, { state: PhysicsState.Kinematic });
|
|
88
|
+
|
|
89
|
+
// In a system's update loop, move it:
|
|
90
|
+
update(delta, time) {
|
|
91
|
+
for (const entity of this.queries.platforms.entities) {
|
|
92
|
+
entity.object3D.position.y = 1 + Math.sin(time) * 2;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Resetting or Teleporting an Existing Body
|
|
98
|
+
|
|
99
|
+
Do not remove/re-add physics components or write only to the Object3D of a dynamic
|
|
100
|
+
body. Use the live physics system so Havok and the render transform update together:
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
import { PhysicsSystem } from '@iwsdk/core';
|
|
104
|
+
|
|
105
|
+
const physics = world.getSystem(PhysicsSystem);
|
|
106
|
+
physics.setBodyTransform(entity, {
|
|
107
|
+
position: homePosition,
|
|
108
|
+
quaternion: homeQuaternion,
|
|
109
|
+
});
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Velocity is cleared by default. Pass `{ resetVelocity: false }` only when a teleport
|
|
113
|
+
must preserve motion. `gravityFactor`, `linearDamping`, and `angularDamping` can be
|
|
114
|
+
changed reactively with `entity.setValue`; changing motion state or shape requires
|
|
115
|
+
deliberate body lifecycle handling.
|
|
116
|
+
|
|
117
|
+
### Making an Object Grabbable with Physics
|
|
118
|
+
|
|
119
|
+
Combine grab components with physics for throwable objects:
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
import {
|
|
123
|
+
RayInteractable,
|
|
124
|
+
OneHandGrabbable,
|
|
125
|
+
DistanceGrabbable,
|
|
126
|
+
} from '@iwsdk/core';
|
|
127
|
+
|
|
128
|
+
// Physics components
|
|
129
|
+
entity.addComponent(PhysicsShape, { shape: PhysicsShapeType.Auto });
|
|
130
|
+
entity.addComponent(PhysicsBody, { state: PhysicsState.Dynamic });
|
|
131
|
+
|
|
132
|
+
// Grab components
|
|
133
|
+
entity.addComponent(RayInteractable);
|
|
134
|
+
entity.addComponent(OneHandGrabbable);
|
|
135
|
+
|
|
136
|
+
// Optional: allow grabbing from a distance
|
|
137
|
+
entity.addComponent(DistanceGrabbable, {
|
|
138
|
+
rotate: true,
|
|
139
|
+
translate: true,
|
|
140
|
+
});
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
When grabbed, the `GrabSystem` drives the held body kinematically (via `HP_Body_SetTargetQTransform`), making the object follow the hand. On release, the object resumes dynamic simulation with natural velocity for realistic throwing. Note: near-field grabs do not add `Hovered`/`Pressed` tags — assert grab behavior with ECS snapshot/diff, not tag queries.
|
|
144
|
+
|
|
145
|
+
### Reading Velocity for Game Logic
|
|
146
|
+
|
|
147
|
+
```typescript
|
|
148
|
+
const velocity = entity.getVectorView(PhysicsBody, '_linearVelocity');
|
|
149
|
+
const speed = Math.sqrt(velocity[0] ** 2 + velocity[1] ** 2 + velocity[2] ** 2);
|
|
150
|
+
|
|
151
|
+
if (speed > 5.0) {
|
|
152
|
+
// High-speed impact logic
|
|
153
|
+
}
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Explosion Pattern (Radial Force)
|
|
157
|
+
|
|
158
|
+
Apply outward force to all nearby physics objects:
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
const explosionPos = bomb.object3D.position;
|
|
162
|
+
const radius = 5.0;
|
|
163
|
+
const force = 50.0;
|
|
164
|
+
|
|
165
|
+
for (const target of this.queries.physicsObjects.entities) {
|
|
166
|
+
const dist = target.object3D.position.distanceTo(explosionPos);
|
|
167
|
+
if (dist < radius && dist > 0) {
|
|
168
|
+
const direction = target.object3D.position
|
|
169
|
+
.clone()
|
|
170
|
+
.sub(explosionPos)
|
|
171
|
+
.normalize();
|
|
172
|
+
const strength = force * (1 - dist / radius);
|
|
173
|
+
target.addComponent(PhysicsManipulation, {
|
|
174
|
+
force: direction.multiplyScalar(strength).toArray(),
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## Custom Physics System Pattern
|
|
181
|
+
|
|
182
|
+
Create domain-specific components that interact with the physics system:
|
|
183
|
+
|
|
184
|
+
```typescript
|
|
185
|
+
import {
|
|
186
|
+
createComponent,
|
|
187
|
+
createSystem,
|
|
188
|
+
Types,
|
|
189
|
+
PhysicsBody,
|
|
190
|
+
PhysicsManipulation,
|
|
191
|
+
} from '@iwsdk/core';
|
|
192
|
+
|
|
193
|
+
// 1. Define custom component
|
|
194
|
+
export const Buoyancy = createComponent('Buoyancy', {
|
|
195
|
+
waterLevel: { type: Types.Float32, default: 0.0 },
|
|
196
|
+
buoyancyForce: { type: Types.Float32, default: 15.0 },
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
// 2. Create system that applies physics forces
|
|
200
|
+
export class BuoyancySystem extends createSystem({
|
|
201
|
+
floaters: { required: [Buoyancy, PhysicsBody] },
|
|
202
|
+
}) {
|
|
203
|
+
update(delta) {
|
|
204
|
+
for (const entity of this.queries.floaters.entities) {
|
|
205
|
+
const waterLevel = entity.getValue(Buoyancy, 'waterLevel');
|
|
206
|
+
const force = entity.getValue(Buoyancy, 'buoyancyForce');
|
|
207
|
+
const y = entity.object3D.position.y;
|
|
208
|
+
|
|
209
|
+
if (y < waterLevel) {
|
|
210
|
+
const submersion = Math.min(1, (waterLevel - y) / 0.5);
|
|
211
|
+
if (!entity.hasComponent(PhysicsManipulation)) {
|
|
212
|
+
entity.addComponent(PhysicsManipulation, {
|
|
213
|
+
force: [0, force * submersion * delta, 0],
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// 3. Register with world
|
|
222
|
+
world.registerComponent(Buoyancy);
|
|
223
|
+
world.registerSystem(BuoyancySystem, { priority: 5 });
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
## Complete Example: Physics Playground
|
|
228
|
+
|
|
229
|
+
```typescript
|
|
230
|
+
import {
|
|
231
|
+
World,
|
|
232
|
+
PhysicsShape,
|
|
233
|
+
PhysicsShapeType,
|
|
234
|
+
PhysicsBody,
|
|
235
|
+
PhysicsState,
|
|
236
|
+
PhysicsManipulation,
|
|
237
|
+
RayInteractable,
|
|
238
|
+
OneHandGrabbable,
|
|
239
|
+
} from '@iwsdk/core';
|
|
240
|
+
import projectOptions from 'virtual:iwsdk-project';
|
|
241
|
+
import {
|
|
242
|
+
Mesh,
|
|
243
|
+
BoxGeometry,
|
|
244
|
+
SphereGeometry,
|
|
245
|
+
MeshStandardMaterial,
|
|
246
|
+
Color,
|
|
247
|
+
FrontSide,
|
|
248
|
+
} from 'three';
|
|
249
|
+
|
|
250
|
+
World.create(
|
|
251
|
+
document.getElementById('scene-container'),
|
|
252
|
+
projectOptions,
|
|
253
|
+
).then((world) => {
|
|
254
|
+
// Static floor
|
|
255
|
+
const floor = new Mesh(
|
|
256
|
+
new BoxGeometry(10, 0.1, 10),
|
|
257
|
+
new MeshStandardMaterial({ color: 0x555555 }),
|
|
258
|
+
);
|
|
259
|
+
floor.position.set(0, -0.05, 0);
|
|
260
|
+
const floorEntity = world.createTransformEntity(floor);
|
|
261
|
+
floorEntity.addComponent(PhysicsShape, {
|
|
262
|
+
shape: PhysicsShapeType.Box,
|
|
263
|
+
dimensions: [10, 0.1, 10],
|
|
264
|
+
friction: 0.8,
|
|
265
|
+
});
|
|
266
|
+
floorEntity.addComponent(PhysicsBody, { state: PhysicsState.Static });
|
|
267
|
+
|
|
268
|
+
// Dynamic bouncy ball (grabbable)
|
|
269
|
+
const ball = new Mesh(
|
|
270
|
+
new SphereGeometry(0.15),
|
|
271
|
+
new MeshStandardMaterial({ color: new Color(0xff4444), side: FrontSide }),
|
|
272
|
+
);
|
|
273
|
+
ball.position.set(0, 1.5, -1);
|
|
274
|
+
const ballEntity = world.createTransformEntity(ball);
|
|
275
|
+
ballEntity.addComponent(PhysicsShape, {
|
|
276
|
+
shape: PhysicsShapeType.Sphere,
|
|
277
|
+
dimensions: [0.15, 0, 0],
|
|
278
|
+
restitution: 0.8,
|
|
279
|
+
friction: 0.5,
|
|
280
|
+
});
|
|
281
|
+
ballEntity.addComponent(PhysicsBody, { state: PhysicsState.Dynamic });
|
|
282
|
+
ballEntity.addComponent(RayInteractable);
|
|
283
|
+
ballEntity.addComponent(OneHandGrabbable);
|
|
284
|
+
|
|
285
|
+
// Dynamic box with initial impulse
|
|
286
|
+
const box = new Mesh(
|
|
287
|
+
new BoxGeometry(0.3, 0.3, 0.3),
|
|
288
|
+
new MeshStandardMaterial({ color: new Color(0x4488ff), side: FrontSide }),
|
|
289
|
+
);
|
|
290
|
+
box.position.set(0.5, 2, -1);
|
|
291
|
+
const boxEntity = world.createTransformEntity(box);
|
|
292
|
+
boxEntity.addComponent(PhysicsShape, {
|
|
293
|
+
shape: PhysicsShapeType.Box,
|
|
294
|
+
dimensions: [0.3, 0.3, 0.3],
|
|
295
|
+
restitution: 0.3,
|
|
296
|
+
});
|
|
297
|
+
boxEntity.addComponent(PhysicsBody, {
|
|
298
|
+
state: PhysicsState.Dynamic,
|
|
299
|
+
linearDamping: 0.1,
|
|
300
|
+
});
|
|
301
|
+
boxEntity.addComponent(PhysicsManipulation, { force: [-3, 5, 0] });
|
|
302
|
+
});
|
|
303
|
+
```
|