German Larrain avatar German Larrain committed 6c3960e

backport of 000930d4b5a1 (partial, almost complete)

Comments (0)

Files changed (4)

ars/model/collision/adapters.py

-
-# Created on 2012.04.26
-#
-# @author: german
-
 from abc import ABCMeta, abstractmethod
 
 import ode
 #	def __init__(self):
 #		pass
 
-	#===========================================================================
+	#==========================================================================
 	# Functions and methods not overriding base class functions and methods
-	#===========================================================================
+	#==========================================================================
 
 	@staticmethod
 	def near_callback(args, geom1, geom2):
 		checks if the given geoms do collide and creates contact joints (c_joint)
 		if they do, except if they are connected."""
 
-		c_joint_bounce = 0.2 # default: 0.2
-		c_joint_mu = 500 # default: 500. 0-5 = very slippery, 50-500 = normal, 5000 = very sticky
+		# Contact joint parameters:
+		# 	-bounce: default=0.2
+		# 	-mu: default=500.
+		# 		very slippery 0-5, normal 50-500, very sticky 5000
+		c_joint_bounce = 0.2
+		c_joint_mu = 500
 
 		world = args.world
 		contact_group = args.contact_group
 			geom2):
 			return
 
-		#=======================================================================
+		#======================================================================
 		# Ray's special case
-		#=======================================================================
+		#======================================================================
 		if OdeEngine.is_ray(geom1):
 			if OdeEngine.is_ray(geom2):
 				print('Weird, ODE says two rays may collide. '
 			ray_geom = geom2
 			other_geom = geom1
 
-		#=======================================================================
+		#======================================================================
 		# create contact joints
-		#=======================================================================
+		#======================================================================
 		# check if the objects collide
 		contacts = ode.collide(geom1, geom2)
 		for c in contacts:
 
 			if ray_geom is not None:
 				OdeEngine.handle_ray_collision(ray_geom, other_geom, c)
-			else: # we create a ContactJoint only if both geoms are not rays
+			else:  # we create a ContactJoint only if both geoms are not rays
 				# set contact parameters
 				c.setBounce(c_joint_bounce)
 				c.setMu(c_joint_mu)
 		ray_pos = ray.getPosition()
 		(pos, normal, depth, geom1, geom2) = contact.getContactGeomParams()
 		ray_contact = base.RayContactData(
-			ray, other_geom, ray_pos, normal, depth) # distance = depth
+			ray, other_geom, ray_pos, normal, depth)  # distance = depth
 		try:
 			ray.outer_object.set_last_contact(ray_contact)
 		except AttributeError:
 		except Exception:
 			print("Ray's encapsulating object's last_contact attribute could not be set")
 
+
 class Space(base.Space):
 	def __init__(self):
-		self._inner_object = ode.SimpleSpace() # same as ode.Space(space_type=0)
+		# ode.SimpleSpace() is the same as ode.Space(space_type=0)
+		self._inner_object = ode.SimpleSpace()
 
 	def collide(self, args, callback=None):
 		if callback is None:
 		else:
 			self._inner_object.collide(args, callback)
 
+
 class OdeGeom(base.Geom):
 
 	def attach_body(self, body):
 		super(OdeGeom, self).attach_body(body)
 		self._inner_object.setBody(body.inner_object)
 
-	#===========================================================================
-	# GETTERS AND SETTERS
-	#===========================================================================
+	#==========================================================================
+	# Getters and setters
+	#==========================================================================
 
 	def get_position(self):
 		return self._inner_object.getPosition()
 	def set_rotation(self, rot):
 		self._inner_object.setRotation(rot)
 
+
 class Ray(OdeGeom, base.Ray):
 
 	def __init__(self, space, length):
 		OdeGeom.__init__(self)
 		base.Ray.__init__(self, space, length)
 		self._inner_object = ode.GeomRay(space.inner_object, length)
-		self._inner_object.outer_object = self # it is the encapsulating object
+		self._inner_object.outer_object = self  # the encapsulating object
 
 	def get_length(self):
 		return self._inner_object.getLength()
 	def set_length(self, length):
 		self._inner_object.setLength(length)
 
+
 class Trimesh(OdeGeom, base.Trimesh):
 
 	def __init__(self, space, vertices, faces, center):
 		self._inner_object = ode.GeomTriMesh(tm_data, space.inner_object)
 		self._inner_object.setPosition(center)
 
+
 class OdeBasicShape(OdeGeom):
 	__metaclass__ = ABCMeta
 
+
 class Box(OdeBasicShape, base.Box):
 	"""Box shape, aligned along the X, Y and Z axii by default"""
 
 
 		self._inner_object = ode.GeomBox(space.inner_object, lengths=size)
 
+
 class Sphere(OdeBasicShape, base.Sphere):
 	"""Spherical shape"""
 
 
 		self._inner_object = ode.GeomSphere(space.inner_object, radius)
 
+
 class Capsule(OdeBasicShape, base.Capsule):
 	"""Capsule shape, aligned along the Z-axis by default"""
 
 		self._inner_object = ode.GeomCCylinder(
 			space.inner_object, radius, length)
 
+
 class Cylinder(OdeBasicShape, base.Cylinder):
 	"""Cylinder shape, aligned along the Z-axis by default"""
 
 		self._inner_object = ode.GeomCylinder(
 			space.inner_object, radius, length)
 
+
 class Plane(OdeBasicShape, base.Plane):
 	"""Plane, different from a box"""
 
 		self._inner_object = ode.GeomPlane(
 			space.inner_object, normal, dist)
 
+
 class NearCallbackArgs(base.NearCallbackArgs):
 	pass

ars/model/collision/base.py

-
-# Created on 2011.10.31
-#
-# @author: german
-
 from abc import ABCMeta, abstractmethod
 
 import ars.exceptions as exc
 #	def __init__(self):
 #		raise NotImplementedError()
 
+
 class Space:
 	__metaclass__ = ABCMeta
 
 	def collide(self, args, callback):
 		raise NotImplementedError()
 
+
 class Geom:
 	"""Encapsules a geometry object"""
 	__metaclass__ = ABCMeta
 	def attach_body(self, body):
 		self._attached_body = body
 
-	#===========================================================================
-	# GETTERS AND SETTERS
-	#===========================================================================
+	#==========================================================================
+	# Getters and setters
+	#==========================================================================
 	@property
 	def inner_object(self):
 		return self._inner_object
 	def set_rotation(self, rot):
 		pass
 
+
 class Ray(Geom):
 	"""
 	Ray aligned along the Z-axis by default.
 		self.clear_last_contact()
 		self.clear_closer_contact()
 
