Advertisement
here2share

# setup4cloth_sim.py

Jul 7th, 2022 (edited)
1,016
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.03 KB | None | 0 0
  1. # cloth_simulation.py
  2. # a very simplified cloth simulation
  3.  
  4. # do not import  numpy
  5. # do not import  PyQt5
  6. # do not import pygame
  7. # do not include os
  8. # do not include sleep
  9. # do not include class  
  10. # do not include win32com  
  11. # do not import gui
  12. # do not import ttk
  13. # do not import matplotlib
  14. from tkinter import *
  15. from time import *
  16. from random import *
  17. from math import *
  18.  
  19. # window size
  20. nx = 500
  21. ny = 500
  22.  
  23. # mouse variables
  24. xm = None
  25. ym = None
  26.  
  27. win = Tk()
  28. win.title("Cloth Simulation")
  29.  
  30. canvas = Canvas(win)
  31. canvas.pack()  
  32.  
  33. # set parameters  
  34. xy_max = [0, 0]  
  35. xy_min = [0, 0]
  36. cloth = {}
  37. vel = {} # velocities
  38. mdrag = {} # mouse drag
  39. prox = {} # proximity
  40. cloth = {}
  41. for iy in range(20, ny-60, nsteps):
  42.  for ix in range(20, nx, nsteps):
  43.   if iy == 20:
  44.    TRC = i
  45.   cloth[i] = (ix,iy)
  46.   prox[i] = int(ix/vp), int(iy/vp)
  47.   vel[i] = 0, 0
  48.   mdrag[i] = 0, 0
  49.   i += 1
  50.  
  51.  # create velocity list
  52.  vx = [0,0]
  53.  vy = [0,0]
  54.  
  55.  # define cloth dynamics
  56.  def cloth_dynamics(t, xi, yi, vx, vy, r):
  57.   if ((xi - vx[0]) < 0):
  58.    xi = vx[0]
  59.   elif ((xi - vx[1]) < 0):
  60.    xi = vx[1]
  61.   if ((yi - vy[0]) < 0):
  62.    yi = vy[0]
  63.   elif ((yi - vy[1]) < 0):
  64.    yi = vy[1]
  65.    
  66.    # calculate
  67.    xi = (xi - vx[0])/r
  68.    yi = (yi - vy[0])/r
  69.    
  70.    # update
  71.    vx[0] = xi
  72.    vy[0] = yi
  73.    
  74.    return (xi, yi)
  75.  
  76.  # define cloth motion
  77.  def cloth_motion(t, xi, yi, vx, vy):
  78.   if ((xi-vx[0]) < 0):
  79.    vx[0] = xi
  80.   elif ((xi-vx[1]) < 0):
  81.    vx[1] = xi
  82.   if ((yi-vy[0]) < 0):
  83.    vy[0] = yi
  84.   elif ((yi-vy[1]) < 0):
  85.    vy[1] = yi
  86.    
  87.    # calculate
  88.    xi = (xi-vx[0])/r
  89.    yi = (yi-vy[0])/r
  90.    
  91.    # update
  92.    vx[0] = xi
  93.    vy[0] = yi
  94.    
  95.    return (xi, yi)
  96.  
  97.  # define mouse cloth drag
  98.  def mouse_cloth_drag(x, y):
  99.   if (x > 0):
  100.    x -= xm
  101.   else:
  102.    x += xm
  103.   if (y > 0):
  104.    y -= ym
  105.   else:
  106.    y += ym
  107.    
  108.  # update
  109.  if (x > 0):
  110.   x -= xm
  111.  else:
  112.   x += xm
  113.  if (y > 0):
  114.   y -= ym
  115.  else:
  116.   y += ym
  117.  
  118.  return x, y
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement