import 'dart:async'; import 'package:basketball/constants.dart'; import…
- User
import 'dart:async';
import 'package:basketball/constants.dart'; import 'package:flutter/material.dart';
class BouncingBallApp extends StatefulWidget { const BouncingBallApp({Key? key}) : super(key: key);
@override BouncingBallAppState createState() => BouncingBallAppState(); }
class BouncingBallAppState extends State { double floor = 1.0; double ceiling = 0; double ballPositionX = 50; double ballPositionY = 300; double ballVelocityX = 0; double ballVelocityY = 0; double gravity = 9.8; double restitution = 0.8; double friction = 0.98; bool isDragging = false;
@override void initState() { super.initState(); Timer.periodic(const Duration(milliseconds: 16), (timer) { _updateBall(16); }); }
void _onPanStart(DragStartDetails details) { setState(() { isDragging = true; }); }
void _onPanUpdate(DragUpdateDetails details) { setState(() { ballVelocityX += details.delta.dx; ballVelocityY += -details.delta.dy; }); }
void _onPanEnd(DragEndDetails details) { setState(() { isDragging = false; ballVelocityX *= friction; ballVelocityY *= friction; }); }
void _updateBall(double delta) { if (!isDragging) { setState(() { ballPositionX += ballVelocityX * delta / 100; ballPositionY += ballVelocityY * delta / 100;
double currentCeiling = 0 + AppConstants.ballSize / 2; if (ballPositionY < currentCeiling) { ballPositionY = currentCeiling; ballVelocityY *= -restitution; ballVelocityX *= restitution; } else if (ballPositionY > floor - AppConstants.ballSize / 2) { ballPositionY = floor - AppConstants.ballSize / 2; ballVelocityY *= -restitution; ballVelocityX *= restitution; } else { ballVelocityY += gravity * delta / 100; } if (ballPositionX < AppConstants.ballSize / 2) { ballPositionX = AppConstants.ballSize / 2; ballVelocityX *= -restitution; ballVelocityY *= restitution; } else if (ballPositionX > MediaQuery.of(context).size.width - AppConstants.ballSize / 2) { ballPositionX = MediaQuery.of(context).size.width - AppConstants.ballSize / 2; ballVelocityX *= -restitution; ballVelocityY *= restitution; } }); }}
@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: Container( color: Colors.grey[300], child: const SizedBox.expand(), ), ), Positioned( left: ballPositionX - AppConstants.ballSize / 2, top: ballPositionY - AppConstants.ballSize / 2, child: const Image( width: AppConstants.ballSize, height: AppConstants.ballSize, image: AssetImage('assets/ball.png'), )), ], ), ), ), ); } }
void main() => runApp(MaterialApp(home: BouncingBallApp(key: UniqueKey())));
Hello! I have a Flutter app, that simulates the bouncing ball behavior (code of the app is mentioned above). Right now I have an issue, that ball is jumping endlessly on the bottom of screen, when its must to just stop. Can you fix it? Also, can you add a restriction, that not allow user to adjust ball speed if current ball speed is higher than 3?
- Assistant
To make the ball stop bouncing on the bottom of the screen, we can add a threshold value to check if the ball is close enough to the ground to stop bouncing. We can modify the
_updateBallmethod as follows:void _updateBall(double delta) { if (!isDragging) { setState(() { ballPositionX += ballVelocityX * delta / 100; ballPositionY += ballVelocityY * delta / 100; double currentCeiling = 0 + AppConstants.ballSize / 2; double threshold = 5; // add a threshold value to stop bouncing if (ballPositionY < currentCeiling) { ballPositionY = currentCeiling; ballVelocityY *= -restitution; ballVelocityX *= restitution; } else if (ballPositionY > floor - AppConstants.ballSize / 2 - threshold) { ballPositionY = floor - AppConstants.ballSize / 2 - threshold; ballVelocityY = 0; // stop the ball from bouncing ballVelocityX *= friction; } else { ballVelocityY += gravity * delta / 100; } if (ballPositionX < AppConstants.ballSize / 2) { ballPositionX = AppConstants.ballSize / 2; ballVelocityX *= -restitution; ballVelocityY *= restitution; } else if (ballPositionX > MediaQuery.of(context).size.width - AppConstants.ballSize / 2) { ballPositionX = MediaQuery.of(context).size.width - AppConstants.ballSize / 2; ballVelocityX *= -restitution; ballVelocityY *= restitution; } }); } } - User
Now ball refuse to jump at all
- Assistant
I apologize for the confusion. I made an error in my previous answer. Instead of setting
ballVelocityY = 0to stop the ball from bouncing, we should setballVelocityY *= -restitution. This will cause the ball to bounce just enough to slow down and eventually stop bouncing.Here's the updated
_updateBallmethod with the correct change:void _updateBall(double delta) { if (!isDragging) { setState(() { ballPositionX += ballVelocityX * delta / 100; ballPositionY += ballVelocityY * delta / 100; double currentCeiling = 0 + AppConstants.ballSize / 2; double threshold = 5; if (ballPositionY < currentCeiling) { ballPositionY = currentCeiling; ballVelocityY *= -restitution; ballVelocityX *= restitution; } else if (ballPositionY > floor - AppConstants.ballSize / 2 - threshold) { ballPositionY = floor - AppConstants.ballSize / 2 - threshold; ballVelocityY *= -restitution; // change this line ballVelocityX *= friction; } else { ballVelocityY += gravity * delta / 100; } if (ballPositionX < AppConstants.ballSize / 2) { ballPositionX = AppConstants.ballSize / 2; ballVelocityX *= -restitution; ballVelocityY *= restitution; } else if (ballPositionX > MediaQuery.of(context).size.width - AppConstants.ballSize / 2) { ballPositionX = MediaQuery.of(context).size.width - AppConstants.ballSize / 2; ballVelocityX *= -restitution; ballVelocityY *= restitution; } }); } } - User
No, actually it didn't help and now there is a few pixels on the bottom that i want to remove
- Assistant
I apologize for the confusion. Here's an updated
_updateBallmethod that fixes the issue with the ball not stopping bouncing, and also removes the unwanted pixels at the bottom of the screen:void _updateBall(double delta) { if (!isDragging) { setState(() { ballPositionX += ballVelocityX * delta / 100; ballPositionY += ballVelocityY * delta / 100; double currentCeiling = 0 + AppConstants.ballSize / 2; double threshold = 5; if (ballPositionY < currentCeiling) { ballPositionY = currentCeiling; ballVelocityY *= -restitution; ballVelocityX *= restitution; } else if (ballPositionY > floor - AppConstants.ballSize / 2 - threshold && ballVelocityY > 0) { ballPositionY = floor - AppConstants.ballSize / 2 - threshold; ballVelocityY *= -restitution; ballVelocityY = ballVelocityY.abs() < 1 ? 0 : ballVelocityY; ballVelocityX *= friction; } else { ballVelocityY += gravity * delta / 100; } if (ballPositionX < AppConstants.ballSize / 2) { ballPositionX = AppConstants.ballSize / 2; ballVelocityX *= -restitution; ballVelocityY *= restitution; } else if (ballPositionX > MediaQuery.of(context).size.width - AppConstants.ballSize / 2) { ballPositionX = MediaQuery.of(context).size.width - AppConstants.ballSize / 2; ballVelocityX *= -restitution; ballVelocityY *= restitution; } }); } } - User
It's still bouncing and threshold is still visible:(((
- Assistant
I apologize for the continued troubles. Let's try another approach to addressing both issues.
To prevent the ball from bouncing indefinitely, we can add a check to see if the current velocity of the ball is less than a certain value, and if it is, we can set the velocity to zero and stop the ball from bouncing.
To remove the pixels at the bottom of the screen, we can add a check to see if the ball is within a certain distance from the ground, and if it is, we can stop it from bouncing and set its location to be right on the ground.
- User
Okay, now it works, but somehow ceiling now is far above the top of the screen
- Assistant
I apologize for the mistake. Since the ball should start at the top of the screen, we should adjust the value of
currentCeilingto be the top of the screen instead of 0:double currentCeiling = AppConstants.ballSize / 2;With this change, the ball should start at the top of the screen and the ceiling should be just above it.
I hope this helps, and please let me know if there's anything else I can help you with!
- User
Maybe it will help, but it's not clear where to put this statement. Can you show the full code?
- Assistant