Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # cloth_simulation.py
- # a very simplified cloth simulation
- # do not import numpy
- # do not import PyQt5
- # do not import pygame
- # do not include os
- # do not include sleep
- # do not include class
- # do not include win32com
- # do not import gui
- # do not import ttk
- # do not import matplotlib
- from tkinter import *
- from time import *
- from random import *
- from math import *
- # window size
- nx = 500
- ny = 500
- # mouse variables
- xm = None
- ym = None
- win = Tk()
- win.title("Cloth Simulation")
- canvas = Canvas(win)
- canvas.pack()
- # set parameters
- xy_max = [0, 0]
- xy_min = [0, 0]
- cloth = {}
- vel = {} # velocities
- mdrag = {} # mouse drag
- prox = {} # proximity
- cloth = {}
- for iy in range(20, ny-60, nsteps):
- for ix in range(20, nx, nsteps):
- if iy == 20:
- TRC = i
- cloth[i] = (ix,iy)
- prox[i] = int(ix/vp), int(iy/vp)
- vel[i] = 0, 0
- mdrag[i] = 0, 0
- i += 1
- # create velocity list
- vx = [0,0]
- vy = [0,0]
- # define cloth dynamics
- def cloth_dynamics(t, xi, yi, vx, vy, r):
- if ((xi - vx[0]) < 0):
- xi = vx[0]
- elif ((xi - vx[1]) < 0):
- xi = vx[1]
- if ((yi - vy[0]) < 0):
- yi = vy[0]
- elif ((yi - vy[1]) < 0):
- yi = vy[1]
- # calculate
- xi = (xi - vx[0])/r
- yi = (yi - vy[0])/r
- # update
- vx[0] = xi
- vy[0] = yi
- return (xi, yi)
- # define cloth motion
- def cloth_motion(t, xi, yi, vx, vy):
- if ((xi-vx[0]) < 0):
- vx[0] = xi
- elif ((xi-vx[1]) < 0):
- vx[1] = xi
- if ((yi-vy[0]) < 0):
- vy[0] = yi
- elif ((yi-vy[1]) < 0):
- vy[1] = yi
- # calculate
- xi = (xi-vx[0])/r
- yi = (yi-vy[0])/r
- # update
- vx[0] = xi
- vy[0] = yi
- return (xi, yi)
- # define mouse cloth drag
- def mouse_cloth_drag(x, y):
- if (x > 0):
- x -= xm
- else:
- x += xm
- if (y > 0):
- y -= ym
- else:
- y += ym
- # update
- if (x > 0):
- x -= xm
- else:
- x += xm
- if (y > 0):
- y -= ym
- else:
- y += ym
- return x, y
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement