VPython - example - 圆柱体斜上抛滑行运动(X-Y- -Z)

本作品采用知识共享署名-非商业性使用-相同方式共享 3.0 Unported许可协议进行许可。允许非商业转载,但应注明作者及出处。


作者:liuyuan_jq

2011-04-10


Fromhttp://wiki.showmedo.com/index.php/PythonThompsonVPythonSeries


from visual import * print""" Converted everything we can to vectors. Eliminated excess variables. Important points: A vector is something with an x,y, and z componenet. The following are always vectors: position velocity acceleration force The magnitude of a vector gives us it's total length. Magnitude is not a vector and it is always positive. The norm of a vector gives us a vector in the same direction with a magnitude of one. A vector is equal to it's magnitude multiplied by it's norm: aVector == mag(aVector) * norm(aVector) A vector in the opposite direction of the same length would be the same thing multiplied by negative one: oppositeVector = -1 * aVector or oppositeVector = -1 * mag(aVector) * norm(bVector) """ # SET UP THE WINDOW scene.width = 800 scene.height = 600 scene.autoscale = 0 # stop vpython from zooming in and out on its own scene.range = (100,100,100) scene.center = (50,40,0) # center in on this point # CREATE OUR PUCK OBJECT AND SET IT'S INITIAL VALUES puck = cylinder(pos=(0,0,0),axis=(0,2,0), radius=2, color=color.green) puck.velocity = vector(0,0,0) puck.acceleration = vector(0,0,0) puck.mass = 1 # kg # CREATE A GROUND FOR THE PUCK ground = box(pos=(50,-1,1),size=(100,2,50)) # CREATE A LABEL FOR US TO PUT INFO IN mylabel = label(pos=(50,60,0),height=10,box=0) # THESE ARE ALL THE ACCELERATION VECTORS THAT WILL BE # ACTING ON OUR PUCK (IGNORE THE COLLISION FORCE AND ACCELERATION) accelerationGravity = vector(0,-9.8,0) # m/s**2 accelerationNormal = vector(0,0,0) # m/s**2, none until hits the ground accelerationKineticFriction = vector(0,0,0) # m/s**2, none until hits ground # SET UP SOME INITIAL CONDITIONS FOR THROWNING THE PUCK velocityThrownMagnitude = 25 # m/s angleThrown = 45 # degrees angleThrown = angleThrown * (pi/180) # converted to radians # CONVERTING THE VELOCITY THROWN ANGLE AND MAGNITUDE # INTO A VECTOR velocityThrown = vector(0,0,0) velocityThrown.y = velocityThrownMagnitude * sin(angleThrown) velocityThrown.x = velocityThrownMagnitude * cos(angleThrown) velocityThrown.z = 0 # SET THE WIND VELOCITY AS A VECTOR velocityWind = vector(0,0,-3) # SET THE PUCK'S INITAL VELOCITY AND ACCELERATION puck.velocity = velocityThrown + velocityWind puck.acceleration = accelerationGravity + accelerationNormal + accelerationKineticFriction seconds = 0 # total time starts out at zero dt = .01 # .01 seconds (difference in time) finished = False while not finished: rate(100) # go thru the loop no more than 100 times/s seconds += dt # total time (we don't use it anymore) # we update the position and velocity incrementally # based on the last calculated acceration and its previous # velocity and accleration (.01 seconds ago). # velocity equation: vel = vel0 + accel * time # position equation: pos = pos0 + vel0*time + .5 * accel * time**2 puck.velocity += puck.acceleration * dt puck.pos += puck.velocity * dt + .5 * puck.acceleration * dt**2 # if we've hit the ground we deal with friction if puck.pos.y <= 0: # We're ignoring in the force of the collision but we'll say # the end result of the collision will make the velocity # in the y direction equal to zero puck.velocity.y = 0 puck.pos.y = 0 # Force Magnitude Due To Kinetic Friction = Normal Force * CoefficientKinetic CoefficientOfKineticFriction = .45 # Newton's second law: Force = Mass * Acceleration forceGravity = puck.mass * accelerationGravity # Newton's third law: forces come in pairs, and these two # forces are equal in magnitude and opposite in direction forceNormal = -forceGravity # Newton's second law written as Acceleration = Force / Mass accelerationNormal = forceNormal / puck.mass # Figure out the magnitude of the frictional force forceKineticFrictionMagnitude = mag(forceNormal * CoefficientOfKineticFriction) # Figure out the direction of the frictional force forceKineticFrictionDirection = norm(puck.velocity) * -1 # Multiply them to get the frictional force vector forceKineticFriction = forceKineticFrictionMagnitude * forceKineticFrictionDirection # Use the Force to find Acceleration: Acceleration = Force / Mass accelerationKineticFriction = forceKineticFriction / puck.mass # Update the pucks acceleration puck.acceleration = accelerationNormal + accelerationGravity + accelerationKineticFriction # End the loop when the puck stops moving # magnitude is always positive unless it's zero # but due to imprecision in our program it will likely # never be zero so stop it when the velocity is close to zero if mag(puck.velocity) <= .02: finished = True puck.velocity = (0,0,0) # show some info as we go through the loop message = "VPython Video Tutorial: Vectors (Force,Acceleration,Velocity,Position) & Kinetic Friction\n" message += "Puck Info:\n" message += "position is: " + str(puck.pos) + "\n" message += "velocity is: " + str(puck.velocity) + "\n" message += "acceleration is: " + str(puck.acceleration) + "\n" message += "magnitude of velocity vector is: " + str(mag(puck.velocity)) + " m/s" + "\n" message += "norm of velocity vector is: " + str(norm(puck.velocity)) mylabel.text = message

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值