+
 class Trimesh(Geom):
 
 	@abstractmethod
 		for x in range(num_x - 1):
 			for z in range(num_z - 1):
 
-				zero = indices[x][z] # top-left corner
-				one = indices[x][z + 1] # bottom-left
-				two = indices[x + 1][z] # top-right
-				three = indices[x + 1][z + 1] # bottom-right
+				zero = indices[x][z]			# top-left corner
+				one = indices[x][z + 1]			# bottom-left
+				two = indices[x + 1][z]			# top-right
+				three = indices[x + 1][z + 1] 	# bottom-right
 
 				# there are two face types for each square
 				# contiguous squares must have different face types
 			new_faces.append((face[2], face[1], face[0]))
 		return new_faces
 
-	#===========================================================================
+	#==========================================================================
 	# def attach_body(self, body):
 	#	raise exc.ArsError('Trimesh shapes are not yet allowed to have a body attached')
 	#
 	# def get_attached_body(self):
 	#	raise exc.ArsError('Trimesh shapes are not yet allowed to have a body attached')
-	#===========================================================================
+	#==========================================================================
 
 
 class Terrain(Geom):
 	__metaclass__ = ABCMeta
 
-#===============================================================================
+#==============================================================================
 # Basic Shapes
-#===============================================================================
+#==============================================================================
+
 
 class BasicShape(Geom):
 	"""Abstract class from whom every solid object's shape derive"""
 	__metaclass__ = ABCMeta
 
+
 class Box(BasicShape):
 	"""Box shape, aligned along the X, Y and Z axii by default"""
 	__metaclass__ = ABCMeta
 	def __init__(self, space, size):
 		super(Box, self).__init__()
 
+
 class Sphere(BasicShape):
 	"""Spherical shape"""
 	__metaclass__ = ABCMeta
 	def __init__(self, space, radius):
 		super(Sphere, self).__init__()
 
+
 class Capsule(BasicShape):
 	"""Capsule shape, aligned along the Z-axis by default"""
 	__metaclass__ = ABCMeta
 	def __init__(self, space, length, radius):
 		super(Capsule, self).__init__()
 
+
 class Cylinder(BasicShape):
 	"""Cylinder shape, aligned along the Z-axis by default"""
 	__metaclass__ = ABCMeta
 	def __init__(self, space, length, radius):
 		super(Cylinder, self).__init__()
 
+
 class Plane(BasicShape):
 	"""Plane, different from a box"""
 	__metaclass__ = ABCMeta
 	def __init__(self, space, normal, dist):
 		super(Plane, self).__init__()
 
+
 class Cone(BasicShape):
 	"""Cone"""
 	__metaclass__ = ABCMeta
 	def __init__(self):
 		super(Cone, self).__init__()
 
-	#===========================================================================
-	# def add_ray(self, position, direction):
-	#	"""Adds a ray to the space, whose `_inner_object` will store it"""
-	#	shapes.Ray(self, position, direction) # no need for assignment
-	#===========================================================================
 
 class RayContactData:
 	"""Contact information of a collision between `ray` and `shape`"""
