what is promblem?

26 views
Skip to first unread message

별과바람

unread,
Oct 19, 2016, 3:27:11 AM10/19/16
to VPython-users
# -*- coding: cp949 -*-


from visual import *

e_const = 9.e9 
e_charge = -1.6e-19

distance = 5.3e-11
q1 = sphere(pos=(-0.5*distance, 0, 0), radius=0.1*distance, color=color.blue) 
q2 = sphere(pos=scene.mouse.getclick().pos, radius=0.1*distance, color=color.red) 

q1.q = e_charge 
q2.q = -e_charge 
f_arrow = arrow(pos=q2.pos, color=color.yellow, axis=(0,0,0),
                shaftwidth=q2.radius/4.)
while True :
         q2.pos = scene.mouse.getclick().pos
         f_arrow.pos = q2.pos
         r_vec = q2.pos - q1.pos 
         r_mag = mag(r_vec) 
         r_hat = norm(r_vec)
         force = (e_const*q1.q*q2.q*r_hat)/(r_mag)**2
         force_mag = mag(force)
         force_hat = norm(force) 
         print force_mag, force_hat
         scale_factor = 0.5*r_mag/force_mag 
         f_arrow.axis = scale_factor*force

hello, I`m korean, so i can`t English very well.
Question: I made program that show you electric force arrow whenever you click anywhere but if i click anywhere , the arrow is not appear only first click. Since second click, all is good. what is problem??
         

Bruce Sherwood

unread,
Oct 19, 2016, 9:54:08 AM10/19/16
to VPython-users
The problem was that when you say q2 = sphere(pos=scene.mouse.getclick().pos) there is a wait for a click before proceeding to the next statement, and then the arrow is created with zero length. Here is a slightly modifies program that works:

# -*- coding: cp949 -*-

from visual import *

e_const = 9.e9 
e_charge = -1.6e-19

distance = 5.3e-11
q1 = sphere(pos=(-0.5*distance, 0, 0), radius=0.1*distance, color=color.blue)
q2 = None

q1.q = e_charge  
f_arrow = arrow(color=color.yellow, axis=(0,0,0), shaftwidth=q1.radius/4.)

while True :
         pos = scene.mouse.getclick().pos
         if q2 is None:
             q2 = sphere(radius=q1.radius, color=color.red, q=-q1.q)
         q2.pos = pos
Reply all
Reply to author
Forward
0 new messages