I have a simple RectangleHitbox (sort of wall) and CircleHitbox (player). When they collide each other I can see through the "onCollision" method which points are involved. However if I move the player away the method keeps on get called (printing out the same points) and if I collide again the player to the wall, still the same points from the first collision are returned. I was expecting that as soon as the player moved away from the wall the "onCollision" method was stopped to get called and display new collision points on a new collision.
class Player extends PositionComponent with CollisionCallbacks, HasGameRef<MainGame> {
Offset direction = Offset.zero;
bool isCollided = false;
Vector2 playerStartPosition;
Player(this.playerStartPosition): super(
position: playerStartPosition,
anchor: Anchor.topLeft
);
@override
Future<void> onLoad() async {
// super.onLoad();
add(CircleHitbox(radius: 2)
..paint = BasicPalette.yellow.paint()
..renderShape = true
);
super.onLoad();
}
@override
void onCollision(Set<Vector2> intersectionPoints, PositionComponent other) {
super.onCollision(intersectionPoints, other);
print('Collision ON');
print(intersectionPoints);
if (other is Level && !isCollided) {
isCollided = true;
game.increaseScore();
}
}
@override
void onCollisionStart(Set<Vector2> intersectionPoints, PositionComponent other) {
super.onCollisionStart(intersectionPoints, other);
print('Collision START');
isCollided = true;
if (other is Level) {
game.increaseScore();
}
}
@override
void onCollisionEnd(PositionComponent other) {
super.onCollisionEnd(other);
print('Collision END');
isCollided = false;
// if (other is Level) {
// game.increaseScore();
// }
}
@override
void update(double delta) {
super.update(delta);
if (!isCollided) {
position.add(direction.toVector2());
}
}
}
class Level extends PositionComponent with CollisionCallbacks {
@override
Future<void> onLoad() async {
// super.onLoad();
add(RectangleHitbox(
position: Vector2(80.0, 10.0), size: Vector2(2.0, 250.0), angle:0,
collisionType: CollisionType.passive
)
..paint = BasicPalette.blue.paint()
..renderShape = true);
super.onLoad();
}
}
Am I missing something?
I just tried printing the
intersectionPoints
in the Circles demo and it works just like intended, the list of points update meanwhile the circles move through each other.This was the only addition I did to that example:
I can see in your code that you have a variable that sets
isCollided
totrue
when you start colliding with anything and in your player'supdate
method you say that the player should not move ifisCollided
is true, so maybe that is why you are getting the same intersection points? It's hard to say, because the part of the code that controls movement is not included.