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

shape='box' Add rigid body, why my rigid body is bigger than the actual object, I don't know what's wrong. #365

Open
volcanoqq opened this issue Sep 26, 2023 Discussed in #364 · 13 comments

Comments

@volcanoqq
Copy link

Discussed in #364

Originally posted by volcanoqq September 26, 2023
image

And then I manually pass in width,height,depth, and it works.
image

I rotated the object in three.js before adding the rigid body, and then added the rigid body in the physical world in a box way, the rigid body did not apply the rotation, I don't know what went wrong
image

@yandeu
Copy link
Member

yandeu commented Sep 28, 2023

If it can't detect the shape, it will add 1x1x1 box.
Or you could try to pass shape while creating the physics.

this.physics.add.existing(object, { shape: "mesh" })

But manually adding a box shape (like you did) is way better for performance.

@yandeu
Copy link
Member

yandeu commented Sep 28, 2023

Yes, adding a box shape manually, does not take into account the rotation of the object. It just wraps a box as it is.

@yandeu
Copy link
Member

yandeu commented Oct 9, 2023

Try to just manually switch width and height for now.

@yandeu
Copy link
Member

yandeu commented Oct 9, 2023

Can you please upload the model?

@volcanoqq
Copy link
Author

cell.zip

There are two models, one with the center point in the center and one with the center point not on the object.

There is another problem if the center point is not the center point of the object, the resulting physical rigid body is at the center point instead of the center point of the object

微信截图_20231009180130
微信截图_20231009180235

@volcanoqq
Copy link
Author

volcanoqq commented Oct 10, 2023

image
Figure 1 shows the model directly creating the physical body

image
Figure 2 shows the physical body created after rotating the test1 model

  const test1 = editor.scene.getObjectByName("test1");
  const test2 = editor.scene.getObjectByName("test2");

  // test1.rotateX(Math.PI / 2);
  
  physicsManager.createRigidBodyByObject(test1, {
    shape: "box",
    width: new THREE.Box3()
      .setFromObject(test1)
      .getSize(new THREE.Vector3()).x,
    height: new THREE.Box3()
      .setFromObject(test1)
      .getSize(new THREE.Vector3()).y,
    depth: new THREE.Box3()
      .setFromObject(test1)
      .getSize(new THREE.Vector3()).z,
  });

  physicsManager.createRigidBodyByObject(test2, {
    shape: "box",
    width: new THREE.Box3()
      .setFromObject(test2)
      .getSize(new THREE.Vector3()).x,
    height: new THREE.Box3()
      .setFromObject(test2)
      .getSize(new THREE.Vector3()).y,
    depth: new THREE.Box3()
      .setFromObject(test2)
      .getSize(new THREE.Vector3()).z,
  });

@yandeu
Copy link
Member

yandeu commented Oct 10, 2023

This works for me:

import { PhysicsLoader, Project, Scene3D, THREE } from 'enable3d'

class MainScene extends Scene3D {
  async preload() {
    this.load.preload('obj1', '/assets/obj1.glb')
  }

  async create() {
    this.warpSpeed()
    this.camera.position.set(2, 2, 4)
    this.physics.debug?.enable()

    {
      const obj1 = (await this.load.gltf('obj1')).scene

      const box = new THREE.Box3()
      const size = new THREE.Vector3()
      const center = new THREE.Vector3()
      box.setFromObject(obj1).getSize(size)
      box.setFromObject(obj1).getCenter(center)

      obj1.traverse(child => {
        if (child.isMesh) {
          child.material.metalness = 0
          child.geometry.translate(-center.x, -center.y, -center.z)
        }
      })

      this.add.existing(obj1)
      this.physics.add.existing(obj1, {
        shape: 'box',
        width: size.x,
        height: size.y,
        depth: size.z
      })
    }
  }
}

PhysicsLoader('/lib', () => new Project({ scenes: [MainScene] }))

@volcanoqq
Copy link
Author

volcanoqq commented Oct 11, 2023

It doesn't work for me. Is the model you're testing mine?

If obj1.position.set(0,1,0) then geometry.translate() will it work?

@yandeu
Copy link
Member

yandeu commented Oct 11, 2023

Yes, your model.

Yes. I tried it with setting a position and it worked. Just do it after the obj1.traverse but before you add.physics.

@volcanoqq
Copy link
Author

Okay、I tried using class MainScene extends Scene3D, rotation and center point in my demo and it worked.
But I'm not using the recommended class MainScene extends Scene3D. I'm using import {AmmoPhysics} from "@enable3d/ammo-physics";

I put the demo in https://github.com/volcanoqq/enable3d-app, can you help me take a look at what's the problem cause。

@deepugupta1846
Copy link

deepugupta1846 commented Feb 1, 2024

I am using the 'box' physics collision with the custom width, height and depth but the bounding box is way bigger than the object see image 2
while when i apply 'mesh' it work fine but it will take system performance. Is there any solution for this problem please help!!

mesh
box

@yandeu
Copy link
Member

yandeu commented Feb 1, 2024

I am using the 'box' physics collision with the custom width, height and depth but the bounding box is way bigger than the object see image 2 while when i apply 'mesh' it work fine but it will take system performance. Is there any solution for this problem please help!!

mesh box

Not sure. But you can manually adjust it. (See above)

@deepugupta1846
Copy link

deepugupta1846 commented Feb 1, 2024

I want to make generic code with the performance because I am running this solution on mobile. Mesh is solving the problem right now but it is not the optimized way to achieve for this reason I am using 'Box' collider can you please help me how the manual width, height, depth solve my issue

getBoundingSize: function(object){
const self = this;

	let box = new THREE.Box3();
            box.setFromObject(object);

    let size = new THREE.Vector3();
        box.getSize(size);

    let center = new THREE.Vector3();
        box.getCenter(center);

    return size;

}

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

3 participants