Giter Club home page Giter Club logo

Comments (22)

drcmda avatar drcmda commented on May 19, 2024 2

it seems to work, that was easier than i thought it would be :-)

from use-cannon.

codynova avatar codynova commented on May 19, 2024 2

Seems to work fine based on testing in CompoundBody demo

from use-cannon.

codynova avatar codynova commented on May 19, 2024 1

Published in 0.2.8

from use-cannon.

drcmda avatar drcmda commented on May 19, 2024 1

nice one! thanks a lot

from use-cannon.

RobRendell avatar RobRendell commented on May 19, 2024 1

You guys are amazingly quick! I get up this morning to find all this extra activity :)

When I started reading the reopening comments, my thought was that if you had memory concerns about the array of active bodies, then a simple counter would also work for detecting any activity... each body would increment it when it transitions from asleep to awake (or when it's added in an awake state) and decrement it when it transitions from awake to asleep.

However, I imagine the array of active bodies will be useful for other optimisations too.

from use-cannon.

codynova avatar codynova commented on May 19, 2024 1

The World already has to iterate over every body multiple times in each step to do integrations, solve contact equations, evaluate and alter waking state, etc.

from use-cannon.

drcmda avatar drcmda commented on May 19, 2024 1

maybe you forgot to build? that is, if you're working with the examples repo. that happens to me all the time. simply updating it wont do.

from use-cannon.

RobRendell avatar RobRendell commented on May 19, 2024 1

I didn't find the examples repo, so I pulled this repo and copy/pasted the CompoundBody demo into my project.

However, I discovered that the problem was with my configuration - I misunderstood the sleepTimeLimit parameter, and assumed it was in milliseconds. The simulation was able to sleep, I just wasn't waiting long enough :)

Apologies for the false report! Thank you so much for being so responsive!

from use-cannon.

drcmda avatar drcmda commented on May 19, 2024

you're right, it should call invalidate. but it's a bit tricky atm because use-cannon itself is useFrame based. i did try to bring the mutation stuff into the main loop, but got into some scope problems, there's data which only the hooks have. but i think it could call invalidate in the main loop if it detects a change. @codynova is it possible to know outright if anything was moving without going through complex vector old-new checks?

from use-cannon.

codynova avatar codynova commented on May 19, 2024

The World's internalStep function always sets the dirty flag on the Broadphase, so as far as the Broadphase is concerned, the simulation is always running. I think the easiest way to check for movement would be something like this:

world.bodies.some(body => body.sleepState !== Body.SLEEPING)

which could be used in conjunction with sleepTimeLimit and sleepSpeedLimit for greater control.

from use-cannon.

drcmda avatar drcmda commented on May 19, 2024

my only worry is, what if we're dealing with hundredes (thousands?) of objects. it would have to climb through that loop every frame. is there maybe a subscription at the body level we can use to flag active state globally?

from use-cannon.

codynova avatar codynova commented on May 19, 2024

There's no reason we couldn't add one. The World (and sometimes the Solver) already iterate through every body at least one on each frame.

What do you think makes the most sense? The most basic solution would be having bodies add/remove themselves from a World.activeBodies array (or Map), then just checking that length > 0.

I wonder what effect that would have on memory? Schteppe did quite a bit of memory optimization, so if there is a more memory-efficient solution I'd love to know!

from use-cannon.

drcmda avatar drcmda commented on May 19, 2024

that sounds great! yeah, i tend to forget, we can change cannon now. :-D i think mem impact for a bunch of flags won't be a problem.

from use-cannon.

codynova avatar codynova commented on May 19, 2024

I was wondering more about the memory impact of the array of bodies, but I'm not really sure how that works since it's all pass-by-reference, I'd need to actually test it. I will take a stab at the solution I proposed above...

from use-cannon.

codynova avatar codynova commented on May 19, 2024

Newer frame validation which simply checks the length of World.activeBodies each frame by referencing getter World.hasActiveBodies released in 0.2.9

from use-cannon.

codynova avatar codynova commented on May 19, 2024

Happy to do it! There is probably a more efficient way, by assuming hasActiveBodies = false and checking for non-sleeping bodies in the World step - but it's a little trickier to figure out when it's safe to read a body's sleepState in the step. I may try to revisit it a little later, while keeping the forward-facing API the same (World.hasActiveBodies)...

from use-cannon.

codynova avatar codynova commented on May 19, 2024

@RobRendell That's a good idea. The counter is closer to what I was imagining doing in the World step. I think we could push it one step further. Rather than keeping a count, just check the body's sleepState in the World step and switch the flag to true if any body is awake - otherwise default to false. The counter would be easier to implement because we can simply call it from the body's wakeUp or sleep methods. The flag is probably (slightly) more performant, but it requires being more careful during the integration with the World step method. I'm wondering if we can come up with any other potential uses for the activeBodies - I can't think of a good reason to keep it.

from use-cannon.

codynova avatar codynova commented on May 19, 2024

Revisit for performance improvements

from use-cannon.

RobRendell avatar RobRendell commented on May 19, 2024

Well, presumably you only need to iterate over the active bodies when simulating updates? If the scene has lots of sleeping physics objects, not having to iterate over the entire list would be more efficient. I imagine that in "real world" type scenes, most physics objects are at rest...

from use-cannon.

codynova avatar codynova commented on May 19, 2024

The new method of updating flags in the World step was released in [email protected] and [email protected]. I'll wait to close this time until we confirm it's working properly :)

from use-cannon.

RobRendell avatar RobRendell commented on May 19, 2024

@codynova Sorry to bug you yet again, but this appears to have regressed (checked with both versions 0.2.11 and 0.3.0). The invalidate function is being called all the time, even after the bodies are sleeping. I even checked by adding a useFrame() callback to the CompoundBody demo, and the callback continues to be invoked, apparently indefinitely.

from use-cannon.

codynova avatar codynova commented on May 19, 2024

@RobRendell Do you mind creating a minimum viable example codesandbox? I just tested locally with invalidateFrameloop on the Canvas and a callback useFrame(() => console.log('test')) - and it seems to work fine, unless I'm just missing something

from use-cannon.

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.