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

OBB isn't centered (includes proposed fix) #5506

Open
zakaton opened this issue Apr 3, 2024 · 6 comments
Open

OBB isn't centered (includes proposed fix) #5506

zakaton opened this issue Apr 3, 2024 · 6 comments

Comments

@zakaton
Copy link

zakaton commented Apr 3, 2024

Using obb-collider for entities with child entities creates a collider that isn't centered, causing an offset

<html>
  <head>
    <script src="https://aframe.io/releases/1.5.0/aframe.min.js"></script>
  </head>

  <body>
    <a-scene embedded obb-collider="showColliders: true" xr-mode-ui="enabled: false">
      <a-entity obb-collider position="0 1.5 -1" rotation="0 20 0">
        <a-box scale="0.1 0.1 0.1" position="0 0.1 0" color="blue"></a-box>
        <a-box scale="0.1 0.1 0.1" position="0.5 0 0" color="blue"></a-box>
      </a-entity>
    </a-scene>
  </body>
</html>

I looked into the obb-collider.js and was able to create a fix by modifying the tick function to include the boundingBox's center. I made a simple build and you can see it in action in the glitch url above

tick: (function () {
        var auxPosition = new THREE.Vector3();
        var auxScale = new THREE.Vector3();
        var auxQuaternion = new THREE.Quaternion();
        var auxMatrix = new THREE.Matrix4();
        var boundingBoxCenter = new THREE.Vector3(); // FOR POSITION FIX

        return function () {
            var obb = this.obb;
            var renderColliderMesh = this.renderColliderMesh;
            var trackedObject3D = this.checkTrackedObject() || this.el.object3D;

            if (!trackedObject3D) {
                return;
            }

            trackedObject3D.updateMatrix();
            trackedObject3D.updateMatrixWorld(true);
            trackedObject3D.matrixWorld.decompose(auxPosition, auxQuaternion, auxScale);
            
            // POSITION FIX
            this.boundingBox.getCenter(boundingBoxCenter);
            boundingBoxCenter.sub(trackedObject3D.position);
            boundingBoxCenter.applyQuaternion(trackedObject3D.quaternion);
            auxPosition.add(boundingBoxCenter);

            // Recalculate collider if scale has changed.
            if (
                Math.abs(auxScale.x - this.previousScale.x) > 0.0001 ||
                Math.abs(auxScale.y - this.previousScale.y) > 0.0001 ||
                Math.abs(auxScale.z - this.previousScale.z) > 0.0001
            ) {
                this.updateCollider();
            }

            this.previousScale.copy(auxScale);

            // reset scale, keep position and rotation
            auxScale.set(1, 1, 1);
            auxMatrix.compose(auxPosition, auxQuaternion, auxScale);

            // Update OBB visual representation.
            if (renderColliderMesh) {
                renderColliderMesh.matrixWorld.copy(auxMatrix);
            }

            // Reset OBB with AABB and apply entity matrix. applyMatrix4 changes OBB internal state.
            obb.copy(this.aabb);
            obb.applyMatrix4(auxMatrix);
        };
    })(),
@dmarcos
Copy link
Member

dmarcos commented Apr 4, 2024

Can try master? there was some recent fixes

@zakaton
Copy link
Author

zakaton commented Apr 4, 2024

updated glitch example to include the master - same issue

@dmarcos
Copy link
Member

dmarcos commented Apr 4, 2024

Feel free to open a PR. Thanks!

@zakaton
Copy link
Author

zakaton commented Apr 4, 2024

made a fork and added the change, but then I realized that when the entity is itself a child of another entity, the issue persists (I updated the glitch example so now neither solution works)

not sure how to fix it 🤷, but personally I can just modify my personal use-case so the child primitives are each obb-colliders instead of the parent entity

@dmarcos
Copy link
Member

dmarcos commented Apr 5, 2024

Yeah. In this case for example:

<a-entity obb-collider position="0 1.5 -1" rotation="0 20 0">
  <a-box position="0 3 0" color="blue"></a-box>
   <a-box position="0 5 0" color="blue"></a-box>
 </a-entity>

In the image below the bounding box is centered in the parent but should be in the middle point between the child boxes in the y-axis:

Screenshot 2024-04-04 at 6 04 16 PM

General solution is the center of mass?

@dmarcos
Copy link
Member

dmarcos commented Apr 5, 2024

Definitely obb-collider doesn't work at the moment with shifted children with respect to the parent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants