Ejemplo n.º 1
0
def wall_collision(pos0, puck, dt):
	rad = puck.get_radius()

	posf = puck.get_position()

	#There's a lot of subtracting of the radius and other similar operations here.
	#This is because the interpolation of the impact point must be skewed because
	#the pucks arn't just a single point, they have a discrete width.

	#if we hit the top, reverse y-velocity
	if puck.get_y() + rad >= settings.TABLE_HEIGHT:
		#get the x coordinate of impact
		x_impact = physics.linear_interpolate_x(pos0, posf, settings.TABLE_HEIGHT - rad)
		impact_pos = Position(x_impact, settings.TABLE_HEIGHT - rad)
		time_to_impact = (impact_pos.get_y() - pos0.get_y()) / puck.get_vy()
		#how much time remains in the timestep
		time_remaining = dt - time_to_impact
		puck.set_vy(-1 * puck.get_vy())
		#set pucks new position including evolution from left over timestep
		puck.set_x(x_impact + (puck.get_vx() * time_remaining))
		puck.set_y((settings.TABLE_HEIGHT - rad) + (puck.get_vy() * time_remaining))

	#if we hit the right side, reverse the x-velocity
	elif puck.get_x() + rad >= settings.TABLE_WIDTH:
		#get the y coordinate of impact
		y_impact = physics.linear_interpolate_y(pos0, posf, settings.TABLE_WIDTH - rad)
		impact_pos = Position(settings.TABLE_WIDTH - rad, y_impact)
		time_to_impact = (impact_pos.get_x() - pos0.get_x()) / puck.get_vx()
		#how much time remains in the timestep
		time_remaining = dt - time_to_impact
		puck.set_vx(-1 * puck.get_vx())
		#set pucks new position including evolution from left over timestep
		puck.set_x((settings.TABLE_WIDTH - rad) + (puck.get_vx() * time_remaining))
		puck.set_y(y_impact + (puck.get_vy() * time_remaining))

	#if we hit the bottom, reverse y-velocity * make contact with paddle
	elif puck.get_y() - rad <= 0:
		#get the x coordinate of impact
		x_impact = physics.linear_interpolate_x(pos0, posf, rad)
		impact_pos = Position(x_impact, rad)
		time_to_impact = (impact_pos.get_y() - pos0.get_y()) / puck.get_vy()
		#how much time remains in the timestep
		time_remaining = dt - time_to_impact
		puck.set_vy((-1 * puck.get_vy()) + physics.paddle_impact())
		#set pucks new position including evolution from left over timestep
		puck.set_x(x_impact + (puck.get_vx() * time_remaining))
		puck.set_y(rad + (puck.get_vy() * time_remaining))

	#if we hit the left side, reverse the x-velocity
	elif puck.get_x() - rad <= 0:
		#get the y coordinate of impact
		y_impact = physics.linear_interpolate_y(pos0, posf, rad)
		impact_pos = Position(rad, y_impact)
		time_to_impact = (impact_pos.get_x() - pos0.get_x()) / puck.get_vx()
		#how much time remains in the timestep
		time_remaining = dt - time_to_impact
		puck.set_vx(-1 * puck.get_vx())
		#set pucks new position including evolution from left over timestep
		puck.set_x(rad + (puck.get_vx() * time_remaining))
		puck.set_y(y_impact + (puck.get_vy() * time_remaining))

	return puck
Ejemplo n.º 2
0
class Puck(Obj):
	def __init__(self, name = False, x_pos = False, y_pos = False, radius = False):
		Obj.__init__(self, name)

		self.position = Position(float(x_pos), float(y_pos))
		self.velocity = Velocity(0.0, 0.0)

		self.radius = radius if radius else settings.PUCK_RADIUS

		self.mass = settings.PUCK_MASS

	def get_mass(self):
		return self.mass

	def set_mass(self, mass):
		self.mass = mass

	def get_position(self):
		return self.position

	def set_position(self, position):
		self.position = position

	def get_x(self):
		return self.position.get_x()

	def set_x(self, x_pos):
		self.position.set_x(float(x_pos))

	def get_y(self):
		return self.position.get_y();

	def set_y(self, y_pos):
		self.position.set_y(float(y_pos))

	def	get_radius(self):
		return self.radius

	def set_radius(self, radius):
		self.radius = float(radius)

	def get_velocity(self):
		return self.velocity

	def set_velocity(self, velocity):
		self.velocity = velocity

	def get_vx(self):
		return self.velocity.get_x()

	def set_vx(self, vx):
		self.velocity.set_x(float(vx))

	def get_vy(self):
		return self.velocity.get_y();

	def set_vy(self, vy):
		self.velocity.set_y(float(vy))

	def __str__(self):
		return "Puck: %s at (%s, %s) going (%s, %s)" % (
			self.get_name(),
			self.get_x(),
			self.get_y(),
			self.get_vx(),
			self.get_vy()
			)