Commits

German Larrain committed db24f23

RSS2 demo now works fine:
-new methods to create sinusoidal and constant heightfield
-get_faces returned indices are modified before given to ODE
-new OFFSET attribute in Vehicle2, allowing modifications in RSS2

Comments (0)

Files changed (2)

 """
 
 import sys
-from random import random, choice
+from random import random
 
 from ars.model.simulator import Simulation
 from ars.model.geometry.shapes import Trimesh
+import ars.utilities.mathematical as mut
 from Vehicle2 import Vehicle2
 
 def random_heightfield(num_x, num_z, scale=1.0):
 	"""
-	Copied from:
-		Edward Dale - Snowballs: An experiment in Winter frivolity (2006).
-		http://scompt.com/files/cg2/final.pdf
+	Based on:
+	Edward Dale - Snowballs: An experiment in Winter frivolity (2006).
+	http://scompt.com/files/cg2/final.pdf
 		
 	A height field where values are completely random.
 	"""
+	# that x and z are integers, not floats, does not matter
 	verts=[]
 	for x in range(num_x):
 		for z in range(num_z):
-			verts.append( (x, random()*scale, z) )
+			verts.append( (x,random()*scale,z) )
 	return verts
 
-def shrink_XZ_heightfield(vertices, factor=(1.0,1.0)):
-	
-	new_vertices = []
-	for vertex in vertices:
-		new_vertices.append((vertex[0]/factor[0], vertex[1], vertex[2]/factor[1]))
-	return new_vertices
+def sinusoidal_heightfield(num_x, num_z, height_scale=1.0, frequency_x=1.0):
+	"""
+	Creates the vertices corresponding to a sinusoidal heightfield along the X axis.
+	The height_scale controls the amplitude of the wave, and frequency_x its frequency.
+	"""
+	# TODO: fix the frequency units
+	verts=[]
+	for x in range(num_x):
+		for z in range(num_z):
+			verts.append( (x, mut.sin(x * frequency_x)*height_scale, z) )
+	return verts
+
+#===============================================================================
+# def shrink_XZ_heightfield(vertices, factor=(1.0,1.0)):
+#	"""
+#	test
+#	"""
+#	new_vertices = []
+#	for vertex in vertices:
+#		new_vertices.append((vertex[0]/factor[0], vertex[1], vertex[2]/factor[1]))
+#	return new_vertices
+#===============================================================================
+
+def constant_heightfield(num_x, num_z, height=0.0):
+	"""
+	A height field where all the values are the same.
+	"""
+	# that x and z are integers, not floats, does not matter
+	verts=[]
+	for x in range(num_x):
+		for z in range(num_z):
+			verts.append((x, height, z))
+	return verts
 
 class RSS2(Vehicle2):
 	
-	STEPS_PER_FRAME = 1000
+	STEPS_PER_FRAME = 500
+	OFFSET = (3,2,5)
 	
 	def __init__(self):
 		""" Constructor, calls first the superclass constructor. """
 		
 		tm_resolution = 1.0
 		tm_x, tm_z = (20,20)
-		shrink_factor = (1.0,1.0)
-		vertices = random_heightfield(tm_x, tm_z, 0.5)
-		vertices = shrink_XZ_heightfield(vertices, shrink_factor)
+		#shrink_factor = (1.0,1.0)
+		vertices = sinusoidal_heightfield(tm_x, tm_z, height_scale=0.7, frequency_x=0.5)
+		#vertices = constant_heightfield(tm_x, tm_z, height=0.0)
+		#vertices = random_heightfield(tm_x, tm_z, 0.5)
+		#vertices = shrink_XZ_heightfield(vertices, shrink_factor)
 		faces = Trimesh.get_faces(int(tm_resolution) * tm_x, int(tm_resolution) * tm_z)
+		faces = Trimesh.swap_faces_indices(faces)
 		
 		# set up the simulation parameters
 		self.sim = Simulation(self.FPS, self.STEPS_PER_FRAME)
 	
 	TORQUE = 300
 	
+	OFFSET = (3,1,3)
+	
 	def __init__(self):
 		""" Constructor, calls first the superclass constructor. """
 		Program.__init__(self)
 		'''
 		
 		z_axis = (0,0,1)
-		offset = (3,1,3)
+		offset = self.OFFSET
 		
 		wheelR = self.sim.add_cylinder(1, 0.2, 0.3, (0,0,-0.5))
 		wheelL = self.sim.add_cylinder(1, 0.2, 0.3, (0,0,0.5))