Set userdata on the body instead of on fixture

This commit is contained in:
Lukas Klingsbo
2020-05-03 12:59:50 +02:00
parent ac6abfcd24
commit 94cd8b6f2b
3 changed files with 12 additions and 4 deletions

View File

@ -18,6 +18,14 @@ A simple `Box2DGame` implementation example can be seen in the [examples folder]
If you are using `Box2DGame` you can take advantage of its way of handling contacts between two `BodyComponent`s.
When creating the body definition for your `BodyComponent` make sure that you set the userdata to the current object, otherwise it will not be possible to detect collisions.
Like this:
```dart
final bodyDef = BodyDef()
// To be able to determine object in collision
..setUserData(this);
```
To do this you have to make an implementation of `ContactCallback` where you set which two types that it should react when they come in contact.
If you have two `BodyComponent`s named Ball and Wall and you want to do something when they come in contact you would do like this:

View File

@ -38,14 +38,14 @@ class Ball extends BodyComponent {
shape.radius = radius;
final fixtureDef = FixtureDef()
// To be able to determine object in collision
..setUserData(this)
..shape = shape
..restitution = 1.0
..density = 1.0
..friction = 0.1;
final bodyDef = BodyDef()
// To be able to determine object in collision
..setUserData(this)
..position = position
..type = BodyType.DYNAMIC;

View File

@ -40,8 +40,8 @@ class ContactCallbacks extends ContactListener {
}
void _maybeCallback(Contact contact, ContactCallback callback, Function f) {
final Object a = contact.fixtureA.userData;
final Object b = contact.fixtureB.userData;
final Object a = contact.fixtureA.getBody().userData;
final Object b = contact.fixtureB.getBody().userData;
final ContactTypes wanted = callback.types;
if (wanted.match(a, b) || (wanted.has(BodyComponent) && wanted.hasOne(a, b))) {