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

Dynamic scaling for Cannon bodies #110

Draft
wants to merge 31 commits into
base: master
Choose a base branch
from
Draft

Conversation

krispya
Copy link
Member

@krispya krispya commented Nov 2, 2021

Hello, this PR is a proof of concept for adding scaling to Cannon bodies that can be dynamically updated at runtime. It is relevant to #102.

Currently the only way to update the size of a shape is to do it manually by changing its dimensions -- which can range from a radius to an array of vertices -- and then calling the appropriate update functions -- which can also be different per shape. This PR attempts to make a common method for scaling the shapes attached to a body in a way that would be familiar to three users. Part of the motivation is to make it easier to have the props used for three meshes to be shared with Cannon bodies so that they can match. Handling scale was one of those headaches.

Since these are rigid bodies, scale isn't the sort of thing that should be updated each frame. Because of this I opted for having the scale set on the body followed by an updateScale() method for its calculation. This way something like a hook can update the scale each time it changes on a corresponding three mesh but only calculate the new bounding box once it settles (or some other logic).

I open this PR as a discussion. I am interested if people think there is a better way to standardize this across the shapes, or a better update method in general. There is a "Scaling" example added that you can use to test. Okay, now some notes on compatibility:

  • Box. Works as expected.
  • Sphere. Works as expected, but only respects the x value in the Vec3. Maybe there is a better way to do this interface.
  • Convex Polyhedron. Works as expected.
  • Cylinder. This is just a convex polyhedron in disguise so it works as expected, but currently the cylinder args go untouched so the scale won't be reflected in them if they are read. This might be a better pattern in general than what I am doing elsewhere.
  • Heightfield. It scales in x, y and z directions which includes starting to split out elementSize into two dimensions. There are issues with scaling and collision though that I need to debug. There is also an internal scale that can get passed in so also should be some refactoring on standardizing how to scale shapes relative to bodies.
  • Particle. Has no dimensions so moot.
  • Plane. Has infinite dimensions so moot.
  • Trimesh. No scaling method implemented here yet.
  • Compound. This is not supported yet and would happen after I fix bugs to do with Heightfield.

@lmf-git
Copy link

lmf-git commented Dec 8, 2021

nice

@marcofugaro marcofugaro self-requested a review December 27, 2021 18:44
@krispya krispya marked this pull request as draft March 29, 2022 01:32
upisfree added a commit to upisfree/cannon-es that referenced this pull request Nov 8, 2022
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

Successfully merging this pull request may close these issues.

2 participants