Tracking physics collisions in Python

From Digital Spaces

Jump to: navigation, search
if 'trackBody' not in locals().keys():
	class BodyTracker:
		""" This class takes a node name, or a list of node names, and checks the
			collisions occuring in the simulation, to see if the body names match any 
			of the supplied names
		"""
		physicsFactories = dss_core.DIPhysics.GetFactoriesWithInterface()
		
		def __init__(self, nodeNames):
			if isinstance( nodeNames, str ):
				self.nodeNames = ( nodeNames, )
			else:
				self.nodeNames = nodeNames
		
		def PerformHeartbeat(self):
			""" This function will be called each run of the script, which is each Heartbeat of Digital Spaces.
			"""
			for physicsFactory in self.physicsFactories:
				recentCollisions = physicsFactory.GetCollisionsLastHeartbeat()
				for collisionShapeOne,collisionShapeOther in recentCollisions:
					if collisionShapeOne in self.nodeNames:
						print "Collision occured involving", collisionShapeOne
					if collisionShapeOther in self.nodeNames:
						print "Collision occured involving", collisionShapeOther
	
	# Create an instance of the BodyTracker class, providing it with the name of the scene node to be tracked
	trackBody = BodyTracker("vehicle_chassis")
# Each time this script is ran (each Digital Spaces Heartbeat), call BodyTracker::PerformHeartbeat
trackBody.PerformHeartbeat()
 
Personal tools