chandlerprall / Physijs

Physics plugin for Three.js
MIT License
2.78k stars 456 forks source link

Scaling objects doesnt scale their physijs size #20

Closed ghost closed 12 years ago

ghost commented 12 years ago

var box = new Physijs.BoxMesh(new THREE.CubeGeometry(10, 10, 10));
box.scale.set(1000, 1000, 1000);

produces a 1000 by 1000 by 1000 box which acts like its 10 by 10 by 10 when collided with

chandlerprall commented 12 years ago

Scaling is the one feature of Three.js that I purposefully left out because it is a feature not supported by physics libraries. It would be impossible to update an object's scaling on the fly and a new collision body would need to be created every time the scale changes. This would be an expensive operation to do between frames if the user isn't planning for it, and I feel that cost is much more apparent if you are forced to create a whole new object if you change an object's size.

That said, I could use an object's scale at creation time for the physics body but I don't want to set up an expectation for the user that changing the scale later will have an effect. The only use case I can think of is if you are importing a custom geometry and want multiple sizes of it.

What are you trying to use scale to do?

ghost commented 12 years ago

thats actually a really good reason for leaving it out. its not really a problem, but it would be nice if it was hinted at somewhere in the documentation - i just spent a couple hours in confusion because i scaled my ground x100 as a BoxMesh and objects kept falling through. a mistake on my part that hopefully no one else makes

chandlerprall commented 12 years ago

I apologize for the confusion - I've added a page to the wiki to help address this.

tapio commented 12 years ago

That said, I could use an object's scale at creation time for the physics body but I don't want to set up an expectation for the user that changing the scale later will have an effect. The only use case I can think of is if you are importing a custom geometry and want multiple sizes of it.

@chandlerprall I have bumped into that exact use case: I'm loading a few different rock models and I want the size of each individual instance be randomized to give extra variance to the scene, but I also want to reuse the geometry.

As far as I know, THREE.Geometry doesn't have a scale property, so the scale would need to be another parameter to Physijs mesh constructor, but that way it should also be apparent that the scale is a construction time option.

chandlerprall commented 12 years ago

'scale' property is now respected when adding a mesh to the scene.

tapio commented 12 years ago

Thanks!

lazd commented 11 years ago

It doesn't seem scaling works for Physijs.ConcaveMesh:

var groundMesh = new Physijs.ConcaveMesh(geometry, faceMaterial, 0);
groundMesh.scale.set(10, 10, 10);
scene.add(groundMesh);

The mesh appears and can be collided with, but it is considered to be it's default size by the physics engine. I had a look at 1afa96be42ba2b879a9dbb92984e6296f8bb9f91, then added a breakpoint at line 784, and the ConcaveMesh doesn't seem to have object._physijs.width and friends. Setting those variables manually to 1 doesn't help either.

Thoughts?

chandlerprall commented 11 years ago

@lazd mind creating a new issue for this? It's a simple fix but I'd like an active ticket so I don't lose track of it and forget.