-
I came across #564 and wanted to implement this in my react-native jsi turbo module (new-arch) I wasn't really able to find many example of how to do this, so I resorted to reading the code surrounding The result that I came up with is pretty much the following: //world.h
class PhysicsFrame : jsi::MutableBuffer {
public:
std::vector<double> buffer;
PhysicsFrame();
~PhysicsFrame();
size_t size() const override;
uint8_t *data() override;
};
namespace joltphysics {
class world {
private:
//bunch of private members ...
PhysicsFrame physics_frame;
public:
//other world methods
}
}
//world.cpp
joltphysics::world::world(
facebook::react::JoltPhysicsModule &module,
jsi::Runtime& rt,
const std::shared_ptr<facebook::react::CallInvoker> &jsInvoker
)
:
physics_frame()
{
//... some world initialization stuff above this(it works)
//create the physics frame ArrayBuffer
auto physics_frame_sp = std::make_shared<PhysicsFrame>(physics_frame);
auto arrayBuffer = jsi::ArrayBuffer(rt, std::reinterpret_pointer_cast<jsi::MutableBuffer>(physics_frame_sp));
rt.global().setProperty(rt, "__PhysicsFrame", arrayBuffer);
}
PhysicsFrame::PhysicsFrame() {
buffer.reserve(cMaxBodies * 7);
}
PhysicsFrame::~PhysicsFrame() {
buffer.clear();
}
size_t PhysicsFrame::size() const {
return buffer.size() * sizeof(double);
}
uint8_t *PhysicsFrame::data() {
return reinterpret_cast<uint8_t *>(buffer.data());
} The important bits here are the following: //create the jsi::ArrayBuffer from the jsi::MutableBuffer
auto arrayBuffer = jsi::ArrayBuffer(rt, std::reinterpret_pointer_cast<jsi::MutableBuffer>(physics_frame_sp));
//set the ArrayBuffer to the global object
rt.global().setProperty(rt, "__PhysicsFrame", arrayBuffer); In my JS I access the __PhysicsFrame like so: const physicsBuffer: ArrayBuffer = global["__PhysicsFrame"]
const physicsFrameBuffer = new Float64Array(physicsBuffer) While this indeed gives me an ArrayBuffer and a Float64Array typed array (not undefined) Im pretty lost and appreciate anyones help 🙂 |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
I see a couple of smaller problems:
I don't think either of these explain your problem, but there is something else that I am curious about: where are you setting the size of |
Beta Was this translation helpful? Give feedback.
-
My buffer is mutated inside the I'll provide some context, I'm creating a native JSI wrapper for the popular open source physics engine JoltPhysics Modifying the buffer is done in two locations:
//push position to physics frame
physics_frame.buffer.push_back(body.position.x);
physics_frame.buffer.push_back(body.position.y);
physics_frame.buffer.push_back(body.position.z);
//push rotation to physics frame
physics_frame.buffer.push_back(static_cast<double>(creation_settings.mRotation.GetX()));
physics_frame.buffer.push_back(static_cast<double>(creation_settings.mRotation.GetY()));
physics_frame.buffer.push_back(static_cast<double>(creation_settings.mRotation.GetZ()));
physics_frame.buffer.push_back(static_cast<double>(creation_settings.mRotation.GetW()));
void joltphysics::world::step(jsi::Runtime &rt, const std::shared_ptr<facebook::react::CallInvoker> &jsInvoker_) {
//more physics step logic above...
//update the physics frame
for (int i = 0; i < bodies.size(); i++) {
auto body_id = bodies[i];
auto position = body_interface.GetPosition(body_id);
auto rotation = body_interface.GetRotation(body_id);
//update positions
physics_frame.buffer[i*7] = position.GetX();
physics_frame.buffer[i*7+1] = position.GetY();
physics_frame.buffer[i*7+2] = position.GetZ();
//update rotations
physics_frame.buffer[i*7+3] = rotation.GetX();
physics_frame.buffer[i*7+4] = rotation.GetY();
physics_frame.buffer[i*7+5] = rotation.GetZ();
physics_frame.buffer[i*7+6] = rotation.GetW();
}
//more physics step logic below...
} Again the important bits here being:
EDIT: just checked |
Beta Was this translation helpful? Give feedback.
-
I'm wondering if for some reason the pointer returned from uint8_t *PhysicsFrame::data() {
return reinterpret_cast<uint8_t *>(buffer.data());
} I'm not really sure, maybe I'm doing something else wrong, but looking at |
Beta Was this translation helpful? Give feedback.
-
Are you sure that the buffer size is non-zero when you call |
Beta Was this translation helpful? Give feedback.
Are you sure that the buffer size is non-zero when you call
jsi::ArrayBuffer
? The size of the ArrayBuffer cannot change after creation, it has to be created with its final size.