mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-03 04:18:25 +08:00
Set userdata on the body instead of on fixture
This commit is contained in:
@ -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.
|
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.
|
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:
|
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:
|
||||||
|
|
||||||
|
|||||||
@ -38,14 +38,14 @@ class Ball extends BodyComponent {
|
|||||||
shape.radius = radius;
|
shape.radius = radius;
|
||||||
|
|
||||||
final fixtureDef = FixtureDef()
|
final fixtureDef = FixtureDef()
|
||||||
// To be able to determine object in collision
|
|
||||||
..setUserData(this)
|
|
||||||
..shape = shape
|
..shape = shape
|
||||||
..restitution = 1.0
|
..restitution = 1.0
|
||||||
..density = 1.0
|
..density = 1.0
|
||||||
..friction = 0.1;
|
..friction = 0.1;
|
||||||
|
|
||||||
final bodyDef = BodyDef()
|
final bodyDef = BodyDef()
|
||||||
|
// To be able to determine object in collision
|
||||||
|
..setUserData(this)
|
||||||
..position = position
|
..position = position
|
||||||
..type = BodyType.DYNAMIC;
|
..type = BodyType.DYNAMIC;
|
||||||
|
|
||||||
|
|||||||
@ -40,8 +40,8 @@ class ContactCallbacks extends ContactListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _maybeCallback(Contact contact, ContactCallback callback, Function f) {
|
void _maybeCallback(Contact contact, ContactCallback callback, Function f) {
|
||||||
final Object a = contact.fixtureA.userData;
|
final Object a = contact.fixtureA.getBody().userData;
|
||||||
final Object b = contact.fixtureB.userData;
|
final Object b = contact.fixtureB.getBody().userData;
|
||||||
final ContactTypes wanted = callback.types;
|
final ContactTypes wanted = callback.types;
|
||||||
|
|
||||||
if (wanted.match(a, b) || (wanted.has(BodyComponent) && wanted.hasOne(a, b))) {
|
if (wanted.match(a, b) || (wanted.has(BodyComponent) && wanted.hasOne(a, b))) {
|
||||||
|
|||||||
Reference in New Issue
Block a user