Commits

German Larrain committed b867838

model.simulator.physics: all Body subclasses's constructors modified to accept either mass or density arguments. ODE-related private module functions modified as well.

Comments (0)

Files changed (1)

ars/model/simulator/physics.py

 		self._inner_object.setRotation(rot)
 
 class Capsule(Body):
-	def __init__(self, world, space, density, length, radius):
+	def __init__(self, world, space, length, radius, mass=None, density=None):
 		'''
 		create capsule body (aligned along the z-axis so that it matches the Geom created below,
 		which is aligned along the Z-axis by default)
 		'''
-	
+
+		if mass is not None:
+			if density is not None:
+				raise Exception('Both mass and density arguments were given')
+		
 		# create a capsule geom for collision detection
 		#geom = ode.GeomCCylinder(space.get_inner_object(), radius, length)
 		#geom.setBody(body)
 		self._length = length
 		self._radius = radius
 		
-		body = _create_ode_capsule(world, space, density, length, radius)
+		body = _create_ode_capsule(world, space, length, radius, mass, density)
 		geom = shapes.Capsule(space, length, radius)
 		self._inner_object = body
 		self.attach_geom(geom)
 
 class Cylinder(Body):
 
-	def __init__(self, world, space, density, length, radius):
+	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)
 		'''
 	
+		if mass is not None:
+			if density is not None:
+				raise Exception('Both mass and density arguments were given')
+		
 		#geom = ode.GeomCylinder(space.get_inner_object(), radius, length)
 		#geom.setBody(body)
 		
 		self._length = length
 		self._radius = radius
 	
-		body = _create_ode_cylinder(world, space, density, length, radius)
+		body = _create_ode_cylinder(world, space, length, radius, mass, density)
 		geom = shapes.Cylinder(space, length, radius)
 		self._inner_object = body
 		self.attach_geom(geom)
 		return self._radius
 
 class Box(Body):
-	def __init__(self, world, space, density, size):
+	def __init__(self, world, space, size, mass=None, density=None):
+
+		if mass is not None:
+			if density is not None:
+				raise Exception('Both mass and density arguments were given')
+		
 		self._size = size
 		
-		body = _create_ode_box(world, space, density, size)
+		body = _create_ode_box(world, space, size, mass, density)
 		geom = shapes.Box(space, size)
 		self._inner_object = body
 		self.attach_geom(geom)
 		return self._size
 
 class Sphere(Body):
-	def __init__(self, world, space, density, radius):
+	def __init__(self, world, space, radius, mass=None, density=None):
+
+		if mass is not None:
+			if density is not None:
+				return Exception('Both mass and density arguments were given')
+
 		self._radius = radius
 		
-		body = _create_ode_sphere(world, space, density, radius)
+		body = _create_ode_sphere(world, space, radius, mass, density)
 		geom = shapes.Sphere(space, radius)
 		self._inner_object = body
 		self.attach_geom(geom)
 		return self._radius
 
 class Cone(Body):
-	def __init__(self, world, space, density, height, radius):
+	def __init__(self, world, space, height, radius, mass=None, density=None):
 		raise Exception("Not available in ODE")
 
 def create_world(gravity = (0.0,-9.81,0.0), ERP = 0.8, CFM = 1E-10):
 	world.setCFM(CFM)
 	return world
 
-def _create_ode_capsule(world, space, density, length, radius):
+def _create_ode_capsule(world, space, length, radius, mass=None, density=None):
 
 	capsule_direction = 3 # z-axis
 	body = ode.Body(world)
-	m = ode.Mass()
-	m.setCappedCylinder(density, capsule_direction, radius, length)
-	body.setMass(m)
+	
+	if mass is not None:
+		m = ode.Mass()
+		m.setCappedCylinderTotal(mass, capsule_direction, radius, length)
+		body.setMass(m)
+	elif density is not None:
+		m = ode.Mass()
+		m.setCappedCylinder(density, capsule_direction, radius, length)
+		body.setMass(m)
 
 	# set parameters for drawing the body
 	body.shape = "capsule" # TODO: delete this, because it is related to the original implementation
 	
 	return body
 
-def _create_ode_cylinder(world, space, density, length, radius):
-	
+def _create_ode_cylinder(world, space, length, radius, mass=None, density=None):
 	cylinderDirection = 3 # Z-axis
 	body = ode.Body(world)
-	m = ode.Mass()
-	m.setCylinder(density, cylinderDirection, radius, length)
-	body.setMass(m)
+	
+	if mass is not None:
+		m = ode.Mass()
+		m.setCylinderTotal(mass, cylinderDirection, radius, length)
+		body.setMass(m)
+	elif density is not None:
+		m = ode.Mass()
+		m.setCylinder(density, cylinderDirection, radius, length)
+		body.setMass(m)
 	
 	return body
 
-def _create_ode_box(world, space, density, size):
+def _create_ode_box(world, space, size, mass=None, density=None):
 	body = ode.Body(world)
-	m = ode.Mass()
-	m.setBox(density, *size)
-	body.setMass(m)
+	
+	if mass is not None:
+		m = ode.Mass()
+		m.setBoxTotal(mass, *size)
+		body.setMass(m)
+	elif density is not None:
+		m = ode.Mass()
+		m.setBox(density, *size)
+		body.setMass(m)
 	
 	return body
 
-def _create_ode_sphere(world, space, density, radius):
+def _create_ode_sphere(world, space, radius, mass=None, density=None):
 	body = ode.Body(world)
-	m = ode.Mass()
-	m.setSphere(density, radius)
-	body.setMass(m)
+	
+	if mass is not None:
+		m = ode.Mass()
+		m.setSphere(mass, radius)
+		body.setMass(m)
+	elif density is not None:
+		m = ode.Mass()
+		m.setSphere(density, radius)
+		body.setMass(m)
+
 	return body
 
 def create_joint_group():