stephengold / Libbulletjme

A JNI interface to Bullet Physics and V-HACD
https://stephengold.github.io/Libbulletjme/lbj-en/English/overview.html
Other
83 stars 9 forks source link

RigidBody Location/Orientation listener ? #5

Open AlexisDrogoul opened 3 years ago

AlexisDrogoul commented 3 years ago

Hi,

I have begun using (and adapting) Libbulletjme to the GAMA simulation platform (http://gama_platform.org), in order to replace the ageing JBullet implementation we were using. Thank you for the wonderful job you've been doing and for producing a version of the library usable by non-JME users/developers.

I have however a question (or request) : in the previous version of Bullet (and JBullet), the motion state of rigid bodies was receiving setTransform(...) whenever the body was changing its location or orientation in space. This allowed me to notify the corresponding agents in the simulation world of the changes.

I havent been able to find a similar entry point in your library. Does it mean it does not exist (anymore) in Bullet and that the only solution is to browse through all rigid bodies every step to read their motion state ? Or is there some smarter way to implement such a "listener" ?

Thanks in advance !

stephengold commented 3 years ago

Perhaps the RigidBodyMotionState class can meet your need.

AlexisDrogoul commented 3 years ago

But the methods by which this motion state is updated are not exposed in Java, are they ? What I had in mind was to create, like I did with JBullet, a subclass of the motion state in order to capture the messages sent by the engine whenever a change in location/rotation was made for the body (and later propagate this change to the software agent in charge of this physical body).

stephengold commented 3 years ago

You're right that it's currently not exposed. I'll investigate how difficult it might be to expose.

AlexisDrogoul commented 3 years ago

Actually, any kind of listener to the properties changes (ideally those in the MotionState -- location and rotation --) would be nice. I have changed my strategy: I'm not trying to inherit from Bullet objects (body, motion state, etc. as I did with JBullet) anymore, and I manage the exchanges between the simulation and the engine with a thin layer of wrappers. It works well but the downside is that, with let's say 10000 agents in a scene and only 100 of them actually changing their position or orientation, I have to browse through all of them to verify it (which means I also have to keep somewhere a record of their previous location). Not extremely efficient, and it could be better handled with some listener attached to the bodies. Thanks anyway for the hard work on this very nice library !

stephengold commented 3 years ago

The most straightforward way to add location/rotation listeners for rigid bodies would be to define a new MotionStateListener interface and add an addListener() method to RigidBodyMotionState.

Before introducing a new interface, I'd like to get clearer on the motivation. What's the application? The scene you describe (10000 agents with only 1% moving on any particular tick) sounds like a corner case to me. Do the other 9900 agents ever move at all? I'm also surprised that you don't already have a record each agent's previous location.

AlexisDrogoul commented 3 years ago

Yep. That's a corner case. You're right. One of the extreme cases I'm using as tests. But actually, we have users on GAMA building very different models (from people evacuating buildings to land-use change under climate changes, from physics to computational sociology) and each of them is a corner case. We cant cover all of them, obviously, but at least try to offer balanced and reasonable performances in different scenarios. Regarding the location+rotation, well, it's actually a bit more complicated than that. Each agent has already a "body" defined as a 2D JTS geometry (+ additional 3D information when we use it). Neither its location or rotation are kept as they are completely encoded into the geometry itself : the location is the center of mass of the of the object, and the rotation is applied to the object immediately. To make a long story short, comparing the new location to the previous one is evidently possible, but it's a long process. And rotation would need to completely change the encoding of geometries...