Jump to content

Help with physics and performance


PorlyBe
 Share

Recommended Posts

Hello @PorlyBe,

welcome to the forum.

Physics is fun! for someone being new to babylon and javascript, you did very well creating your first playground :)

A few notes that might get move you forward:

1) There is nothing you can do about the amount of impostors and their impact on performance. If you need ALL coins to always react correctly, they will all have to have their own impostors.

2) Try disposing unneeded impostors. Meaning - If you are 100% sure a coin will never be a part of the pile that needs to react to a new coin, dispose its impostor (and recreate it when needed). This will increase performance. I di it usually when an object is below a certain threshold (for example,  if it falls into a pit or something) or when it is not visible to the user. Of course, you need to set your own rules.

3) Try using OimoJS instead - Oimo is wonderful when it comes to a greater number of objects. A simple example - https://www.babylonjs-playground.com/#P13KEN

You will notice, that I changed your code a bit.

First, I took setTimeout out. The reason for that is rather simple - setTimeout is not being "disposed" when you press run again. I guess you noticed your console is full of errors, and you didn't know why - this is because each time you press "run", the setTimeout function(s) of the last playground is still set and is still being executed. I used registerBeforeRender with a simple frame counter, but you can use whatever your heart desires.

Second - I created a new impostor for each object and removed it from the cloning object. I don't see a reason doing that and then changing the mass. It is a change that is simple not needed. Create your impostor with the right mass at the right time.

 

Hope this gets you started! Let me know if you have any other questions.

Link to comment
Share on other sites

Main thing that jumps out at me is, Cannon's cylinder primitive isn't a mathematically exact cylinder - it creates an approximation out of subdivisions, and Babylon passes in 16 for the number of subdivisions (which is probably overkill for your case).

So anyway each coin in the simulation is a convex polyhedron with like 64 faces (?), so that's why it gets so slow. If Oimo has precise cylinder primitives, using it instead should be way faster.

You should probably also search for coins that are on the bottom of the pan, or under other coins, and change them to static if they're not moving. That wouldn't be exactly physically correct, but it would limit the number of collisions and make things scale much better.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...