Comments (22)
it seems to work, that was easier than i thought it would be :-)
from use-cannon.
Seems to work fine based on testing in CompoundBody demo
from use-cannon.
Published in 0.2.8
from use-cannon.
nice one! thanks a lot
from use-cannon.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
@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.
Revisit for performance improvements
from use-cannon.
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.
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.
@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.
@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)
- CCD / Continous Collision Detection Enable HOT 3
- useCannon/usePhysics hook to get world state HOT 1
- docs: broken examples url
- Add body existence check in step function HOT 7
- Is it possible to use cannon-worker-api with vanilla cannon-es? HOT 1
- GET http://127.0.0.1:5173/node_modules/.vite/deps/@react-three_cannon.js?v=78dbe0b1 net::ERR_ABORTED 504 (Gateway Timeout) HOT 2
- Jest cannot find module '@react-three/cannon' HOT 4
- InstanceMesh Performance Issue
- Detect collision between two kinematic objects HOT 3
- Can't use react-cannon with other libraries
- Deprecated function example
- Circle and Cylinder sinks into Plane HOT 1
- Doesn't work with Remix.run
- Convex collider hit another convex collider makes the screen froze
- mesh controlled by useBox doesn't respect its parent group tag properties
- useTrimesh scale HOT 2
- Android browser compatibility lower than Chrome80 HOT 1
- Can't set amount of segments for shapes within compoundBody
- Suggestion: 'Inverted Collider' Option HOT 1
- Create a type in useBox which is static and also can be moved with the api of useBox
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from use-cannon.