import 'package:flutter/material.dart'; class BouncingBallApp extends StatefulWidget {…
- User
import 'package:flutter/material.dart';
class BouncingBallApp extends StatefulWidget { const BouncingBallApp({Key? key}) : super(key: key);
@override BouncingBallAppState createState() => BouncingBallAppState(); }
class BouncingBallAppState extends State with SingleTickerProviderStateMixin { double ballDiameter = 50; double floor = 0; double ceiling = 1.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; late AnimationController _animationController; bool isDragging = false;
@override void initState() { super.initState(); _animationController = AnimationController( vsync: this, duration: const Duration(minutes: 1), // set the duration to one minute )..repeat(); _animationController.addListener(() { _updateBall(_animationController.lastElapsedDuration!.inMilliseconds.toDouble()); }); }
void _onPanStart(DragStartDetails details) { setState(() { isDragging = true; }); }
void _onPanUpdate(DragUpdateDetails details) { setState(() { ballVelocityX += details.delta.dx / 2; ballVelocityY += -details.delta.dy / 2; }); }
void _onPanEnd(DragEndDetails details) { setState(() { isDragging = false; ballVelocityX *= friction; ballVelocityY *= friction; }); }
void _updateBall(double timeStamp) { double delta = timeStamp / 1000;
codeif (!isDragging) { setState(() { ballPositionX += ballVelocityX * delta; ballPositionY += ballVelocityY * delta; double currentFloor = MediaQuery.of(context).size.height - ballDiameter / 2; if (ballPositionY > currentFloor) { ballPositionY = currentFloor; ballVelocityY *= -restitution; ballVelocityX *= friction; } else if (ballPositionY < ceiling + ballDiameter / 2) { ballPositionY = ceiling + ballDiameter / 2; ballVelocityY *= -restitution; ballVelocityX *= friction; } else { ballVelocityY += gravity * delta; } if (ballPositionX < ballDiameter / 2) { ballPositionX = ballDiameter / 2; ballVelocityX *= -restitution; } else if (ballPositionX > MediaQuery.of(context).size.width - ballDiameter / 2) { ballPositionX = MediaQuery.of(context).size.width - ballDiameter / 2; ballVelocityX *= -restitution; } }); } - Assistant
- User
Yeah, splendid. Can you swap the floor with the ceiling, cuz ball now fall on the top of the screen))
- Assistant
- User
Okay, great! But why ball loose it speed even if it's just bouncing on the floor? Can u retain impulse lose from the wall collisions, but make ball behaviour more realistic?
- Assistant
Good observation! In real-world physics, every collision between objects causes some energy loss due to various factors, such as deformation of the objects, sound, heat, etc. This energy loss affects the velocity of the colliding objects after the collision.
- User
I don't want the ball to slow down with time, and it's jumping to high when bouncing on the floor even if its impulse is very low
- Assistant
Got it! In that case, you can adjust the values of
gravity,restitution, andfrictionto control the ball's behavior and achieve the desired effect.To prevent the ball from losing its speed over time, you can set the
frictionvalue to 1.0, which means there is no energy loss due to friction. You can also reduce therestitutionvalue to make the ball bounce less and lose less energy with each bounce. - User
Okay, we almost there! I changed all the 1000 denominators to 100 and it's became way more realistic! The one problem i have got, is that ball bouncing endlessly in the end, when it's suppose to finally stop.
- Assistant
I'm glad to hear that the modifications helped! As for the issue of the ball bouncing endlessly, this could be happening because the ball's velocity is very small and its position is oscillating around the floor due to numerical errors in the simulation. However, this can be fixed by adding a threshold for the ball's velocity below which it should be stopped.
Here's an updated
_updateBall()method that implements this threshold:code