I'm basically totally knew to programming, been doing it for about 3 days and I became quite interested quite fast - as an exercise to help me learn, I've opted for trial by fire by just going straight in and using vPython to try to create a snooker simulation.
So far its going okay, I've got all the models for the table, cushions, pocket jaws etc, and I've managed to make the balls rebound off the cushions and jaws in a realistic fashion. I've also got the balls to slow down at a rate proportional to their speed - all quite simple stuff I know, but I'm new here so its all quite exciting really.
Anyway - the problem. I'm creating a function into which two balls are the input, and it firstly detects whether they are in contact then causes them to change direction (hitting each other). So far I have this:
def ballhit(ball1,ball2):
v1 = ball1.velocity
v2 = ball2.velocity
btb = ball1.pos - ball2.pos # Ball To Ball
nbtb = btb/abs(btb) # Normalised btb
if abs(ball1.velocity) == 0:
theta1 = 0
else:
theta1 = acos(abs(ball1.velocity.x)/abs(ball1.velocity))
if abs(ball2.velocity) == 0:
theta2 = 0
else:
theta2 = acos(abs(ball2.velocity.x)/abs(ball2.velocity))
phi = acos(abs(vector(1,0,0))/abs(nbtb))
ball1.velocity.x = ((2*bm*ball2.velocity*cos(theta2-phi)*cos(phi))/2*bm) + ball1.velocity*(sin(theta1-phi))*(cos(phi+(pi/2)))
ball1.velocity.y = ((2*bm*ball2.velocity*cos(theta2-phi)*sin(phi))/2*bm) + ball1.velocity*(sin(theta1-phi))*(sin(phi+(pi/2)))
ball2.velocity.x = ((2*bm*ball1.velocity*cos(theta1-phi)*cos(phi))/2*bm) + ball2.velocity*(sin(theta2-phi))*(cos(phi+(pi/2)))
ball2.velocity.y = ((2*bm*ball1.velocity*cos(theta1-phi)*sin(phi))/2*bm) + ball2.velocity*(sin(theta2-phi))*(sin(phi+(pi/2)))
return ball1.velocity
return ball2.velocity
The two theta values are the angles the balls make with the x-axis, the phi value is the angle the normal (which is also normalized) at the point of contact makes with the x-axis, then my calculations for each of the components of each ball. As far as I can tell the calculations are correct, but the way I've written it must be absolutely dire (as I expect).
This is the error I get:
line 320, in <module>
ballhit(red1,blue)
line 302, in ballhit
ball1.velocity.x = ((2*bm*ball2.velocity*cos(theta2-phi)*cos(phi))/2*bm) +
ball1.velocity*(sin(theta1-phi))*(cos(phi+(pi/2))) ArgumentError:
Python argument types in
None.None(vector, vector) did not match C++ signature:
None(class cvisual::vector {lvalue}, double)
I have defined a velocity for both balls and there are no other unassigned variables from what I can see.
If anyone could just explain to me what the error actually means it'd be appreciated, and if someone could show me how its fixed that'd be double points.
Thanks :)