@threlte/rapier 0.0.1 → 0.0.2

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,11 @@
1
1
  # @threlte/extras
2
2
 
3
+ ## 0.0.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 62d2c5c: fixed `<RigidBody>` initial transforms
8
+
3
9
  ## 0.0.1
4
10
 
5
11
  ### Patch Changes
@@ -1,9 +1,9 @@
1
1
  <script>import { LayerableObject, SceneGraphObject } from '@threlte/core';
2
- import { createEventDispatcher, onDestroy, setContext } from 'svelte';
2
+ import { createEventDispatcher, onDestroy, setContext, tick } from 'svelte';
3
3
  import { Object3D, Vector3 } from 'three';
4
4
  import { useRapier } from '../../hooks/useRapier';
5
5
  import { applyTransforms } from '../../lib/applyTransforms';
6
- import { getWorldPosition, getWorldQuaternion } from '../../lib/getWorldTransforms';
6
+ import { getWorldPosition, getWorldQuaternion, getWorldScale } from '../../lib/getWorldTransforms';
7
7
  import { parseRigidBodyType } from '../../lib/parseRigidBodyType';
8
8
  import { positionToVector3 } from '../../lib/positionToVector3';
9
9
  import { rotationToEuler } from '../../lib/rotationToEuler';
@@ -59,14 +59,26 @@ const parentWorldScale = object.parent?.getWorldScale(new Vector3()) || new Vect
59
59
  /**
60
60
  * RigidBody Description
61
61
  */
62
- const desc = new rapier.RigidBodyDesc(parseRigidBodyType(type))
63
- .setCanSleep(canSleep)
64
- .setTranslation(worldPosition.x * parentWorldScale.x, worldPosition.y * parentWorldScale.y, worldPosition.z * parentWorldScale.z)
65
- .setRotation({ x: worldRotation.x, y: worldRotation.y, z: worldRotation.z, w: worldRotation.w });
62
+ const desc = new rapier.RigidBodyDesc(parseRigidBodyType(type)).setCanSleep(canSleep);
66
63
  /**
67
64
  * RigidBody init
68
65
  */
69
66
  export const rigidBody = world.createRigidBody(desc);
67
+ /**
68
+ * Apply transforms after the parent component added "object" to itself
69
+ */
70
+ const initPosition = async () => {
71
+ await tick();
72
+ applyTransforms(object, position, rotation, scale, lookAt);
73
+ object.updateMatrix();
74
+ object.updateWorldMatrix(true, false);
75
+ const parentWorldScale = object.parent ? getWorldScale(object.parent) : new Vector3(1, 1, 1);
76
+ const worldPosition = getWorldPosition(object).multiply(parentWorldScale);
77
+ const worldQuaternion = getWorldQuaternion(object);
78
+ rigidBody.setTranslation(worldPosition, true);
79
+ rigidBody.setRotation(worldQuaternion, true);
80
+ };
81
+ initPosition();
70
82
  /**
71
83
  * Will come in handy in the future for joints
72
84
  */
@@ -1,7 +1,8 @@
1
1
  import { Euler, Object3D, Quaternion, Vector3 } from 'three';
2
- const tempVector3 = new Vector3();
2
+ const tempPosition = new Vector3();
3
3
  const tempQuaternion = new Quaternion();
4
- const tempEuler = new Euler();
4
+ const tempRotation = new Euler();
5
+ const tempScale = new Vector3();
5
6
  /**
6
7
  * Get the world position of an object.
7
8
  * If no target is provided, a globally used
@@ -12,7 +13,7 @@ const tempEuler = new Euler();
12
13
  * @returns
13
14
  */
14
15
  export const getWorldPosition = (object, target) => {
15
- return object.getWorldPosition(target ?? tempVector3);
16
+ return object.getWorldPosition(target ?? tempPosition);
16
17
  };
17
18
  /**
18
19
  * Get the world quaternion of an object.
@@ -39,7 +40,7 @@ export const getWorldRotation = (object, target) => {
39
40
  object.getWorldQuaternion(tempQuaternion);
40
41
  return target
41
42
  ? target.setFromQuaternion(tempQuaternion)
42
- : tempEuler.setFromQuaternion(tempQuaternion);
43
+ : tempRotation.setFromQuaternion(tempQuaternion);
43
44
  };
44
45
  /**
45
46
  * Get the world scale of an object.
@@ -51,5 +52,5 @@ export const getWorldRotation = (object, target) => {
51
52
  * @returns
52
53
  */
53
54
  export const getWorldScale = (object, target) => {
54
- return object.getWorldScale(target ?? tempVector3);
55
+ return object.getWorldScale(target ?? tempScale);
55
56
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@threlte/rapier",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "author": "Grischa Erbe <hello@legrisch.com> (https://legrisch.com)",
5
5
  "license": "MIT",
6
6
  "devDependencies": {
@@ -8,7 +8,7 @@
8
8
  "@sveltejs/adapter-auto": "next",
9
9
  "@sveltejs/adapter-static": "^1.0.0-next.29",
10
10
  "@sveltejs/kit": "next",
11
- "@threlte/core": "4.1.2",
11
+ "@threlte/core": "4.2.0",
12
12
  "@types/node": "^18.0.3",
13
13
  "@types/three": "^0.140.0",
14
14
  "@typescript-eslint/eslint-plugin": "^4.31.1",