kripken / ammo.js

Direct port of the Bullet physics engine to JavaScript using Emscripten
Other
4.18k stars 559 forks source link

ThreeJS - items in group falling to wrong direction after rotation #384

Open AfarsheziF opened 2 years ago

AfarsheziF commented 2 years ago

Hello all. I like to ask your help solving this rare case.

I have a group containing items (spheres). I am then able to rotate the group and therefore changing the positions of the items with it. When I active gravity with vector(0, -100, 0) after the group rotation, the items will fall to the direction the group was rotated to and not to the gravity direction.

Gravity without rotating the group: Untitled2

Gravity after rotating the group (group base is now facing the camera): Untitled

Is this related to .up direction? Both group and items .up vector is always pointing (0, 1, 0). Any ideas?

Thank you for your help and time!

kaphula commented 2 years ago

Is there even a concept of groups in Bullet? I don't think you can rely on the ThreeJS groups to rotate a group of objects that have been added to the physics world. You must rotate each individual physics object (in world space) and then ask for their motion state (position and rotation) and pass those values to ThreeJs so it can draw the objects to correct places based on the physics simulation.

You can however use ThreeJs to make a bunch of balls, add them to a group, rotate that whole group in world space AND THEN construct the corresponding physics objects based on the rotated world position of the ThreeJs ball objects.

You might also want to make sure that you give the physics world a "pure" new btVector that is not referenced by anything else by accident.

physicsWorld.setGravity(new Ammo.btVector3(0, -5, 0));