Python My Python is not working like I need it to

  • Thread starter Thread starter jrgoodin
  • Start date Start date
  • Tags Tags
    Python
AI Thread Summary
The discussion revolves around a coding issue related to simulating a spring and weight system using a physics engine. The original code fails to move the weight because it does not update the weight's position during each iteration of the simulation loop. To resolve this, it is suggested to add a line that updates the weight's position based on the calculated forces. The user is advised to replace a placeholder movement expression with a proper calculation that incorporates the spring's effect on the weight. Additionally, the importance of continuously updating the spring's displacement based on the weight's position is emphasized for accurate simulation. The exchange concludes with the user expressing gratitude for the guidance provided.
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
 
Technology 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?
 
yes, very much so...thank you
-J
 
Sure, glad to help!
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top