def ball_collision_response(self, ball): if self.ball_collision_detection(ball): normal = vector3d.Vector3D(self.position.x - ball.position.x, self.position.y - ball.position.y, self.position.z - ball.position.z) normal.normalise() force_magnitude = ((self.velocity.dot(normal) - ball.velocity.dot(normal)) * 2.0) / (self.mass + ball.mass) self.velocity = vector3d.Vector3D(self.velocity.x - ((force_magnitude * ball.mass) * normal.x), self.velocity.y - ((force_magnitude * ball.mass) * normal.y), self.velocity.z - ((force_magnitude * ball.mass) * normal.z)) ball.velocity = vector3d.Vector3D(ball.velocity.x + ((force_magnitude * self.mass) * normal.x), ball.velocity.y + ((force_magnitude * self.mass) * normal.y), ball.velocity.z + ((force_magnitude * self.mass) * normal.z)) self.velocity.x *= normal.x * self.ball_elastic_constant(ball) self.velocity.y *= normal.y * self.ball_elastic_constant(ball) self.velocity.z *= normal.z * self.ball_elastic_constant(ball) ball.velocity.x *= normal.x * ball.ball_elastic_constant(self) ball.velocity.y *= normal.y * ball.ball_elastic_constant(self) ball.velocity.z *= normal.z * ball.ball_elastic_constant(self)
def __init__(self): self.translation = vector3d.Vector3D(0.0, 0.0, 1.0) self.rotation_magnitude = vector3d.Vector3D(0.0, 0.0, 0.0) self.rotation_direction = vector3d.Vector3D(0.0, 0.0, 0.0) self.translation_bool = False self.rotation_bool = False self.speed = 1.0
def __init__(self, elasticity, friction): self.size = 10 self.mass = 1.0 self.colour = vector3d.Vector3D(0.0, 0.0, 0.0) self.position = vector3d.Vector3D(0.0, 0.0, 0.0) self.previous_position = self.position self.velocity = vector3d.Vector3D(0.0, 0.0, 0.0) self.elasticity = elasticity self.friction = friction
def integrate(value, increment, delta_time): new_value = vector3d.Vector3D(0, 0, 0) new_value.x = value.x + (increment.x * delta_time) new_value.y = value.y + (increment.y * delta_time) new_value.z = value.z + (increment.z * delta_time) return new_value
def get_random_velocity(box_size): return vector3d.Vector3D(random.uniform(-box_size, box_size) * 2.0, 0.0, random.uniform(-box_size, box_size) * 2.0)
def get_random_position(self, box_size): return vector3d.Vector3D(random.uniform(-box_size + self.size, box_size - self.size), random.uniform(0, box_size - self.size), random.uniform(-box_size + self.size, box_size - self.size))
def get_random_colour(): return vector3d.Vector3D(random.random(), random.random(), random.random())
def reset_rotation(self): self.rotation_magnitude = vector3d.Vector3D(0.0, 0.0, 0.0) self.rotation_direction = vector3d.Vector3D(0.0, 0.0, 0.0) self.rotation_bool = False
def reset_translation(self): self.translation = vector3d.Vector3D(0.0, 0.0, 1.0) self.translation_bool = False
def update_moving(self): if self.velocity.dot() < 0.1: self.velocity = vector3d.Vector3D(0, 0, 0) self.moving = False
import time import random import matplotlib.pyplot import mpl_toolkits.mplot3d import vector2d import vector3d import ball import box gravitational_acceleration = vector3d.Vector3D(0, -9.8, 0) room = box.Box(vector3d.Vector3D(-5, -5, -5), vector3d.Vector3D(5, 5, 5), 0.8, 0.2) balls = [] for i in range(2): balls.append(ball.Ball(vector3d.Vector3D(random.uniform(room.origin.x, room.limit.x), random.uniform((room.limit.y + room.origin.y) * 0.5, room.limit.y), random.uniform(room.origin.z, room.limit.z)), vector3d.Vector3D(random.uniform(-room.limit.x, room.limit.x) * 2, random.uniform(0, room.limit.y) * 2, random.uniform(-room.limit.z, room.limit.z) * 2), gravitational_acceleration, 1, 1, 0.8, 0.2)) moving = [] for i in balls: moving.append(True) previous_time = time.time()
def gravitational_acceleration(): return vector3d.Vector3D(0, -9800, 0)