- #1
Tanner65
- 28
- 0
Hi there, I'd like to preface my question with a few things. First off, I'm not great at physics, but I don't think the following is a hard question for any of you to answer. Also, I'm not too great with VPython, mostly because I know I'm only going to be using Python in general for another 2 weeks and never again (more of C++ and VB kinda guy), but my logic is usually pretty sound.
[/preface]
I'm working on a project where there's a box, with two spheres inside of it. One sphere is stationary while the other is moving. Well, one of the requirements is that the moving sphere must change direction upon impact with both the walls and the stationary sphere. I've got the answer to that problem, but the "velocity" of the moving sphere is not static and for the life of me I can't figure out how to have it change direction correctly and keep the same relative velocity. It seems to be move faster after the impact with the middle ball. I'm hoping this is an easy answer that's going to make me look silly, because I've looked and thought about it but probably not in the right frame of mind.
Below is the code for the whole thing:
Also, if there's a preferred forum for this type of message, I apologize, this seemed most appropriate. Please move accordingly.
[/preface]
I'm working on a project where there's a box, with two spheres inside of it. One sphere is stationary while the other is moving. Well, one of the requirements is that the moving sphere must change direction upon impact with both the walls and the stationary sphere. I've got the answer to that problem, but the "velocity" of the moving sphere is not static and for the life of me I can't figure out how to have it change direction correctly and keep the same relative velocity. It seems to be move faster after the impact with the middle ball. I'm hoping this is an easy answer that's going to make me look silly, because I've looked and thought about it but probably not in the right frame of mind.
Below is the code for the whole thing:
Code:
from visual import *
side = 4.0
thick = .03
Height = 2*side - thick
Width = 2*side + thick
RightWall = box(pos =(side,0,0),length=thick,height=Height,width=Width,color=color.red)
LeftWall = box(pos=(-side,0,0),length=thick,height=Height,width=Width,color=color.blue)
BottomWall = box(pos=(0,-side,0),length=Width,height=thick,width=Width,color=color.green)
TopWall = box(pos=(0,side,0),length=Width,height=thick,width=Width,color=color.orange)
BackWall = box(pos=(0,0,-side),length=Height,height=Height,width=thick,color=color.white)
CenterSphere = sphere(pos=(0,0,0),radius = 1,color=color.black)
ball = sphere(color=color.yellow,raidus=1,pos=(0,side-1,0))
ball.mass = 1.0
ball.velocity = vector(-0.15,-0.23,+0.27)
side = side-thick*0.5-ball.radius
dt = 0.5
t = 0.0
ball.trail = curve(color=color.white)
while 1:
rate(100)
t = t+dt
ball.pos = ball.pos + (ball.velocity/ball.mass)*dt
if not (side > ball.x >-side):
ball.velocity.x = -ball.velocity.x
if not (side > ball.y >-side):
ball.velocity.y = -ball.velocity.y
if not (side > ball.z > -side):
ball.velocity.z = -ball.velocity.z
distance=mag(CenterSphere.pos-ball.pos)
if distance<(ball.radius + CenterSphere.radius):
direction=norm(CenterSphere.pos - ball.pos) #<-- Here is where I'm calculating incorrectly
print "direction: "+str(direction)
print "ball.velocity before: " + str(ball.velocity)
ball.velocity=ball.velocity-(direction)
print "ball.velocity after: "+ str(ball.velocity)
ball.pos = ball.pos + (ball.velocity/ball.mass)*dt
print "ball.pos after: "+str(ball.pos)
ball.trail.append(pos=ball.pos)
Also, if there's a preferred forum for this type of message, I apologize, this seemed most appropriate. Please move accordingly.