Giter Club home page Giter Club logo

Comments (13)

yandeu avatar yandeu commented on June 3, 2024

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.

from enable3d.

yandeu avatar yandeu commented on June 3, 2024

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

from enable3d.

yandeu avatar yandeu commented on June 3, 2024

Try to just manually switch width and height for now.

from enable3d.

yandeu avatar yandeu commented on June 3, 2024

Can you please upload the model?

from enable3d.

volcanoqq avatar volcanoqq commented on June 3, 2024

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

from enable3d.

volcanoqq avatar volcanoqq commented on June 3, 2024

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,
  });

from enable3d.

yandeu avatar yandeu commented on June 3, 2024

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] }))

from enable3d.

volcanoqq avatar volcanoqq commented on June 3, 2024

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?

from enable3d.

yandeu avatar yandeu commented on June 3, 2024

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.

from enable3d.

volcanoqq avatar volcanoqq commented on June 3, 2024

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。

from enable3d.

deepugupta1846 avatar deepugupta1846 commented on June 3, 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

from enable3d.

yandeu avatar yandeu commented on June 3, 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)

from enable3d.

deepugupta1846 avatar deepugupta1846 commented on June 3, 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;

}

from enable3d.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.