def Test(): ball = tableballdefs.Ball(1, 1, 0, 0, 0, 105) table = tableballdefs.Table() plot = plotballs.PlotBalls(1, table) timeStep = 0.1 frictionForce = Solve(ball, table, timeStep) while frictionForce != 0: print("topSpin ", ball.topSpin * ball.radius, " velocity ", ball.Vel.getLength(), " friction force ", frictionForce) frictionForce = Solve(ball, table, timeStep)
def Rack(numBalls, table): ballList = [] for i in range(0, numBalls): ballList.append(tableballdefs.Ball()) if len(ballList) == 4: ballList[0].Loc.x = table.length / 4 ballList[0].Loc.y = table.width / 2 ballList[1].Loc.x = table.length * 3 / 4 ballList[1].Loc.y = table.width / 2 ballList[2].Loc.x = ballList[1].Loc.x + random.random( ) / 100 + math.cos(math.radians(60)) * ballList[1].radius ballList[2].Loc.y = ballList[1].Loc.y + math.sin( math.radians(60)) * ballList[1].radius ballList[3].Loc.x = ballList[1].Loc.x + math.cos( math.radians(60)) * ballList[1].radius ballList[3].Loc.y = ballList[1].Loc.y - random.random( ) / 100 - math.sin(math.radians(60)) * ballList[1].radius return ballList
newXSpeed1 = speed1 * math.cos(dir1 - collisionAngle) newYSpeed1 = speed1 * math.sin(dir1 - collisionAngle) newXSpeed2 = speed2 * math.cos(dir2 - collisionAngle) newYSpeed2 = speed2 * math.sin(dir2 - collisionAngle) finalXSpeed1 = ( (ball1.mass - ball2.mass) * newXSpeed1 + (ball2.mass + ball2.mass) * newXSpeed2) / (ball1.mass + ball2.mass) finalXSpeed2 = ( (ball1.mass + ball2.mass) * newXSpeed1 + (ball2.mass - ball1.mass) * newXSpeed2) / (ball1.mass + ball1.mass) finalYSpeed1 = newYSpeed1 finalYSpeed2 = newYSpeed2 cosAngle = math.cos(collisionAngle) sinAngle = math.sin(collisionAngle) ball1.Vel.x = cosAngle * finalXSpeed1 - sinAngle * finalYSpeed1 ball1.Vel.y = sinAngle * finalXSpeed1 + cosAngle * finalYSpeed1 ball2.Vel.x = cosAngle * finalXSpeed2 - sinAngle * finalYSpeed2 ball2.Vel.y = sinAngle * finalXSpeed2 + cosAngle * finalYSpeed2 if __name__ == "__main__": ball1 = tableballdefs.Ball(0, 0, 0.68, 0.68, 0, 0) ball2 = tableballdefs.Ball(-1.50, 0, 0.7372, 0.68, 0, 0) if happened(ball1, ball2): run(ball1, ball2) else: print("no collision")
# -*- coding: utf-8 -*- """ Created on Wed Jul 18 11:30:21 2018 @author: tony.gold """ import tableballdefs import math def aim(cueBall, objBall): azmuth = math.atan2(objBall.Loc.y - cueBall.Loc.y, objBall.Loc.x - cueBall.Loc.x) return azmuth if __name__ == "__main__": ball1 = tableballdefs.Ball() ball2 = tableballdefs.Ball() aim(ball1, ball2)
ball.Vel.x += (accX * timeStep) ball.english += (alpha * timeStep) return True return False def CalcFF(v0, v1, timeStep, mass, coef): acc = (v0 - v1) / timeStep norm = acc * mass return norm * coef def CalcAccPara(ff, mass): return ff / mass def CalcAlpha(ff, radius, moi): torq = ff * radius return torq / moi if __name__ == "__main__": table = tableballdefs.Table() ball = tableballdefs.Ball(1, +2, table.length, table.width / 2, 0, 2) Carom(ball, table, 0.01) print(ball.Vel.x, ball.Vel.y, ball.english)