@threlte/rapier 0.4.0 → 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 +6 -0
- package/dist/components/Attractor/Attractor.svelte +50 -0
- package/dist/components/Attractor/Attractor.svelte.d.ts +23 -0
- package/dist/components/Debug/Debug.svelte +4 -2
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -0
- package/dist/lib/createRapierContext.d.ts +1 -0
- package/dist/lib/createRapierContext.js +3 -1
- package/dist/types/components.d.ts +28 -0
- package/package.json +1 -1
package/dist/CHANGELOG.md
CHANGED
|
@@ -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
|
|
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
|
|
@@ -144,3 +145,30 @@ export declare type CollisionGroupsProperties = {
|
|
|
144
145
|
filter: CollisionGroupsBitMask;
|
|
145
146
|
memberships: CollisionGroupsBitMask;
|
|
146
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
|
+
};
|