0% found this document useful (0 votes)
6 views29 pages

VPython Motion Modeling Guide

Uploaded by

Ryadh Haddad
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views29 pages

VPython Motion Modeling Guide

Uploaded by

Ryadh Haddad
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SVSM 2008 Mathematical Modeling

MODELING MOTION IN VPYTHON


VPYTHON = VISUAL PYTHON

 Easy to learn
 3D Interactive modeling

 Python – background language

 Main site [Link]

 Download
[Link]
 Python Site [Link]
GETTING STARTED

 Install Python and then VPython


 Run IDLE
 First line
 from visual import *
 Next line
 sphere()
 Save – CTRL-S
 Save as [Link]
 Run – F5
CHANGE SPHERE ATTRIBUTES
 Color
 sphere(color=[Link])
 Radius
 sphere(radius=0.5,color=[Link])
 Name
 ball = sphere(radius=0.5,color=[Link])
 Position
 ball = sphere(pos=(0,2,0),radius=0.5,color=[Link])
 Change position
 [Link] = (1,2,3)
NAVIGATION

 Zoom
 Hold middle button and move mouse
 Or, hold both left and right buttons

 Rotate
 Hold right button
SIMPLE SCENE
from visual import *

#Create a sphere
ball = sphere(pos=(0,2,0),color=[Link],radius=1)

#Create a box
floor = box(length=10, height=2, width=4,color=[Link])
VECTORS
 Cartesian Coordinates
 Locate points with (x,y,z)
 Vectors
 Draw arrow from origin
 Vectors have magnitude and
direction
 (1,2,3)
 General Vector
 Draw from one point to another
 Examples
 Position, velocity, force
VECTORS IN VPYTHON

 Draw arrow from origin to center of ball


 arrow(pos=(0,0,0), axis=[Link])
 Begins at pos
 Ends at axis
 Arbitrary arrow
 arrow(pos=(1,2,3), axis=(0,2,-1))
 Arrow attributes
 arrow(pos=(1,2,3), axis=(0,2,-1),
shaftwidth=1,headwidth=2,headlength=3)
MOVING BALL – CONSTANT VELOCITY
ADDING MOTION

Goal: Make ball move and bounce off wall


 Create new program
 File
– New Window
 Save as [Link]

 Create scene
CONSTANT VELOCITY
 Velocity
 Velocity= displacement/elapsed time
 v = Dx/Dt

 Rewrite
 Dx=new position – old position
 Dx=vDt
 new position = old position + vDt
 In Vpython
 [Link] = [Link] + [Link]*dt
MOVING BALL
 Need several time steps
 Use while loop
 Keep executing commands over and over
 Additional Information
 Specify dt and velocity
 Need rate – while loop executed 100 times/sec
MAKING BALL BOUNCE

 Logical Tests
 If the ball moves to far to right, reverse its direction.
 If statement
 Ifball.x > wallR.x:
 Then what?

 Reverse direction
 [Link] = -[Link]
MODIFY PROGRAM
 Assignment 1
 Add wallL on left at (-6,00)
 Add test to have ball bounce off this wall too.
 Assignment 2
 Change ball velocity to move ball at an angle
with no z-component
 Assignment 3
 Add a ceiling and a floor that touch vertical walls
 Add back wall
 Add invisible front wall (i.e., only use if statement)
 Run program with ball bouncing off all walls.
UNIFORM MOTION – ADDING ACCELERATION
FREE FALL
Goal: Drop a ball that bounces on the floor.
 Free fall
 Allobjects near the surface of the Earth fall with an
acceleration -9.8 m/s2, or -32 ft/s2 (or a = -g).
 Acceleration
a = Dv/Dt
 Rewrite for VPython
 new velocity = old velocity + aDt
 [Link] = [Link] + [Link]*dt
BOUNCING BALL
PROJECTILE MOTION

Free fall with constant horizontal velocity.


PROGRAM FOR PROJECTILE MOTION
ERROR IN CODE?
floor = box(length=300, height=0.5, width=4, color=[Link]) dt = 0.01
ball = sphere(pos=(-90,100,0),radius=2, color=[Link]) while 1:
[Link] = vector(30,0,0) rate(100)
[Link] = [Link] + [Link]*dt
[Link] = curve(color=[Link])
if ball.y < 1:
[Link].y = -[Link].y
ball2 = sphere(pos=(-90,100,0),radius=2, color=[Link]) else:
[Link] = vector(30,0,0) [Link].y = [Link].y - 9.8*dt
[Link] = curve(color=[Link]) [Link](pos=[Link])

[Link] = [Link] + [Link]*dt


ball3 = sphere(pos=(-90,100,0),radius=2, color=[Link]) vel0=[Link]
v = vector(30,0,0) if ball2.y < 1:
[Link] = curve(color=[Link]) [Link].y = -[Link].y
a = vector(0,-9.8,0) else:
[Link].y = [Link].y - 9.8*dt
[Link] = [Link] + (vel0+[Link])*dt/2
[Link](pos=[Link])

if ball3.y < 1:
v.y= -v.y
else:
v += a * dt
[Link] += v* dt + .5 * a * dt**2
[Link](pos=[Link])
MASS – SPRING SYSTEM
MASS-SPRING SYSTEM

 Hooke’s Law for Springs


 F = -kx
 Newton’s 2nd Law of Motion
 F=ma
 Or, a= F/m
CREATE THE SCENE
from visual import *

[Link] = (1,1,1)
[Link] = (0,.1,0)
[Link] = 800
[Link] = 275
[Link] = (1,1,1)

x0 = vector(.65,0,0)
Surface = box(size=(2,.02,.5),pos=(0,-.1,0))
wall = box(size=(.04,.5,.3),pos=(-.77,.15,0))
spring = helix(pos=(-.75,0,0),axis=x0,
radius=.08,coils=6,thickness=.01,color=[Link])
block = box(pos=(0,0,0),size=(.2,.2,.2),color=[Link])
INITIALIZE
k = 10
m = 10
b=2
v = vector(0,0,0)
a = vector(0,0,0)
F = vector(0,0,0)

[Link]=(0.25,0,0) # Set Initial position of block and connect spring


x = [Link]
[Link] = x0 + x
ADD MOTION
finished = False
dt = .01
while not finished:
rate(100)

F = -k*x - b*v
a = F/m
v += a * dt
x += v + .5 * a * dt**2

[Link] = x
[Link] = x0 + x
CHANGE PROGRAM

 What happens when the mass is changed?


 What happens when k is changed?

 Change the initial block position


 i.e.,
[Link]=(0.25,0,0)
 Do you need to change other seetings?
MAKE UP YOUR OWN MOVING SYSTEM
FREE FOR ALL #1
Play with Vpython and create your own moving
objects:
 Use walls, surfaces, and other objects
 Other built-in objects
[Link]

Cylinder, arrow, cone, pyramid, sphere, ring,


box, ellipsoid, helix
FREE FOR ALL #2

Seach for Vpython Code and see what it does at


[Link]

You might also like