Skip to content

hamed-rezaee/flutter_tearable_cloth

Repository files navigation

Flutter Cloth Simulation

This documentation provides an overview of the classes and their roles, each class plays a crucial role in achieving the interactive and dynamic cloth behavior. Use this documentation as a reference to understand the structure and purpose of each component in the project.

Cloth Simulation

Table of Contents

  1. ClothPainter Class
  2. Cloth Class
  3. Point Class
  4. Constraint Class
  5. Pointer Class
  6. Settings
  7. Inspiration
  8. License

ClothPainter Class

The ClothPainter class is responsible for painting the cloth on the canvas.

Attributes

  • cloth: An instance of the Cloth class representing the cloth.
  • showHeatmap: A boolean value indicating whether to display a heatmap effect.
  • pointMode: A PointMode enum value representing the point drawing mode.

Methods

  • paint(Canvas canvas, Size size): Paints the cloth on the canvas.
  • shouldRepaint(CustomPainter oldDelegate): Determines if a repaint is required.

Cloth Class

The Cloth class represents the cloth and is responsible for its creation and simulation.

Attributes

  • _points: A list of Point objects representing the cloth's points.

Methods

  • update(): Updates the cloth's simulation.
  • draw(Canvas canvas, Paint paint, bool showHeatmap, PointMode pointMode): Draws the cloth on the canvas.
  • Other helper methods to handle cloth simulation.

Point Class

The Point class represents a point in the cloth and handles its behavior.

Attributes

  • position: The position of the point.
  • pointer: An instance of the Pointer class representing user interaction.
  • velocity: The velocity of the point.
  • pinPosition: The position at which the point is pinned.

Methods

  • update(double delta): Updates the point's position.
  • draw(Canvas canvas, Paint paint, PointMode pointMode): Draws the point on the canvas.
  • resolveConstraints(): Resolves constraints related to the point.
  • Other methods for point behavior.

Constraint Class

The Constraint class defines a constraint between two points in the cloth.

Attributes

  • p1: The first Point involved in the constraint.
  • p2: The second Point involved in the constraint.
  • length: The desired length of the constraint.

Methods

  • resolve(): Resolves the constraint to maintain the desired length.
  • draw(Canvas canvas, Paint paint, PointMode pointMode): Draws the constraint on the canvas.

Pointer Class

The Pointer class represents user interaction with the cloth.

Attributes

  • pressed: A boolean indicating if the pointer is pressed.
  • isActionPressed: A boolean indicating if the pointer is in an action (e.g., tearing the cloth).
  • position: The current position of the pointer.
  • previousPosition: The previous position of the pointer.

Settings

The Settings class defines constants and settings for the cloth simulation.

const double canvasWidth = 800;
const double canvasHeight = 600;
const double clothWidth = 80;
const double clothHeight = 60;

const double gravity = 1200;
const int physicsAccuracy = 3;
const double friction = 0.98;

const double spacing = 5;
const double tearDistance = 50;
const Offset start = Offset(canvasWidth * 0.5 - (clothWidth * spacing) * 0.5, 20);

const double pointerInfluence = 40;
const double pointerCut = 7;

Inspiration

This project is inspired by Tearable Cloth by Dissimulate.

License

This project is licensed under the MIT License - see the LICENSE file for details.