Skip to content

Instantly share code, notes, and snippets.

@NSTCG
Last active June 19, 2025 12:16
Show Gist options
  • Save NSTCG/75ec645dce590e4fcc8b2dccac9b5c34 to your computer and use it in GitHub Desktop.
Save NSTCG/75ec645dce590e4fcc8b2dccac9b5c34 to your computer and use it in GitHub Desktop.
A simple wonderland engine component to grab and release a physx / non physx object in VR and nonVR.
/**
Copyright © 2025 Nithin Steven F <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
import {Component, Object3D, PhysXComponent} from "@wonderlandengine/api";
import {CursorTarget} from "@wonderlandengine/components";
import {property} from "@wonderlandengine/api/decorators.js";
import {quat2} from "gl-matrix";
/**
* A component that lets you grab and release objects
*
* If using phsx objects ,make sure they are not static and collision shape is not triangle mesh.(if you want to use custom shape , use convex mesh)
* For moving physx collision objects make sure "raycast mode" in cursor component is set to physx
* cursor is already setup in vr template
*
* incase you wanna set it up manually :
*
* add cursor component in object named nonVrcamera (with view and mouse look component ) for nonvr
* add cursor in objects named cursorRight/cursorLeft for vr , make sure to add input component with rayleft or rayright
* set raycast mode to physx if you wanna pick up physx objects.
*
*/
export class GrabRelease extends Component {
static TypeName = "grab-release";
@property.object() nonVrCamera;
@property.object() vrController;
@property.float(3) offset;
static onRegister(engine) {
engine.registerComponent(CursorTarget);
}
/* Position to return to when "unpressing" the button */
returnPos = new Float32Array(3);
target: CursorTarget;
parent: Object3D | null = null;
currentTransform = quat2.create();
hasPhysx = false;
physxComp: PhysXComponent | null = null;
start() {
this.target =
this.object.getComponent(CursorTarget) ||
this.object.addComponent(CursorTarget);
this.parent = this.object.parent;
try {
this.physxComp = this.object.getComponent(PhysXComponent);
console.log(this.physxComp);
if (this.physxComp) {
this.hasPhysx = true;
}
} catch {
this.hasPhysx = false;
}
}
update() {
this.currentTransform = this.object.getTransformWorld();
}
onActivate() {
this.target.onDown.add(this.onDown);
window.addEventListener("pointerup", this.onPointerUp.bind(this));
window.addEventListener("select", this.onPointerUp.bind(this));
this.engine.onXRSessionStart.add(this.onXRSessionStart.bind(this));
}
onXRSessionStart(s) {
s.addEventListener("selectend", this.onPointerUp.bind(this));
}
onDeactivate() {
this.target.onDown.remove(this.onDown);
window.removeEventListener("pointerup", this.onPointerUp.bind(this));
window.removeEventListener("select", this.onPointerUp.bind(this));
}
onPointerUp = () => {
this.object.parent = this.parent;
this.object.setTransformWorld(this.currentTransform);
if (this.hasPhysx) {
this.physxComp!.kinematic = false;
}
};
/* Called by 'cursor-target' */
onDown = (_, cursor) => {
if (this.hasPhysx) {
try {
this.physxComp!.kinematic = true;
} catch {
console.error(
`Oopsies , you are moving an immovable object
In your physic component untick the static
and use simple shapes or convex mesh
triangle mesh cant move`,
);
return;
}
}
setTimeout(() => {
this.object.parent = this.engine.xr
? this.vrController
: this.nonVrCamera;
this.object.resetPositionRotation();
this.object.translateLocal([0, 0, -this.offset]);
});
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment