Question: Why do I get this strange behaviour ? How can I fix it ?
I made a very simple example sketch using the Toxiclibs library. I also added some step-by-step explanations to be as clear as possible.
Any suggestions are more than welcomed !
add_library('verletphysics')
add_library('toxiclibscore')
from toxi.physics2d import VerletParticle2D
from toxi.physics2d import VerletPhysics2D
from toxi.physics2d.behaviors import GravityBehavior
pointlist = []
t = 0
def setup():
global physics
size(800, 800, P2D)
smooth(8)
physics = VerletPhysics2D()
#Adding gravity
physics.addBehavior(GravityBehavior(Vec2D(0, 1)))
#Creating particles and adding them to physics engine
for e in range(31):
p = VerletParticle2D(width/2 - 15 * 20 + e*20, height/2)
pointlist.append(p)
physics.addParticle(p)
#Connecting particles with springs
for i in range(len(pointlist)-1):
a = pointlist[i]
b = pointlist[i+1]
s = VerletSpring2D(a, b, 15, .1)
physics.addSpring(s)
#Locking particles on the edges (far left and far right)
pointlist[0].lock()
pointlist[-1].lock()
def draw():
global t, poitlist
background(255)
#Running the physics engine
physics.update()
#drawing points
strokeWeight(8)
for i, p in enumerate(pointlist):
#if middle point -> change height and add it to pointlist
if i == 15:
y = height/2 + sin(t*.05) * 380
pointlist[i] = VerletParticle2D(p.x(), y)
else:
y = p.y()
stroke(255, 30, 30) if i == 15 else stroke(0)
point(p.x(), y)
#drawing springs
strokeWeight(.6)
for i in range(len(pointlist)-1):
line(pointlist[i].x(), pointlist[i].y(), pointlist[i+1].x(), pointlist[i+1].y())
t += 1
You’re creating newVerletParticle2D, not mutating existing 1s!
Check whether a VerletParticle2D got some setting method called set() or something like that.
Or… given you’re only interested in its y field, you may attempt to change it directly.
As @GoToLoop suggested, applying the addVelocity() method to the moving point was the way to go.
Thank you !
add_library('verletphysics')
add_library('toxiclibscore')
from toxi.physics2d import VerletParticle2D
from toxi.physics2d import VerletPhysics2D
from toxi.physics2d.behaviors import GravityBehavior
pointlist = []
t = 0
def setup():
global physics
size(800, 800, P2D)
smooth(8)
physics = VerletPhysics2D()
#Adding gravity
physics.addBehavior(GravityBehavior(Vec2D(0, 1)))
#Creating particles and adding them to physics engine
for e in range(31):
p = VerletParticle2D(Vec2D(width/2 - 15 * 20 + e*20, height/2))
pointlist.append(p)
physics.addParticle(p)
#Connecting particles with springs
for i in range(len(pointlist)-1):
a = pointlist[i]
b = pointlist[i+1]
s = VerletSpring2D(a, b, 15, .1)
physics.addSpring(s)
#Locking particles on the edges (far left and far right)
pointlist[0].lock()
pointlist[-1].lock()
def draw():
global t
background(255)
#Running the physics engine
physics.update()
#drawing points
strokeWeight(8)
for i, p in enumerate(pointlist):
#if middle point -> change height and add it to pointlist
if i == 15:
y = height/2 + sin(t * .1) * 10
p.addVelocity(Vec2D(0, y - height/2))
stroke(255, 30, 30) if i == 15 else stroke(0)
point(p.x(), p.y())
#drawing springs
strokeWeight(.6)
for i in range(len(pointlist)-1):
line(pointlist[i].x(), pointlist[i].y(), pointlist[i+1].x(), pointlist[i+1].y())
t += 1