@threlte/rapier 3.1.4 → 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.
Files changed (51) hide show
  1. package/README.md +0 -4
  2. package/dist/components/Attractor/Attractor.svelte +58 -32
  3. package/dist/components/Attractor/Attractor.svelte.d.ts +2 -1
  4. package/dist/components/Attractor/types.d.ts +1 -1
  5. package/dist/components/Colliders/AutoColliders/AutoColliders.svelte +123 -76
  6. package/dist/components/Colliders/AutoColliders/AutoColliders.svelte.d.ts +15 -4
  7. package/dist/components/Colliders/AutoColliders/types.d.ts +2 -2
  8. package/dist/components/Colliders/AutoColliders/types.js +1 -1
  9. package/dist/components/Colliders/Collider/Collider.svelte +181 -126
  10. package/dist/components/Colliders/Collider/Collider.svelte.d.ts +13 -4
  11. package/dist/components/Colliders/Collider/types.d.ts +2 -2
  12. package/dist/components/Colliders/Collider/types.js +1 -1
  13. package/dist/components/CollisionGroups/CollisionGroups.svelte +27 -7
  14. package/dist/components/CollisionGroups/CollisionGroups.svelte.d.ts +11 -4
  15. package/dist/components/CollisionGroups/types.d.ts +1 -1
  16. package/dist/components/CollisionGroups/types.js +1 -1
  17. package/dist/components/Debug/Debug.svelte +28 -18
  18. package/dist/components/Debug/Debug.svelte.d.ts +2 -1
  19. package/dist/components/RigidBody/RigidBody.svelte +173 -113
  20. package/dist/components/RigidBody/RigidBody.svelte.d.ts +2 -1
  21. package/dist/components/RigidBody/types.d.ts +4 -4
  22. package/dist/components/RigidBody/types.js +1 -2
  23. package/dist/components/World/InnerWorld.svelte +53 -22
  24. package/dist/components/World/InnerWorld.svelte.d.ts +2 -1
  25. package/dist/components/World/World.svelte +6 -3
  26. package/dist/components/World/World.svelte.d.ts +2 -1
  27. package/dist/components/World/types.d.ts +2 -2
  28. package/dist/components/World/types.js +1 -1
  29. package/dist/hooks/useFixedJoint.js +2 -2
  30. package/dist/hooks/useJoint.d.ts +1 -1
  31. package/dist/hooks/useJoint.js +1 -1
  32. package/dist/hooks/usePhysicsTask.js +1 -1
  33. package/dist/hooks/usePrismaticJoint.js +2 -2
  34. package/dist/hooks/useRapier.d.ts +1 -1
  35. package/dist/hooks/useRevoluteJoint.js +2 -2
  36. package/dist/hooks/useRigidBody.d.ts +1 -1
  37. package/dist/hooks/useRopeJoint.js +2 -2
  38. package/dist/hooks/useSphericalJoint.js +2 -2
  39. package/dist/index.d.ts +12 -12
  40. package/dist/index.js +11 -11
  41. package/dist/lib/applyColliderActiveEvents.d.ts +1 -1
  42. package/dist/lib/createCollidersFromChildren.d.ts +1 -1
  43. package/dist/lib/createPhysicsStages.d.ts +1 -1
  44. package/dist/lib/createPhysicsStages.js +1 -1
  45. package/dist/lib/createPhysicsTasks.d.ts +1 -1
  46. package/dist/lib/createPhysicsTasks.js +1 -1
  47. package/dist/lib/createRapierContext.d.ts +1 -1
  48. package/dist/lib/createRapierContext.js +2 -2
  49. package/dist/lib/scaleColliderArgs.d.ts +1 -1
  50. package/dist/lib/useCreateEvent.d.ts +1 -1
  51. package/package.json +11 -12
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
- [![Powered by Vercel](./assets/vercel/powered-by-vercel.svg)](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">import { T, useTask } from '@threlte/core';
2
- import { Group, Vector3 } from 'three';
3
- import { useRapier } from '../../hooks/useRapier';
4
- let { strength = 1, range = 50, gravityType = 'static', gravitationalConstant = 6.673e-11, ref = $bindable(), children, ...props } = $props();
5
- const { world, debug } = useRapier();
6
- const gravitySource = new Vector3();
7
- const group = new Group();
8
- const calcForceByType = {
9
- static: (s, _m2, _r, _d, _G) => s,
10
- linear: (s, _m2, r, d, _G) => s * (d / r),
11
- newtonian: (s, m2, _r, d, G) => (G * s * m2) / Math.pow(d, 2)
12
- };
13
- const impulseVector = new Vector3();
14
- const bodyV3 = new Vector3();
15
- function applyImpulseToBodiesInRange() {
16
- group.getWorldPosition(gravitySource);
17
- world.forEachRigidBody((body) => {
18
- const { x, y, z } = body.translation();
19
- bodyV3.set(x, y, z);
20
- const distance = gravitySource.distanceToSquared(bodyV3);
21
- if (distance < range ** 2) {
22
- let force = calcForceByType[gravityType](strength, body.mass(), range, distance, gravitationalConstant);
23
- // Prevent wild forces when Attractors collide
24
- force = force === Infinity ? strength : force;
25
- impulseVector.subVectors(gravitySource, bodyV3).normalize().multiplyScalar(force);
26
- body.applyImpulse(impulseVector, true);
27
- }
28
- });
29
- }
30
- useTask(() => {
31
- applyImpulseToBodiesInRange();
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,3 +1,4 @@
1
- import type { AttractorProps } from './types';
1
+ import type { AttractorProps } from './types.js';
2
2
  declare const Attractor: import("svelte").Component<AttractorProps, {}, "ref">;
3
+ type Attractor = ReturnType<typeof Attractor>;
3
4
  export default Attractor;
@@ -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
- >import { ActiveCollisionTypes, CoefficientCombineRule } from '@dimforge/rapier3d-compat';
5
- import { createParentObject3DContext, useParentObject3D, watch } from '@threlte/core';
6
- import { onDestroy, onMount } from 'svelte';
7
- import { Group } from 'three';
8
- import { useCollisionGroups } from '../../../hooks/useCollisionGroups';
9
- import { useRapier } from '../../../hooks/useRapier';
10
- import { useRigidBody } from '../../../hooks/useRigidBody';
11
- import { applyColliderActiveEvents } from '../../../lib/applyColliderActiveEvents';
12
- import { createCollidersFromChildren } from '../../../lib/createCollidersFromChildren';
13
- import { eulerToQuaternion } from '../../../lib/eulerToQuaternion';
14
- import { useParentRigidbodyObject } from '../../../lib/rigidBodyObjectContext';
15
- import { useCreateEvent } from '../../../lib/useCreateEvent';
16
- let { shape = 'convexHull', restitution, restitutionCombineRule, friction, frictionCombineRule, sensor, contactForceEventThreshold, density, mass, centerOfMass, principalAngularInertia, angularInertiaLocalFrame, colliders = $bindable(), oncreate, oncollisionenter, oncollisionexit, oncontact, onsensorenter, onsensorexit, children } = $props();
17
- const group = new Group();
18
- const { updateRef } = useCreateEvent(oncreate);
19
- const rigidBody = useRigidBody();
20
- const rigidBodyParentObject = useParentRigidbodyObject();
21
- const { world, addColliderToContext, removeColliderFromContext } = useRapier();
22
- const collisionGroups = useCollisionGroups();
23
- const cleanup = () => {
24
- if (colliders === undefined)
25
- return;
26
- collisionGroups.removeColliders(colliders);
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
- removeColliderFromContext(c);
29
- world.removeCollider(c, true);
30
- });
31
- colliders.length = 0;
32
- };
33
- const events = {
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
- const create = () => {
41
- cleanup();
42
- colliders = createCollidersFromChildren(group, shape ?? 'convexHull', world, rigidBody, rigidBodyParentObject);
43
- colliders.forEach((c) => addColliderToContext(c, group, events));
44
- collisionGroups.registerColliders(colliders);
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
- applyColliderActiveEvents(collider, events, rigidBody?.userData?.events);
47
- collider.setActiveCollisionTypes(ActiveCollisionTypes.ALL);
48
- collider.setRestitution(restitution ?? 0);
49
- collider.setRestitutionCombineRule(restitutionCombineRule ?? CoefficientCombineRule.Average);
50
- collider.setFriction(friction ?? 0.7);
51
- collider.setFrictionCombineRule(frictionCombineRule ?? CoefficientCombineRule.Average);
52
- collider.setSensor(sensor ?? false);
53
- collider.setContactForceEventThreshold(contactForceEventThreshold ?? 0);
54
- if (density)
55
- collider.setDensity(density);
56
- if (mass) {
57
- if (centerOfMass && principalAngularInertia && angularInertiaLocalFrame)
58
- collider.setMassProperties(mass, { x: centerOfMass[0], y: centerOfMass[1], z: centerOfMass[2] }, {
59
- x: principalAngularInertia[0],
60
- y: principalAngularInertia[1],
61
- z: principalAngularInertia[2]
62
- }, eulerToQuaternion(angularInertiaLocalFrame));
63
- else
64
- collider.setMass(mass);
65
- }
66
- });
67
- updateRef(colliders);
68
- };
69
- /**
70
- * Refresh the colliders.
71
- */
72
- export const refresh = () => create();
73
- onMount(() => {
74
- create();
75
- });
76
- /**
77
- * Cleanup
78
- */
79
- onDestroy(cleanup);
80
- const parent3DObject = useParentObject3D();
81
- createParentObject3DContext(group);
82
- watch(parent3DObject, (parent) => {
83
- parent?.add(group);
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
- parent?.remove(group);
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(): AutoCollidersProps<TMassDef>;
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
- import { type Snippet } from 'svelte';
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.
@@ -1 +1 @@
1
- import {} from 'svelte';
1
+ export {};