My Python is not working like I need it to

  • Context: Python 
  • Thread starter Thread starter jrgoodin
  • Start date Start date
  • Tags Tags
    Python
Join the discussion
Registration is free. Start your own thread to ask a follow-up.
3 replies · 3K views
jrgoodin
Messages
2
Reaction score
0
This is what I have, and for some reason it won't move...I trust all you smart people could point me in the right direction...thanks

from visual import *
from visual.graph import *


relaxedlength = vector(.60,0,0) # length of spring when it isn't stretched or compressed
spring = helix(pos=(-.75,0,0),axis=relaxedlength, radius=.1,coils=8,thickness=.01,color=color.green)
spring.constant = 25 # k

weight = box(pos=(0,0,0),size=(.3,.3,.3),color=color.yellow)
weight.mass = 10 # kg
weight.velocity = vector(.30,0,0)
weight.acceleration = vector(.01,0,0)
weight.force = vector(0.3,0,0)

frictionlessSurface = box(size=(2,.02,.5),pos=(0,-.16,0))
wall = box(size=(.04,.5,.3),pos=(-.77,.1,0),color=color.red)
spring.displacement = weight.pos # the weight starts at (0,0,0) and is attached to spring
spring.axis = relaxedlength + spring.displacement


dt = .01 # seconds
t = 0 # total time
while 1:
rate(100)
t += dt

# Calculate the spring force using Hooke's Law
spring.force = -spring.constant * spring.displacement

# The spring force acts on the weight
weight.force = spring.force

# calculate the new spring displacement
spring.displacement = weight.pos

# update the length of the spring
spring.axis = relaxedlength + spring.displacement
 
Physics news on Phys.org
Hi jrgoodin,

jrgoodin said:
This is what I have, and for some reason it won't move

Your code is not yet telling the weight to move. For example, to get the weight to move at constant velocity, you might add a line to the end like this:

Code:
from visual import *
from visual.graph import *


relaxedlength = vector(.60,0,0) # length of spring when it isn't stretched or compressed
spring = helix(pos=(-.75,0,0),axis=relaxedlength, radius=.1,coils=8,thickness=.01,color=color.green)
spring.constant = 25 # k

weight = box(pos=(0,0,0),size=(.3,.3,.3),color=color.yellow)
weight.mass = 10 # kg
weight.velocity = vector(.30,0,0)
weight.acceleration = vector(.01,0,0)
weight.force = vector(0.3,0,0)

frictionlessSurface = box(size=(2,.02,.5),pos=(0,-.16,0))
wall = box(size=(.04,.5,.3),pos=(-.77,.1,0),color=color.red)
spring.displacement = weight.pos # the weight starts at (0,0,0) and is attached to spring
spring.axis = relaxedlength + spring.displacement


dt = .01    # seconds
t = 0 # total time
while 1:
    rate(100)
    t += dt
    
    # Calculate the spring force using Hooke's Law
    spring.force = -spring.constant * spring.displacement

    # The spring force acts on the weight
    weight.force = spring.force
    
    # calculate the new spring displacement
    spring.displacement = weight.pos

    # update the length of the spring
    spring.axis = relaxedlength + spring.displacement

    weight.pos = weight.pos + (dt,0,0)

Now of course this is not what you want for your problem; you'll have to replace the (dt,0,0) with an expression for the effect of the spring. (Also, you'll have to use the position of the weight to update the spring displacement for the next iteration of the loop, etc.)

But the point is for each iteration of the loop you have to calculate the new position (and velocity) of the weight; those give the new value of the force for the next pass of the loop, etc. Does this help?