feat: add zero-allocation scalar getters/setters for RigidBody, Collider, and KinematicCharacterController#365
Open
GbGr wants to merge 1 commit intodimforge:masterfrom
Open
Conversation
…der, and KinematicCharacterController Add scalar component getters that return individual f32→number values, bypassing all intermediate struct creation, WASM heap allocation, and JS wrapper object overhead. Getters: - RigidBody: translationX/Y(/Z), rotationAngle (2D), rotationX/Y/Z/W (3D), linvelX/Y(/Z) - Collider: translationX/Y(/Z), rotationAngle (2D), rotationX/Y/Z/W (3D) - KinematicCharacterController: computedMovementX/Y(/Z) Setters (scalar-argument alternatives avoiding VectorOps.intoRaw() allocation): - RigidBody: setLinvelXY/XYZ, addForceXY/XYZ, applyImpulseXY/XYZ Each existing struct-returning method (translation(), rotation(), linvel(), etc.) remains unchanged for backwards compatibility.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add scalar component getters and setters that return/accept individual
f32→numbervalues, bypassing all intermediate struct creation, WASM heap allocation, and JS wrapper object overhead.Motivation
Every call to
RigidBody.translation(),.rotation(),.linvel(), etc. currently creates multiple short-lived allocations:RawVector/RawRotationstruct on the WASM heap (Box::new)Object.create+FinalizationRegistry)VectorOps.fromRaw()reads.x/.yvia WASM getter calls, creates a newVector2/Vector3/Quaternion, callsraw.free()For game engines doing
syncDynamicFromRapierwith hundreds of entities at 60+ ticks/sec (with rollback resimulation multiplying that), this creates tens of thousands of short-lived allocations per second — unnecessary GC pressure.The new scalar methods return primitives directly through the WASM stack, eliminating all allocations.
Per-call savings (getter)
Changes
Rust (
src/)Getters (return
f32, zero-alloc):RawRigidBodySet:rbTranslationX/Y(/Z),rbRotationAngle(2D),rbRotationX/Y/Z/W(3D),rbLinvelX/Y(/Z)RawColliderSet:coTranslationX/Y(/Z),coRotationAngle(2D),coRotationX/Y/Z/W(3D)RawKinematicCharacterController:computedMovementX/Y(/Z)Setters (accept scalar args, avoid
RawVectorheap allocation):RawRigidBodySet:rbSetLinvelXY/XYZ,rbAddForceXY/XYZ,rbApplyImpulseXY/XYZTypeScript (
src.ts/)Corresponding methods on
RigidBody,Collider, andKinematicCharacterControllerclasses. All existing methods remain unchanged for backwards compatibility.Tests
rapier-compat/tests/ScalarGetters2d.test.tsrapier-compat/tests/ScalarGetters3d.test.tsVerifies scalar getters match existing struct-returning methods, and scalar setters work correctly.
Backwards compatibility
Purely additive — no existing method signatures or return types were changed.