I am currently attempting to make a simple string simulation. The purpose is to just look like string but mine looks a bit weird and I don't know what the solution is. Does anyone have any ideas, here is the code
from pygame.locals import *
from math import sqrt
import pygame
pygame.init()
screen = pygame.display.set_mode([500, 500])
clock = pygame.time.Clock()
class Node():
def __init__(self, position, mass, initalVelocity):
self.position = position[:]
self.mass = mass
self.velocity = initalVelocity
def update(self):
self.position[0] += self.velocity[0]
self.position[1] += self.velocity[1]
class String():
def __init__(self, nodes, gravity = .981 , springConstant = 10, iterations = 1):
self.nodes = nodes[:]
self.gravity = gravity
self.springC = springConstant
self.iterations = iterations
self.setDistance = 1
def calculateForces(self):
for x in range(self.iterations):
for i in range(1, len(self.nodes), 1):
distance = sqrt((self.nodes[i].position[0] - self.nodes[i - 1].position[0]) ** 2 + (self.nodes[i].position[1] - self.nodes[i - 1].position[1]) ** 2)
force = -self.springC * (distance - self.setDistance)
force = force / self.nodes[i].mass
nDistanceVector = [(self.nodes[i].position[0] - self.nodes[i - 1].position[0]) / distance, (self.nodes[i].position[1] - self.nodes[i - 1].position[1]) / distance]
nDistanceVector = [nDistanceVector[0] * force, nDistanceVector[1] * force]
self.nodes[i].velocity[0] = 0.71 * self.nodes[i].velocity[0] + nDistanceVector[0]
self.nodes[i].velocity[1] = 0.71 * self.nodes[i].velocity[1] + (nDistanceVector[1] + self.gravity)
self.nodes[i].update()
pygame.draw.aalines(screen, [0, 0, 0], False, a)
a = [Node([250 + i * 10, 100], 100, [0, 1]) for i in range(25)]
s = String(a)
while 1:
screen.fill([255, 255, 255])
s.calculateForces()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
pygame.display.update()
clock.tick(60)`
I'm using Hooke's law and each node acts as a spring as soon as it goes beyond a certain distance from the previous node, but it just looks clunky and unrealistic. What am i missing?