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

Character movement round 1 #881

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open

Character movement round 1 #881

wants to merge 26 commits into from

Conversation

erincatto
Copy link
Owner

@erincatto erincatto commented Feb 4, 2025

Sensor updates:

  • support sensor vs sensor
  • enableSensorEvents returns and is false by default
  • sensors now ignore shapes on the same body
    [fix] body move events are now correctly adjusted for time of impact

@jpeletier
Copy link

jpeletier commented Feb 19, 2025

Hello! I am testing this branch, this is really an improvement! Thanks!

I would suggest to rename the new enableSensorEvents flag to detectedBySensors because at first I thought I had to enable this in the sensor itself, but rather it must be enabled in all the other shapes that could be detected by a sensor.

Anyway, thanks!

bool isSensor;

/// Enable contact events for this shape. Only applies to kinematic and dynamic bodies. Ignored for sensors.
/// Enable sensor events for this shape. This applies to sensors and non-sensors. False by default, even for sensors.
bool enableSensorEvents;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to rename the new enableSensorEvents flag to detectedBySensors because at first I thought I had to enable this in the sensor itself, but rather it must be enabled in all the other shapes that could be detected by a sensor.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got a feature request for sensors themselves to be disabled and for sensors to detect other sensors. Basically every combination.

@jpeletier
Copy link

Hmm, after further testing, I see that enableSensorEvents must be true for both the sensor itself and the detected shape, otherwise no events are produced.

Shouldn't it work so that enableSensorEvents must be true only for the target shape for a sensor to work? Otherwise the sensor itself could be detected by other sensor, which perhaps is not the usual case.

For now, I am setting enableSensorEvents to true for all sensors inconditionally, otherwise they won't work.

Thanks!

src/world.c Outdated
@@ -2488,6 +2577,112 @@ b2TreeStats b2World_CastPolygon( b2WorldId worldId, const b2Polygon* polygon, b2
return treeStats;
}

static b2AABB b2ComputerShapeBounds( const b2ShapeProxy* shape, b2Transform xf )
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be Compute?

bool isSensor;

/// Enable contact events for this shape. Only applies to kinematic and dynamic bodies. Ignored for sensors.
/// Enable sensor events for this shape. This applies to sensors and non-sensors. False by default, even for sensors.
bool enableSensorEvents;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding this before enableHitEvents will subtly break bindings generated for 3.0.0 that point to a 3.1.0 shared library, which might be unexpected if people expect Open/Close Principle adherence.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to organize structs to for good packing, not for adding new stuff to the end. Shouldn't a binding generator look at the names?

I also want these two bools to be close together because they are related.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair, I just wanted to point it out.

Binding generators will create bindings at design time, but they are fixed at runtime - and so something compiled against Box2D 3.0.0 will have a layout mismatch if it's later pointed to 3.1.0. Because at the interface it's just a lump of data, there will be no compiletime or runtime errors and fields in the consumer code will just be in the wrong locations. The best thing for developers is to just recreate the bindings programmatically, but where someone's got, for example, a NuGet package that on the surface appears to do it for them, and they expect to be able to just drop in a Box2D 3.1.0 shared library, then they'll run into trouble when they use the 'displaced' fields.

In my case it's put together manually and I'm taking care to observe and absorb changes in 3.1.0. Here's what it looks like for me:
image

Each bool takes 1 byte but internalValue is aligned to the next 4-byte boundary, so the packing is not affected in this case and it won't break as it would if you were 1 bool over the next boundary.

With all that said, I understand your reasons for putting EnableSensorEvents adjacent to IsSensor

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm hoping to generate some json like raylib does to assist with binding generators. I wonder if some sort of version number check could be used.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my case I could call b2GetVersion at first use, but I'm building the shared libs and including them in the NuGet package so everything should be "joined up" anyway. That's something I might do at build time, so that I get the error if the shared lib version doesn't match, and then I can deal with it. A unit test would be a good place for it.

Those using what we could call "prepared bindings" could call b2GetVersion at their leisure, but I don't see a good way of using that to try to solve this problem. Even if you required that people include the version in b2WorldDef, if they get it from b2GetVersion then the value will be correct always. It would be the same for modifying B2_SECRET_COOKIE: because box2d itself is responsible for distributing it, it would always match.

@@ -1440,9 +1448,15 @@ typedef struct b2DebugDraw
/// Option to draw contact normal impulses
bool drawContactImpulses;

/// Option to draw contact feature ids
bool drawContactFeatures;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding this before drawFrictionImpulses will subtly break bindings generated for 3.0.0 that point to a 3.1.0 shared library, which might be unexpected if people expect Open/Close Principle adherence.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot anticipate all the assumptions made by binding generators. I'll make a note of this in the 3.1 release notes.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had made my bindings against the partial 3.1.0 in main, which was an error on my part. I've changed everything so my Github actions build the latest tag instead, and my bindings work against 3.0.0, with a unit test to detect if b2Version returns something other than 3.0.0. When you're ready to release 3.1.0, I'll get a failing build and I'll update my code accordingly. The two I've mentioned in this PR would be the tip of the iceberg if we were to analyse the gap between 3.0.0 and 3.1.0, so if you're going to make a note in the release notes, it should probably either be vague about a number of changes that will require recreation of any bindings, or I can help you an exhaustive list.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expect any point release to have breaking changes. I feel a physics engine is a bit different than something like SDL where API stability is at a premium. For Box2D features, performance, and robustness are at a premium and there are a lot of features to come.

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.

3 participants