@threlte/rapier 3.1.5 → 3.2.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 +0 -4
- package/dist/components/Attractor/Attractor.svelte +58 -32
- package/dist/components/Attractor/Attractor.svelte.d.ts +2 -1
- package/dist/components/Attractor/types.d.ts +1 -1
- package/dist/components/Colliders/AutoColliders/AutoColliders.svelte +123 -76
- package/dist/components/Colliders/AutoColliders/AutoColliders.svelte.d.ts +15 -4
- package/dist/components/Colliders/AutoColliders/types.d.ts +1 -1
- package/dist/components/Colliders/Collider/Collider.svelte +181 -126
- package/dist/components/Colliders/Collider/Collider.svelte.d.ts +13 -4
- package/dist/components/Colliders/Collider/types.d.ts +1 -1
- package/dist/components/CollisionGroups/CollisionGroups.svelte +27 -7
- package/dist/components/CollisionGroups/CollisionGroups.svelte.d.ts +11 -4
- package/dist/components/Debug/Debug.svelte +28 -18
- package/dist/components/Debug/Debug.svelte.d.ts +2 -1
- package/dist/components/RigidBody/RigidBody.svelte +173 -113
- package/dist/components/RigidBody/RigidBody.svelte.d.ts +2 -1
- package/dist/components/RigidBody/types.d.ts +2 -2
- package/dist/components/World/InnerWorld.svelte +53 -22
- package/dist/components/World/InnerWorld.svelte.d.ts +2 -1
- package/dist/components/World/World.svelte +6 -3
- package/dist/components/World/World.svelte.d.ts +2 -1
- package/dist/components/World/types.d.ts +1 -1
- package/dist/hooks/useFixedJoint.js +2 -2
- package/dist/hooks/useJoint.d.ts +1 -1
- package/dist/hooks/useJoint.js +1 -1
- package/dist/hooks/usePhysicsTask.js +1 -1
- package/dist/hooks/usePrismaticJoint.js +2 -2
- package/dist/hooks/useRapier.d.ts +1 -1
- package/dist/hooks/useRevoluteJoint.js +2 -2
- package/dist/hooks/useRigidBody.d.ts +1 -1
- package/dist/hooks/useRopeJoint.js +2 -2
- package/dist/hooks/useSphericalJoint.js +2 -2
- package/dist/index.d.ts +12 -12
- package/dist/index.js +11 -11
- package/dist/lib/applyColliderActiveEvents.d.ts +1 -1
- package/dist/lib/createCollidersFromChildren.d.ts +1 -1
- package/dist/lib/createPhysicsStages.d.ts +1 -1
- package/dist/lib/createPhysicsStages.js +1 -1
- package/dist/lib/createPhysicsTasks.d.ts +1 -1
- package/dist/lib/createPhysicsTasks.js +1 -1
- package/dist/lib/createRapierContext.d.ts +1 -1
- package/dist/lib/createRapierContext.js +2 -2
- package/dist/lib/scaleColliderArgs.d.ts +1 -1
- package/dist/lib/useCreateEvent.d.ts +1 -1
- package/package.json +10 -11
package/README.md
CHANGED
|
@@ -48,10 +48,6 @@ Contributions are what make the open source community such an amazing place to l
|
|
|
48
48
|
- **Filing Issues** - if you have feature requestions or you think you spotted a bug, [submit an issue](https://github.com/threlte/threlte/issues/new).
|
|
49
49
|
- **Contributing Code** - if you would like to drop us a PR, read the [contribution guide](https://github.com/threlte/threlte/blob/main/CONTRIBUTING.md) first.
|
|
50
50
|
|
|
51
|
-
## Sponsors
|
|
52
|
-
|
|
53
|
-
[](https://vercel.com/?utm_source=threlte&utm_campaign=oss)
|
|
54
|
-
|
|
55
51
|
---
|
|
56
52
|
|
|
57
53
|
### License
|
|
@@ -1,35 +1,61 @@
|
|
|
1
|
-
<script lang="ts">
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { RigidBody } from '@dimforge/rapier3d-compat'
|
|
3
|
+
import { T, useTask } from '@threlte/core'
|
|
4
|
+
import { Group, Vector3 } from 'three'
|
|
5
|
+
import { useRapier } from '../../hooks/useRapier.js'
|
|
6
|
+
import type { AttractorProps } from './types.js'
|
|
7
|
+
|
|
8
|
+
let {
|
|
9
|
+
strength = 1,
|
|
10
|
+
range = 50,
|
|
11
|
+
gravityType = 'static',
|
|
12
|
+
gravitationalConstant = 6.673e-11,
|
|
13
|
+
ref = $bindable(),
|
|
14
|
+
children,
|
|
15
|
+
...props
|
|
16
|
+
}: AttractorProps = $props()
|
|
17
|
+
|
|
18
|
+
const { world, debug } = useRapier()
|
|
19
|
+
const gravitySource = new Vector3()
|
|
20
|
+
const group = new Group()
|
|
21
|
+
|
|
22
|
+
const calcForceByType = {
|
|
23
|
+
static: (s: number, _m2: number, _r: number, _d: number, _G: number): number => s,
|
|
24
|
+
linear: (s: number, _m2: number, r: number, d: number, _G: number) => s * (d / r),
|
|
25
|
+
newtonian: (s: number, m2: number, _r: number, d: number, G: number) =>
|
|
26
|
+
(G * s * m2) / Math.pow(d, 2)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const impulseVector = new Vector3()
|
|
30
|
+
const bodyV3 = new Vector3()
|
|
31
|
+
|
|
32
|
+
function applyImpulseToBodiesInRange() {
|
|
33
|
+
group.getWorldPosition(gravitySource)
|
|
34
|
+
|
|
35
|
+
world.forEachRigidBody((body: RigidBody) => {
|
|
36
|
+
const { x, y, z } = body.translation()
|
|
37
|
+
bodyV3.set(x, y, z)
|
|
38
|
+
|
|
39
|
+
const distance = gravitySource.distanceToSquared(bodyV3)
|
|
40
|
+
if (distance < range ** 2) {
|
|
41
|
+
let force = calcForceByType[gravityType](
|
|
42
|
+
strength,
|
|
43
|
+
body.mass(),
|
|
44
|
+
range,
|
|
45
|
+
distance,
|
|
46
|
+
gravitationalConstant
|
|
47
|
+
)
|
|
48
|
+
// Prevent wild forces when Attractors collide
|
|
49
|
+
force = force === Infinity ? strength : force
|
|
50
|
+
impulseVector.subVectors(gravitySource, bodyV3).normalize().multiplyScalar(force)
|
|
51
|
+
body.applyImpulse(impulseVector, true)
|
|
52
|
+
}
|
|
53
|
+
})
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
useTask(() => {
|
|
57
|
+
applyImpulseToBodiesInRange()
|
|
58
|
+
})
|
|
33
59
|
</script>
|
|
34
60
|
|
|
35
61
|
<T
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Props } from '@threlte/core';
|
|
2
2
|
import type { Group } from 'three';
|
|
3
|
-
import type { GravityType } from '../../types/types';
|
|
3
|
+
import type { GravityType } from '../../types/types.js';
|
|
4
4
|
export type AttractorProps = Props<Group> & {
|
|
5
5
|
/**
|
|
6
6
|
* The strength factor applied to the impulse affecting rigid-bodies within range. For newtonian
|
|
@@ -1,90 +1,137 @@
|
|
|
1
1
|
<script
|
|
2
2
|
lang="ts"
|
|
3
3
|
generics="TMassDef extends MassDef"
|
|
4
|
-
>
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
4
|
+
>
|
|
5
|
+
import { ActiveCollisionTypes, CoefficientCombineRule } from '@dimforge/rapier3d-compat'
|
|
6
|
+
import { createParentObject3DContext, useParentObject3D, watch } from '@threlte/core'
|
|
7
|
+
import { onDestroy, onMount } from 'svelte'
|
|
8
|
+
import { Group } from 'three'
|
|
9
|
+
import { useCollisionGroups } from '../../../hooks/useCollisionGroups.js'
|
|
10
|
+
import { useRapier } from '../../../hooks/useRapier.js'
|
|
11
|
+
import { useRigidBody } from '../../../hooks/useRigidBody.js'
|
|
12
|
+
import { applyColliderActiveEvents } from '../../../lib/applyColliderActiveEvents.js'
|
|
13
|
+
import { createCollidersFromChildren } from '../../../lib/createCollidersFromChildren.js'
|
|
14
|
+
import { eulerToQuaternion } from '../../../lib/eulerToQuaternion.js'
|
|
15
|
+
import { useParentRigidbodyObject } from '../../../lib/rigidBodyObjectContext.js'
|
|
16
|
+
import { useCreateEvent } from '../../../lib/useCreateEvent.js'
|
|
17
|
+
import type { AutoCollidersProps, MassDef } from './types.js'
|
|
18
|
+
|
|
19
|
+
let {
|
|
20
|
+
shape = 'convexHull',
|
|
21
|
+
restitution,
|
|
22
|
+
restitutionCombineRule,
|
|
23
|
+
friction,
|
|
24
|
+
frictionCombineRule,
|
|
25
|
+
sensor,
|
|
26
|
+
contactForceEventThreshold,
|
|
27
|
+
density,
|
|
28
|
+
mass,
|
|
29
|
+
centerOfMass,
|
|
30
|
+
principalAngularInertia,
|
|
31
|
+
angularInertiaLocalFrame,
|
|
32
|
+
colliders = $bindable(),
|
|
33
|
+
oncreate,
|
|
34
|
+
oncollisionenter,
|
|
35
|
+
oncollisionexit,
|
|
36
|
+
oncontact,
|
|
37
|
+
onsensorenter,
|
|
38
|
+
onsensorexit,
|
|
39
|
+
children
|
|
40
|
+
}: AutoCollidersProps<TMassDef> = $props()
|
|
41
|
+
|
|
42
|
+
const group = new Group()
|
|
43
|
+
|
|
44
|
+
const { updateRef } = useCreateEvent(oncreate)
|
|
45
|
+
const rigidBody = useRigidBody()
|
|
46
|
+
const rigidBodyParentObject = useParentRigidbodyObject()
|
|
47
|
+
|
|
48
|
+
const { world, addColliderToContext, removeColliderFromContext } = useRapier()
|
|
49
|
+
|
|
50
|
+
const collisionGroups = useCollisionGroups()
|
|
51
|
+
|
|
52
|
+
const cleanup = () => {
|
|
53
|
+
if (colliders === undefined) return
|
|
54
|
+
|
|
55
|
+
collisionGroups.removeColliders(colliders)
|
|
27
56
|
colliders.forEach((c) => {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
})
|
|
31
|
-
colliders.length = 0
|
|
32
|
-
}
|
|
33
|
-
|
|
57
|
+
removeColliderFromContext(c)
|
|
58
|
+
world.removeCollider(c, true)
|
|
59
|
+
})
|
|
60
|
+
colliders.length = 0
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const events = {
|
|
34
64
|
oncollisionenter,
|
|
35
65
|
oncollisionexit,
|
|
36
66
|
oncontact,
|
|
37
67
|
onsensorenter,
|
|
38
68
|
onsensorexit
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
colliders
|
|
44
|
-
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const create = () => {
|
|
72
|
+
cleanup()
|
|
73
|
+
colliders = createCollidersFromChildren(
|
|
74
|
+
group,
|
|
75
|
+
shape ?? 'convexHull',
|
|
76
|
+
world,
|
|
77
|
+
rigidBody,
|
|
78
|
+
rigidBodyParentObject
|
|
79
|
+
)
|
|
80
|
+
colliders.forEach((c) => addColliderToContext(c, group, events))
|
|
81
|
+
|
|
82
|
+
collisionGroups.registerColliders(colliders)
|
|
83
|
+
|
|
45
84
|
colliders.forEach((collider) => {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
if (
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
85
|
+
applyColliderActiveEvents(collider, events, rigidBody?.userData?.events)
|
|
86
|
+
collider.setActiveCollisionTypes(ActiveCollisionTypes.ALL)
|
|
87
|
+
collider.setRestitution(restitution ?? 0)
|
|
88
|
+
collider.setRestitutionCombineRule(restitutionCombineRule ?? CoefficientCombineRule.Average)
|
|
89
|
+
collider.setFriction(friction ?? 0.7)
|
|
90
|
+
collider.setFrictionCombineRule(frictionCombineRule ?? CoefficientCombineRule.Average)
|
|
91
|
+
collider.setSensor(sensor ?? false)
|
|
92
|
+
collider.setContactForceEventThreshold(contactForceEventThreshold ?? 0)
|
|
93
|
+
if (density) collider.setDensity(density)
|
|
94
|
+
if (mass) {
|
|
95
|
+
if (centerOfMass && principalAngularInertia && angularInertiaLocalFrame)
|
|
96
|
+
collider.setMassProperties(
|
|
97
|
+
mass,
|
|
98
|
+
{ x: centerOfMass[0], y: centerOfMass[1], z: centerOfMass[2] },
|
|
99
|
+
{
|
|
100
|
+
x: principalAngularInertia[0],
|
|
101
|
+
y: principalAngularInertia[1],
|
|
102
|
+
z: principalAngularInertia[2]
|
|
103
|
+
},
|
|
104
|
+
eulerToQuaternion(angularInertiaLocalFrame)
|
|
105
|
+
)
|
|
106
|
+
else collider.setMass(mass)
|
|
107
|
+
}
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
updateRef(colliders)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Refresh the colliders.
|
|
115
|
+
*/
|
|
116
|
+
export const refresh = () => create()
|
|
117
|
+
|
|
118
|
+
onMount(() => {
|
|
119
|
+
create()
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Cleanup
|
|
124
|
+
*/
|
|
125
|
+
onDestroy(cleanup)
|
|
126
|
+
|
|
127
|
+
const parent3DObject = useParentObject3D()
|
|
128
|
+
createParentObject3DContext(group)
|
|
129
|
+
watch(parent3DObject, (parent) => {
|
|
130
|
+
parent?.add(group)
|
|
84
131
|
return () => {
|
|
85
|
-
|
|
86
|
-
}
|
|
87
|
-
})
|
|
132
|
+
parent?.remove(group)
|
|
133
|
+
}
|
|
134
|
+
})
|
|
88
135
|
</script>
|
|
89
136
|
|
|
90
137
|
{@render children?.({ colliders: colliders ?? [], refresh })}
|
|
@@ -1,8 +1,19 @@
|
|
|
1
|
-
import type { AutoCollidersProps, MassDef } from './types';
|
|
1
|
+
import type { AutoCollidersProps, MassDef } from './types.js';
|
|
2
|
+
declare function $$render<TMassDef extends MassDef>(): {
|
|
3
|
+
props: AutoCollidersProps<TMassDef>;
|
|
4
|
+
exports: {
|
|
5
|
+
/**
|
|
6
|
+
* Refresh the colliders.
|
|
7
|
+
*/ refresh: () => void;
|
|
8
|
+
};
|
|
9
|
+
bindings: "colliders";
|
|
10
|
+
slots: {};
|
|
11
|
+
events: {};
|
|
12
|
+
};
|
|
2
13
|
declare class __sveltets_Render<TMassDef extends MassDef> {
|
|
3
|
-
props():
|
|
4
|
-
events():
|
|
5
|
-
slots():
|
|
14
|
+
props(): ReturnType<typeof $$render<TMassDef>>['props'];
|
|
15
|
+
events(): ReturnType<typeof $$render<TMassDef>>['events'];
|
|
16
|
+
slots(): ReturnType<typeof $$render<TMassDef>>['slots'];
|
|
6
17
|
bindings(): "colliders";
|
|
7
18
|
exports(): {
|
|
8
19
|
/**
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { CoefficientCombineRule, Collider } from '@dimforge/rapier3d-compat';
|
|
2
2
|
import type { Snippet } from 'svelte';
|
|
3
3
|
import type { Euler, Vector3 } from 'three';
|
|
4
|
-
import type { AutoCollidersShapes, ColliderEvents, CreateEvent } from '../../../types/types';
|
|
4
|
+
import type { AutoCollidersShapes, ColliderEvents, CreateEvent } from '../../../types/types.js';
|
|
5
5
|
type BaseProps = {
|
|
6
6
|
/**
|
|
7
7
|
* The shape of the collider.
|