-	
+
 	def __init__(self, ray=None, shape=None, position=None, normal=None, depth=None):
+		# -position: point at which the ray intersects the surface of the other
+		# 	shape/geom.
+		# -normal: surface normal of the other geom at the contact point.
+		# -depth: distance from the start of the ray to the contact point.
+		# 	TODO: is it true?
 		self.ray = ray
 		self.shape = shape
-		self.position = position # point at which the ray intersects the surface of the other shape/geom
-		self.normal = normal # surface normal of the other geom at the contact point
-		self.depth = depth # distance from the start of the ray to the contact point # TODO: is it true?
+		self.position = position
+		self.normal = normal
+		self.depth = depth
+
 
 class NearCallbackArgs:
 	"""Class to encapsulate args passed to the `near_callback` function"""
-	
+
 	def __init__(self, world=None, contact_group=None, ignore_connected=True):
 		self.world = world
 		self.contact_group = contact_group

ars/model/physics/adapters.py

-
-# Created on 2012.04.26
-#
-# @author: german
-
 import ode
 
 import ars.exceptions as exc
 
 #	def __init__(self):
 #		pass
-	
+
 	@staticmethod
 	def create_joint_group():
 		return ode.JointGroup()
 	def __init__(self):
 		self._inner_object = None
 
-#===============================================================================
-# GETTERS AND SETTERS
-#===============================================================================
+	#==========================================================================
+	# Getters and setters
+	#==========================================================================
 
 	def get_position(self):
 		return self._inner_object.getPosition()
 	def set_rotation(self, rot):
 		self._inner_object.setRotation(rot)
 
+
 class Capsule(OdeBody, base.Capsule):
 	def __init__(self, world, space, length, radius, mass=None, density=None):
 		"""create capsule body (aligned along the z-axis so that it matches the
 		return self._radius
 
 class Cylinder(OdeBody, base.Cylinder):
-
 	def __init__(self, world, space, length, radius, mass=None, density=None):
 		"""create cylinder body (aligned along the z-axis so that it matches
 		the Geom created below,	which is aligned along the Z-axis by default)"""
 	def radius(self):
 		return self._radius
 
-#===============================================================================
+#==============================================================================
 # Private functions
-#===============================================================================
+#==============================================================================
 
 def _create_ode_capsule(world, length, radius, mass=None, density=None):
 
-	capsule_direction = 3 # z-axis
+	capsule_direction = 3  # z-axis
 	body = ode.Body(world)
 
 	if mass is not None:
 		body.setMass(m)
 
 	# set parameters for drawing the body
-	body.shape = "capsule" # TODO: delete this, because it is related to the original implementation
+	# TODO: delete this, because it is related to the original implementation
+	body.shape = "capsule"
 	body.length = length
 	body.radius = radius
 
 	return body
 
+
 def _create_ode_cylinder(world, length, radius, mass=None, density=None):
-	cylinderDirection = 3 # Z-axis
+	cylinderDirection = 3  # Z-axis
 	body = ode.Body(world)
 
 	if mass is not None:
 
 	return body
 
+
 def _create_ode_box(world, size, mass=None, density=None):
 	body = ode.Body(world)
 
 
 	return body
 
+
 def _create_ode_sphere(world, radius, mass=None, density=None):
 	body = ode.Body(world)
 
 		body.setMass(m)
 
 	return body
-

ars/model/physics/base.py

-
-# Created on 2011.08.09
-#
-# @author: german
-
 from abc import ABCMeta, abstractmethod
 
 
 #	def __init__(self):
 #		raise NotImplementedError()
 
+
 class Body:
 	__metaclass__ = ABCMeta
 
 		geom.attach_body(self)
 		self._attached_geom = geom
 
-#===============================================================================
-# GETTERS AND SETTERS
-#===============================================================================
+	#==========================================================================
+	# Getters and setters
+	#==========================================================================
 
 	@property
 	def inner_object(self):
 	def set_rotation(self, rot):
 		raise NotImplementedError()
 
+
 class Box(Body):
 	__metaclass__ = ABCMeta
+
 	def __init__(self, size, position, rotation=None):
 		super(Box, self).__init__()
 
+
 class Cone(Body):
 	__metaclass__ = ABCMeta
+
 	def __init__(self, height, radius, center, rotation=None):
 		super(Cone, self).__init__()
 
+
 class Sphere(Body):
 	__metaclass__ = ABCMeta
+
 	def __init__(self, radius, center, rotation=None):
 		super(Sphere, self).__init__()
 
+
 class Cylinder(Body):
 	__metaclass__ = ABCMeta
+
 	def __init__(self, length, radius, center, rotation=None):
 		super(Cylinder, self).__init__()
 
+
 class Capsule(Body):
 	__metaclass__ = ABCMeta
+
 	def __init__(self, length, radius, center, rotation=None):
 		super(Capsule, self).__init__()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.