Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make react-native-rapier work with rapier3d layer #5

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1775,4 +1775,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: bf0dba164bb306f3c0e051504ea8747a310f022a

COCOAPODS: 1.16.2
COCOAPODS: 1.15.2
4 changes: 3 additions & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
"start": "react-native start"
},
"dependencies": {
"@dimforge/rapier3d-compat": "0.11.2",
"react": "18.3.1",
"react-native": "0.75.4"
"react-native": "0.75.4",
"text-encoding-polyfill": "^0.6.7"
},
"devDependencies": {
"@babel/core": "^7.20.0",
Expand Down
83 changes: 71 additions & 12 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,84 @@
import { useState, useEffect, useCallback } from 'react';
import { useCallback } from 'react';
import { StyleSheet, View, Text, Button } from 'react-native';
import Rapier from '@callstack/react-native-rapier';

// @ts-ignore
global.WebAssembly = {
instantiate: async (bytes: any, importObject = {}) => {
const rapier = Rapier.create(importObject);
return {
instance: {
exports: rapier.exports,
},
module: {
bytes,
imports: importObject,
},
};
},
Instance: class {
constructor() {}
},
};

import 'text-encoding-polyfill'; // Required for `rapier3d-compat`

import RAPIER, {
World,
ColliderDesc,
RigidBodyDesc,
} from '@dimforge/rapier3d-compat';

export default function App() {
const [module, setModule] = useState<any | undefined>();
const worldSimulation = useCallback(async () => {
try {
await RAPIER.init();
const gravity = { x: 0.0, y: -9.81, z: 0.0 };
const world = new World(gravity);

useEffect(() => {
setModule(Rapier.create({ wbg: {} }));
}, []);
// Create a ground plane
const groundColliderDesc = ColliderDesc.cuboid(10.0, 0.1, 10.0);
world.createCollider(groundColliderDesc);

const callFn = useCallback(() => {
console.log(module, module.exports);
let res = module.exports.rawimpulsejointset_new();
console.log('res: ', res);
}, [module]);
// Create a dynamic rigid-body with a cube collider
const rigidBodyDesc = RigidBodyDesc.dynamic().setTranslation(
0.0,
5.0,
0.0
);
const rigidBody = world.createRigidBody(rigidBodyDesc);

const colliderDesc = ColliderDesc.cuboid(0.5, 0.5, 0.5);
world.createCollider(colliderDesc, rigidBody);

// Simulation loop
const timeStep = 1 / 60;
function simulate() {
world.step();

// Get the position of the cube
const position = rigidBody.translation();
console.log(
`Cube position: x=${position.x.toFixed(2)}, y=${position.y.toFixed(2)}, z=${position.z.toFixed(2)}`
);

// Continue simulation if cube is above ground
if (position.y > 0.5) {
setTimeout(() => simulate(), timeStep * 1000);
}
}

// Start the simulation
simulate();
} catch (error) {
console.error(error);
}
}, []);

return (
<View style={styles.container}>
<Text>Module loaded: {!!module}</Text>
{!!module && <Text>{JSON.stringify(module)}</Text>}
{!!module && <Button title="Call" onPress={callFn} />}
<Button title="Run simulation" onPress={worldSimulation} />
</View>
);
}
Expand Down
8 changes: 2 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"types": "./lib/typescript/commonjs/src/index.d.ts",
"default": "./lib/commonjs/index.js"
}
}
},
"./package.json": "./package.json"
},
"files": [
"src",
Expand Down Expand Up @@ -158,7 +159,6 @@
"source": "src",
"output": "lib",
"targets": [
"codegen",
[
"commonjs",
{
Expand All @@ -184,10 +184,6 @@
"name": "RNReactNativeRapierSpec",
"type": "all",
"jsSrcsDir": "src",
"outputDir": {
"ios": "ios/generated",
"android": "android/generated"
},
"android": {
"javaPackageName": "com.callstack.reactnativerapier"
}
Expand Down
16 changes: 16 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1629,6 +1629,7 @@ __metadata:
"@babel/core": ^7.20.0
"@babel/preset-env": ^7.20.0
"@babel/runtime": ^7.20.0
"@dimforge/rapier3d-compat": 0.11.2
"@react-native/babel-preset": 0.75.4
"@react-native/metro-config": 0.75.4
"@react-native/typescript-config": 0.75.4
Expand All @@ -1637,6 +1638,7 @@ __metadata:
react-native: 0.75.4
react-native-builder-bob: ^0.30.2
react-native-test-app: ^3.10.14
text-encoding-polyfill: ^0.6.7
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -1875,6 +1877,13 @@ __metadata:
languageName: node
linkType: hard

"@dimforge/rapier3d-compat@npm:0.11.2":
version: 0.11.2
resolution: "@dimforge/rapier3d-compat@npm:0.11.2"
checksum: f0fbf99feaef4ea3b5cb3d81892c1caf14bd35c89c90ce31462f3707f068f5677d5a2c845efdefa2951a90b276a6ba63a9453f139badda66c4b322b661bf6b81
languageName: node
linkType: hard

"@eslint-community/eslint-utils@npm:^4.2.0":
version: 4.4.0
resolution: "@eslint-community/eslint-utils@npm:4.4.0"
Expand Down Expand Up @@ -12334,6 +12343,13 @@ __metadata:
languageName: node
linkType: hard

"text-encoding-polyfill@npm:^0.6.7":
version: 0.6.7
resolution: "text-encoding-polyfill@npm:0.6.7"
checksum: 8e5b45154f3394cd29af01760ec2f728caba7cfc57fbcab60c073dc9c6db479d923efda3e074ae467379c4a0710c45e6427c6917a70da3059876924c190e03ed
languageName: node
linkType: hard

"text-extensions@npm:^1.0.0":
version: 1.9.0
resolution: "text-extensions@npm:1.9.0"
Expand Down
Loading