Tracking a physics object in Python

From Digital Spaces

Jump to: navigation, search

We recommend reading Basic rotation in Python before this example.

In this example, we will use the scene graph manager to find a scene node, then convert it to it's physics body representation. We will use the physics body representation to track the bodies velocity.

We also demonstrate the use of the DInterface.cast function, which is equivalent to the DigitalSpaces::dss_cast<DInterface> function used in the C++ API.

if 'trackBody' not in locals().keys():
	class BodyTracker:
		""" This class takes a node name, or a list of node names, and tracks the
			linear velocity of the physics body representations of the nodes.
		"""
		def __init__(self, nodeNames):
			self.physicsBodies = list()
			if isinstance( nodeNames, str ):
				nodeNames = ( nodeNames, )
 

This section queries all the available scene graph managers for nodes of the specified names. There should only be one, but GetFactoriesWithInterface always returns a list. Using every scene graph manager, for each scene node name provided, check for a scene node by that name. If found, check if it is also a physics body. If so, store the interface to the physics body.

# Junk comment for wiki formatter
			for scenegraphManager in dss_core.DISGManager.GetFactoriesWithInterface():
				for nodeName in nodeNames:
					sceneNode = scenegraphManager.GetSGNode( nodeName )
					if sceneNode:
						physicsBody = dss_core.DIPhysicsBody.cast( sceneNode )
						if physicsBody:
							self.physicsBodies.append( physicsBody )
			
		def PerformHeartbeat(self):
			""" This function will be called each run of the script, which is each Heartbeat of Digital Spaces.
			"""
 

For each physics body, print both its name and its linear velocity. The linear velocity will be returned as a vector, which is a three value tuple. Python will automatically format this. In Digital Spaces, the Python print statement is redirected to the file DigitalSpaces.log

# Junk comment for wiki formatter
			for physicsBody in self.physicsBodies:
				print physicsBody.GetName(), ":", physicsBody.GetLinearVelocity()
				
	# 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()
 

When used with an appropriate scene node/body, this will log the bodies velocity every heartbeat. An appropriate .scene declaration would be:

<body name="vehicle_chassis" body="true">
	<mass value="1" x="1" y="1" z="1" />
	<damping linear="0.2" angular="0.2" />
	<entity name="vehicle_chassis_entity" meshFile="vehicle_chassis.mesh" />
	<shape type="boundingbox" entity="vehicle_chassis_entity" group="1" />
</body>
 

This needs to be used in a .space where physics is marked as required. Additionally the .space <environment> section needs to include the required information for physics simulation, such as gravity.

Personal tools