@threlte/rapier 0.3.2 → 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/dist/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @threlte/extras
2
2
 
3
+ ## 0.5.0
4
+
5
+ ### Minor Changes
6
+
7
+ - d69391a: Add Attractor component and documentation
8
+
9
+ ## 0.4.0
10
+
11
+ ### Minor Changes
12
+
13
+ - 4b4d7be: Added rapiers damping properties to the component <RigidBody>
14
+
3
15
  ## 0.3.2
4
16
 
5
17
  ### Patch Changes
@@ -0,0 +1,50 @@
1
+ <script>import { Mesh, useFrame, Object3DInstance } from '@threlte/core';
2
+ import { SphereBufferGeometry, MeshBasicMaterial, Vector3, Object3D } from 'three';
3
+ import { useRapier } from '../../hooks/useRapier';
4
+ export let position = undefined;
5
+ export let strength = 1;
6
+ export let range = 50;
7
+ export let gravityType = 'static';
8
+ export let gravitationalConstant = 6.673e-11;
9
+ const { world, debug } = useRapier();
10
+ const gravitySource = new Vector3();
11
+ let obj = new Object3D();
12
+ const calcForceByType = {
13
+ static: (s, m2, r, d, G) => s,
14
+ linear: (s, m2, r, d, G) => s * (d / r),
15
+ newtonian: (s, m2, r, d, G) => (G * s * m2) / Math.pow(d, 2)
16
+ };
17
+ function applyImpulseToBodiesInRange() {
18
+ const impulseVector = new Vector3();
19
+ obj.getWorldPosition(gravitySource);
20
+ world.forEachRigidBody((body) => {
21
+ const { x, y, z } = body.translation();
22
+ const bodyV3 = new Vector3(x, y, z);
23
+ const distance = gravitySource.distanceTo(bodyV3);
24
+ if (distance < range) {
25
+ let force = calcForceByType[gravityType](strength, body.mass(), range, distance, gravitationalConstant);
26
+ // Prevent wild forces when Attractors collide
27
+ force = force === Infinity ? strength : force;
28
+ impulseVector.subVectors(gravitySource, bodyV3).normalize().multiplyScalar(force);
29
+ body.applyImpulse(impulseVector, true);
30
+ }
31
+ });
32
+ }
33
+ useFrame(() => {
34
+ applyImpulseToBodiesInRange();
35
+ });
36
+ </script>
37
+
38
+ <Object3DInstance
39
+ bind:object={obj}
40
+ {position}
41
+ >
42
+ <slot />
43
+
44
+ {#if $debug}
45
+ <Mesh
46
+ geometry={new SphereBufferGeometry(range)}
47
+ material={new MeshBasicMaterial({ wireframe: true, transparent: true, opacity: 0.25 })}
48
+ />
49
+ {/if}
50
+ </Object3DInstance>
@@ -0,0 +1,23 @@
1
+ import { SvelteComponentTyped } from "svelte";
2
+ import type { AttractorProperties } from '../../types/components';
3
+ declare const __propDef: {
4
+ props: {
5
+ position?: AttractorProperties['position'];
6
+ strength?: number | undefined;
7
+ range?: number | undefined;
8
+ gravityType?: import("../../types/components").GravityType | undefined;
9
+ gravitationalConstant?: number | undefined;
10
+ };
11
+ events: {
12
+ [evt: string]: CustomEvent<any>;
13
+ };
14
+ slots: {
15
+ default: {};
16
+ };
17
+ };
18
+ export declare type AttractorProps = typeof __propDef.props;
19
+ export declare type AttractorEvents = typeof __propDef.events;
20
+ export declare type AttractorSlots = typeof __propDef.slots;
21
+ export default class Attractor extends SvelteComponentTyped<AttractorProps, AttractorEvents, AttractorSlots> {
22
+ }
23
+ export {};
@@ -1,5 +1,5 @@
1
1
  <script>import { LineSegments, useFrame } from '@threlte/core';
2
- import { onDestroy } from 'svelte';
2
+ import { onDestroy, } from 'svelte';
3
3
  import { BufferAttribute, BufferGeometry, LineBasicMaterial } from 'three';
4
4
  import { useRapier } from '../../hooks/useRapier';
5
5
  export let vertexColors = true;
@@ -7,13 +7,14 @@ const material = new LineBasicMaterial({
7
7
  vertexColors,
8
8
  ...$$props
9
9
  });
10
- const { world } = useRapier();
10
+ const { world, debug } = useRapier();
11
11
  const buffers = world.debugRender();
12
12
  const vertices = new BufferAttribute(buffers.vertices, 3);
13
13
  const colors = new BufferAttribute(buffers.colors, 4);
14
14
  const geometry = new BufferGeometry();
15
15
  geometry.setAttribute('position', vertices);
16
16
  geometry.setAttribute('color', colors);
17
+ debug.set(true);
17
18
  useFrame(() => {
18
19
  const buffers = world.debugRender();
19
20
  const vertices = new BufferAttribute(buffers.vertices, 3);
@@ -24,6 +25,7 @@ useFrame(() => {
24
25
  onDestroy(() => {
25
26
  geometry.dispose();
26
27
  material.dispose();
28
+ debug.set(false);
27
29
  });
28
30
  </script>
29
31
 
@@ -16,10 +16,8 @@ export let linearVelocity = {};
16
16
  export let angularVelocity = {};
17
17
  export let gravityScale = 1;
18
18
  export let ccd = false;
19
- export let position = undefined;
20
- export let rotation = undefined;
21
- export let scale = undefined;
22
- export let lookAt = undefined;
19
+ export let angularDamping = 0;
20
+ export let linearDamping = 0;
23
21
  export let lockRotations = false;
24
22
  export let lockTranslations = false;
25
23
  export let enabledRotations = [
@@ -33,6 +31,10 @@ export let enabledTranslations = [
33
31
  true
34
32
  ];
35
33
  export let dominance = 0;
34
+ export let position = undefined;
35
+ export let rotation = undefined;
36
+ export let scale = undefined;
37
+ export let lookAt = undefined;
36
38
  const dispatcher = createEventDispatcher();
37
39
  const object = new Object3D();
38
40
  /**
@@ -107,6 +109,8 @@ $: {
107
109
  rigidBodyTemp.lockTranslations(lockTranslations, true);
108
110
  rigidBodyTemp.setEnabledRotations(...enabledRotations, true);
109
111
  rigidBodyTemp.setEnabledTranslations(...enabledTranslations, true);
112
+ rigidBodyTemp.setAngularDamping(angularDamping);
113
+ rigidBodyTemp.setLinearDamping(linearDamping);
110
114
  }
111
115
  /**
112
116
  * Add userData to the rigidBody
@@ -10,15 +10,17 @@ declare const __propDef: {
10
10
  angularVelocity?: import("@threlte/core").Rotation | undefined;
11
11
  gravityScale?: number | undefined;
12
12
  ccd?: boolean | undefined;
13
- position?: RigidBodyProperties['position'];
14
- rotation?: RigidBodyProperties['rotation'];
15
- scale?: RigidBodyProperties['scale'];
16
- lookAt?: RigidBodyProperties['lookAt'];
13
+ angularDamping?: number | undefined;
14
+ linearDamping?: number | undefined;
17
15
  lockRotations?: boolean | undefined;
18
16
  lockTranslations?: boolean | undefined;
19
17
  enabledRotations?: import("../../types/components").Boolean3Array | undefined;
20
18
  enabledTranslations?: import("../../types/components").Boolean3Array | undefined;
21
19
  dominance?: number | undefined;
20
+ position?: RigidBodyProperties['position'];
21
+ rotation?: RigidBodyProperties['rotation'];
22
+ scale?: RigidBodyProperties['scale'];
23
+ lookAt?: RigidBodyProperties['lookAt'];
22
24
  /**
23
25
  * Export the rigidBody only after positional initialization
24
26
  */ rigidBody?: RigidBody | undefined;
package/dist/index.d.ts CHANGED
@@ -12,6 +12,7 @@ export { default as Debug } from './components/Debug/Debug.svelte';
12
12
  export { default as Collider } from './components/Colliders/Collider.svelte';
13
13
  export { default as AutoColliders } from './components/Colliders/AutoColliders.svelte';
14
14
  export { default as CollisionGroups } from './components/CollisionGroups/CollisionGroups.svelte';
15
+ export { default as Attractor } from './components/Attractor/Attractor.svelte';
15
16
  export { default as BasicPlayerController } from './recipes/BasicPlayerController.svelte';
16
- export type { AutoCollidersProperties, Boolean3Array, RigidBodyProperties, WorldProperties } from './types/components';
17
+ export type { AutoCollidersProperties, Boolean3Array, RigidBodyProperties, WorldProperties, AttractorProperties } from './types/components';
17
18
  export type { CollisionGroupsBitMask, AutoCollidersShapes, ColliderEventDispatcher, ColliderShapes, RapierContext, RigidBodyEventDispatcher, CollisionEnterEvent, CollisionExitEvent, SensorEnterEvent, SensorExitEvent, ContactEvent } from './types/types';
package/dist/index.js CHANGED
@@ -15,5 +15,6 @@ export { default as Debug } from './components/Debug/Debug.svelte';
15
15
  export { default as Collider } from './components/Colliders/Collider.svelte';
16
16
  export { default as AutoColliders } from './components/Colliders/AutoColliders.svelte';
17
17
  export { default as CollisionGroups } from './components/CollisionGroups/CollisionGroups.svelte';
18
+ export { default as Attractor } from './components/Attractor/Attractor.svelte';
18
19
  // recipes
19
20
  export { default as BasicPlayerController } from './recipes/BasicPlayerController.svelte';
@@ -13,4 +13,5 @@ export declare const createRapierContext: (gravity: RAPIER.Vector, rawIntegratio
13
13
  removeColliderFromContext: (collider: Collider) => void;
14
14
  addRigidBodyToContext: (rigidBody: RigidBody, object: Object3D, eventDispatcher: RigidBodyEventDispatcher) => void;
15
15
  removeRigidBodyFromContext: (rigidBody: RigidBody) => void;
16
+ debug: import("svelte/store").Writable<boolean>;
16
17
  };
@@ -1,4 +1,5 @@
1
1
  import RAPIER from '@dimforge/rapier3d-compat';
2
+ import { writable } from 'svelte/store';
2
3
  export const createRapierContext = (...args) => {
3
4
  const world = new RAPIER.World(...args);
4
5
  const colliderObjects = new Map();
@@ -51,6 +52,7 @@ export const createRapierContext = (...args) => {
51
52
  addColliderToContext,
52
53
  removeColliderFromContext,
53
54
  addRigidBodyToContext,
54
- removeRigidBodyFromContext
55
+ removeRigidBodyFromContext,
56
+ debug: writable(false),
55
57
  };
56
58
  };
@@ -5,6 +5,7 @@ import type { RigidBodyTypeString } from '../lib/parseRigidBodyType';
5
5
  import type { AutoCollidersShapes, ColliderShapes, CollisionGroupsBitMask } from './types';
6
6
  export declare type Boolean3Array = [x: boolean, y: boolean, z: boolean];
7
7
  export declare type Vector3Array = [x: number, y: number, z: number];
8
+ export declare type GravityType = 'static' | 'linear' | 'newtonian';
8
9
  export declare type RigidBodyProperties = Omit<TransformableObjectProperties, 'object'> & {
9
10
  /**
10
11
  * Specify the type of this rigid body
@@ -57,6 +58,20 @@ export declare type RigidBodyProperties = Omit<TransformableObjectProperties, 'o
57
58
  * Number in the range -127 to 127, default is 0
58
59
  */
59
60
  dominance?: number;
61
+ /**
62
+ * Damping lets you slow down a rigid-body automatically. This can be used to
63
+ * achieve a wide variety of effects like fake air friction. Larger values of
64
+ * damping coefficients lead to a stronger slow-downs. Their default
65
+ * values are 0.0 (no damping at all).
66
+ */
67
+ linearDamping?: number;
68
+ /**
69
+ * Damping lets you slow down a rigid-body automatically. This can be used to
70
+ * achieve a wide variety of effects like fake air friction. Larger values of
71
+ * damping coefficients lead to a stronger slow-downs. Their default
72
+ * values are 0.0 (no damping at all).
73
+ */
74
+ angularDamping?: number;
60
75
  };
61
76
  export declare type ColliderProperties<Shape extends ColliderShapes> = Omit<TransformableObjectProperties, 'object'> & {
62
77
  shape: Shape;
@@ -130,3 +145,30 @@ export declare type CollisionGroupsProperties = {
130
145
  filter: CollisionGroupsBitMask;
131
146
  memberships: CollisionGroupsBitMask;
132
147
  };
148
+ export declare type AttractorProperties = Omit<TransformableObjectProperties, 'object'> & {
149
+ /**
150
+ * The radius for the Attractor's sphere of influence within which rigid-bodies will be affected.
151
+ * Default: 10.0
152
+ */
153
+ range?: number;
154
+ /**
155
+ * The strength factor applied to the impulse affecting rigid-bodies within range. For newtonian
156
+ * calculations, strength is treated as m1 mass.
157
+ * Default: 1.0
158
+ */
159
+ strength?: number;
160
+ /**
161
+ * The method of calculating gravity on rigid bodies within range.
162
+ * 'static' = the same force (strength) is applied on bodies within range, regardless of distance
163
+ * 'linear' = force is calculated as strength * distance / range (force decreases the farther a body is from the attractor position)
164
+ * 'newtonian' = force is calculated as gravitationalConstant * mass1 * mass2 / Math.pow(distance, 2)
165
+ * Default: 'static'
166
+ */
167
+ gravityType?: GravityType;
168
+ /**
169
+ * The gravitational constant used to calculate force in newtonian calculations. Most people probably won't use this,
170
+ * but it provides an option for more realistic physics simulations.
171
+ * Default: 6.673e-11
172
+ */
173
+ gravitationalConstant?: number;
174
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@threlte/rapier",
3
- "version": "0.3.2",
3
+ "version": "0.5.0",
4
4
  "author": "Grischa Erbe <hello@legrisch.com> (https://legrisch.com)",
5
5
  "license": "MIT",
6
6
  "devDependencies": {