Tinkering a little bit with Vpython.

  • Context: Python 
  • Thread starter Thread starter MathematicalPhysicist
  • Start date Start date
  • Tags Tags
    Bit
Click For Summary
SUMMARY

This discussion focuses on implementing a timer in a VPython simulation to visualize the gradual inflation of a ball. The user provided a basic code snippet using the VPython library but struggled with adding a timing mechanism. A suggested solution involves using the rate() function to control the loop iterations per second, allowing for a smooth inflation effect. The recommended implementation is to place rate(100) at the beginning of the while loop to achieve the desired timing.

PREREQUISITES
  • Familiarity with Python programming language
  • Basic understanding of the VPython library
  • Knowledge of loop structures in programming
  • Concept of controlling execution speed in simulations
NEXT STEPS
  • Explore the VPython documentation for detailed usage of the rate() function
  • Learn about the sleep() function in Python for timing control
  • Investigate other simulation examples on the VPython website
  • Experiment with modifying the inflation rate and visual effects in VPython
USEFUL FOR

Students, educators, and hobbyists interested in programming simulations with VPython, particularly those looking to enhance their understanding of timing and animation in Python.

MathematicalPhysicist
Science Advisor
Gold Member
Messages
4,662
Reaction score
372
I thought to write a naive simulation of an inflated ball, I am not sure how to implement this by Vpython.

I mean I want to see it gradually inflating minute by minute.

This is the code I wrote but I don't know how to add to it a timer.
Code:
from visual import *
ball=sphere(pos=vector(4,7,3),radius=2,color=color.green)
x=1
while x<50:
 ball.radius=ball.radius+x
 x=x+1
can anyone give me hint as to how to add a timing to this inflation?

Thanks.
 
Technology news on Phys.org
MathematicalPhysicist said:
I thought to write a naive simulation of an inflated ball, I am not sure how to implement this by Vpython.

I mean I want to see it gradually inflating minute by minute.

This is the code I wrote but I don't know how to add to it a timer.
Code:
from visual import *
ball=sphere(pos=vector(4,7,3),radius=2,color=color.green)
x=1
while x<50:
 ball.radius=ball.radius+x
 x=x+1
can anyone give me hint as to how to add a timing to this inflation?

Thanks.

I have no experience with python per se, but if your code is in some kind of loop, you could just add a sleep command in there somewhere. Basically calculate your rate and put your rate as your parameter in the sleep command.
 
MathematicalPhysicist said:
I thought to write a naive simulation of an inflated ball, I am not sure how to implement this by Vpython.

I mean I want to see it gradually inflating minute by minute.

This is the code I wrote but I don't know how to add to it a timer.
Code:
from visual import *
ball=sphere(pos=vector(4,7,3),radius=2,color=color.green)
x=1
while x<50:
 ball.radius=ball.radius+x
 x=x+1
can anyone give me hint as to how to add a timing to this inflation?

Thanks.

Interesting, never heard of VPython before. That said, I popped over to the VPython website and noticed a bouncing ball example there:

http://www.vpython.org/contents/bounce_example.html

What you're looking for seems to be rate(100), where 100 is the maximum loop iterations per second on a fast machine (so it could be less, of course). I think it may need to be right after the while statement as the first thing in the while's block (not sure, but that makes sense).
 

Similar threads

  • · Replies 13 ·
Replies
13
Views
7K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
5K
  • · Replies 4 ·
Replies
4
Views
12K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
8
Views
5K