@override
Widget build(BuildContext context) {
floor = MediaQuery.of(context).size.height;
return GestureDetector(
onPanStart: _onPanStart,
onPanUpdate: _onPanUpdate,
onPanEnd: _onPanEnd,
child: Scaffold(
body: Center(
child: Stack(
children: [
Positioned.fill(
child: Image.asset(
'assets/background.png',
fit: BoxFit.fitWidth,
),
),
Positioned(
left: MediaQuery.of(context).size.width / 2 - AppConstants.shieldWidth / 2,
top: MediaQuery.of(context).size.height * 0.2,
child: Image.asset(
'assets/shield.png',
width: AppConstants.shieldWidth,
height: AppConstants.shieldHeight,
fit: BoxFit.fitWidth,
),
),
Positioned(
left: ballPositionX - AppConstants.ballSize / 2,
top: ballPositionY - AppConstants.ballSize / 2,
child: Transform.rotate(
angle: (rotationAngle + previousVelocityMagnitude) / 100,
child: const Image(
width: AppConstants.ballSize,
height: AppConstants.ballSize,
image: AssetImage('assets/ball.png'),
),
),
),
Positioned(
left: MediaQuery.of(context).size.width / 2 - AppConstants.ringWidth / 2,
top: MediaQuery.of(context).size.height * 0.2 + 112,
child: const Image(
width: AppConstants.ringWidth,
height: AppConstants.ringHeight,
image: AssetImage('assets/ring.png'),
),
),
Positioned(
left: MediaQuery.of(context).size.width / 2 - AppConstants.ringWidth / 2 + 4,
top: MediaQuery.of(context).size.height * 0.2 + 117,
child: Container(
height: 8,
width: 4,
color: Colors.black,
),
),
Positioned(
left: MediaQuery.of(context).size.width / 2 + AppConstants.ringWidth / 2 - 6,
top: MediaQuery.of(context).size.height * 0.2 + 117,
child: Container(
height: 8,
width: 4,
color: Colors.black,
),
),
],
),
),
),
);
}
}
I have Flutter app that simulates the ball behavior. Also i have a 2D projection of basketball ring. Imagine, you are looking at the basketball ring staying in front of it. You see just a projection of ring that looks like flat line. That what i have in my app. For better illustration I added two black containers, emulating edges of the ring. I want the ball to collide with the left and right edges of the ring. And if the ball moving from the above of the ring passes between this two container I want to print a message in console (to consider this as a score).
To detect the collision with the left and right edges of the ring, you can use the ball's position and the position and dimensions of the ring. Here's how you can modify the _updateBall method to detect the collision and print a message in the console if the ball passes between the two black containers:
void _updateBall(double delta) {
if (!isDragging) {
double velocityMagnitude =
(ballVelocityX.abs().ceil() + ballVelocityY.abs().ceil()).toDouble();
double deltaVelocity = velocityMagnitude - previousVelocityMagnitude;
previousVelocityMagnitude = velocityMagnitude;
setState(() {
ballPositionX += ballVelocityX * delta / 100;
ballPositionY += ballVelocityY * delta / 100;
if (ballPositionY + AppConstants.ballSize / 2 >= floor) {
// Ball hits the floor
ballPositionY = floor - AppConstants.ballSize / 2;
ballVelocityX *= friction;
if ((ballVelocityY * ballVelocityY) < 4) {
ballVelocityY = 0;
} else {
ballVelocityY *= -restitution;
}
} else if (ballPositionY - AppConstants.ballSize / 2 <= ceiling) {
// Ball hits the ceiling
ballPositionY = ceiling + AppConstants.ballSize / 2;
ballVelocityY *= -restitution;
} else if ((ballVelocityY * ballVelocityY) + (ballVelocityX * ballVelocityX) < 5) {
// Ball comes to a stop
ballVelocityY = 0;
ballVelocityX = 0;
} else {
// Ball is moving
ballVelocityY += gravity * delta / 100;
// Detect collision with the left and right edges of the ring
double ringLeft = MediaQuery.of(context).size.width / 2 - AppConstants.ringWidth / 2;
double ringRight = MediaQuery.of(context).size.width / 2 + AppConstants.ringWidth / 2;
if (ballPositionY - AppConstants.ballSize / 2 <=
MediaQuery.of(context).size.height * 0.2 + AppConstants.ringHeight &&
ballPositionX >= ringLeft && ballPositionX <= ringRight) {
// Ball passes through the ring
if (ballVelocityY < 0) {
print('Score!');
}
} else {
// Detect collision with the sides of the ring
if (ballPositionX < AppConstants.ballSize / 2) {
// Ball hits the left side of the ring
ballPositionX = AppConstants.ballSize / 2;
ballVelocityX *= -restitution;
ballVelocityY *= restitution;
} else if (ballPositionX > MediaQuery.of(context).size.width - AppConstants.ballSize / 2) {
// Ball hits the right side of the ring
ballPositionX = MediaQuery.of(context).size.width - AppConstants.ballSize / 2;
ballVelocityX *= -restitution;
ballVelocityY *= restitution;
}
}
}
rotationAngle += deltaVelocity;
});
}
}
In the code above, we first define the ringLeft and ringRight variables using the position and dimensions of the ring. Then, we check if the ball passes through the ring by comparing the ball's y position and x position with the top of the ring and the ringLeft and ringRight values respectively. If the ball passes through the ring and is moving upwards (i.e. ballVelocityY is negative), we print the "Score!" message in the console.
If the ball does not pass through the ring, we continue to check for collision with the left and right edges of the ring as before.
To fix the collision detection with the ring edges, we can add another condition to check if the ball's y position is within the height of the ring and ball's x position is outside of the left and right edges of the ring. If this condition is true, then the ball has hit the ring edge, and we can reflect the ball's velocity as before.
To fix the score message being printed only when the ball passes through the ring from above, we can add another condition to check if ballVelocityY is negative. This condition ensures that the ball is moving upwards when it passes through the ring, which means that it was thrown from below and passed through the ring.
Here's the modified _updateBall method with the fixes:
void _updateBall(double delta) {
if (!isDragging) {
double velocityMagnitude =
(ballVelocityX.abs().ceil() + ballVelocityY.abs().ceil()).toDouble();
double deltaVelocity = velocityMagnitude - previousVelocityMagnitude;
previousVelocityMagnitude = velocityMagnitude;
setState(() {
ballPositionX += ballVelocityX * delta / 100;
ballPositionY += ballVelocityY * delta / 100;
if (ballPositionY + AppConstants.ballSize / 2 >= floor) {
// Ball hits the floor
ballPositionY = floor - AppConstants.ballSize / 2;
ballVelocityX *= friction;
if ((ballVelocityY * ballVelocityY) < 4) {
ballVelocityY = 0;
} else {
ballVelocityY *= -restitution;
}
} else if (ballPositionY - AppConstants.ballSize / 2 <= ceiling) {
// Ball hits the ceiling
ballPositionY = ceiling + AppConstants.ballSize / 2;
ballVelocityY *= -restitution;
} else if ((ballVelocityY * ballVelocityY) + (ballVelocityX * ballVelocityX) < 5) {
// Ball comes to a stop
ballVelocityY = 0;
ballVelocityX = 0;
} else {
// Ball is moving
ballVelocityY += gravity * delta / 100;
// Detect collision with the left and right edges of the ring
double ringLeft = MediaQuery.of(context).size.width / 2 - AppConstants.ringWidth / 2;
double ringRight = MediaQuery.of(context).size.width / 2 + AppConstants.ringWidth / 2;
if (ballPositionY - AppConstants.ballSize / 2 <=
MediaQuery.of(context).size.height * 0.2 + AppConstants.ringHeight &&
ballPositionX >= ringLeft &&
ballPositionX <= ringRight) {
// Ball passes through the ring
if (ballVelocityY < 0) {
print('Score!');
}
} else if ((ballPositionY + AppConstants.ballSize / 2 >=
MediaQuery.of(context).size.height * 0.2 &&
ballPositionX < ringLeft) ||
(ballPositionY + AppConstants.ballSize / 2 >=
MediaQuery.of(context).size.height * 0.2 &&
ballPositionX > ringRight)) {
// Ball hits the left or right edge of the ring
ballVelocityX *= -restitution;
ballVelocityY *= restitution;
if (ballPositionX < ringLeft) {
ballPositionX = ringLeft - AppConstants.ballSize / 2;
} else {
ballPositionX = ringRight + AppConstants.ballSize / 2;
}
} else {
// Ball does not hit the ring or its edges
if (ballPositionX < AppConstants.ballSize / 2) {
// Ball hits the left side of the screen
ballPositionX = AppConstants.ballSize / 2;
ballVelocityX *= -restitution;
ballVelocityY *= restitution;
} else if (ballPositionX > MediaQuery.of(context).size.width - AppConstants.ballSize / 2) {
// Ball hits the right side of the screen
ballPositionX = MediaQuery.of(context).size.width - AppConstants.ballSize / 2;
ballVelocityX *= -restitution;
ballVelocityY *= restitution;
}
}
}
rotationAngle += deltaVelocity;
});
}
}
In the code above, we first add a new condition to check if the ball's y position is within the height of the ring and x position is outside the left and right edges of the ring. If this condition is true, then we reflect the ball's velocity and set the ball's position to be at the edge of the ring.
We then modify the condition that checks if the ball passes through the ring to only print the "Score!" message if ballVelocityY is negative.
With these modifications, the ball should now collide with the left and right edges of the ring and the "Score!" message should only be printed when the ball passes through the ring from below.