Physics save and load example¶
physics_save_load_example.vpb¶
1 # This example shows how collision data can be made persistent.
2 # The PhysX collision shapes can be serialized with vrPhysicsService.storeCollisionData()
3 # and deserialized with vrPhysicsService.restoreCollisionData().
4 # The serialized data is stored in an attachment of the node and will then be
5 # saved with the VPB.
6 # Please note that the underlying data is platform and version dependent. Should the
7 # PhysX library change the format in a future version, the saved data will be automatically
8 # discarded and recreated using the same parameters as it has been saved with originally.
9
10 # fetch the 3 nodes from the scene
11 box1 = vrNodeService.findNode("Box1")
12 box2 = vrNodeService.findNode("Box2")
13 box3 = vrNodeService.findNode("Box3")
14
15
16 # This function checks if a node has saved collision data. If yes, it will be restored.
17 # If there is no saved collision data, it will be created new and then stored in the node.
18 def createOrLoadCollisionData(node, config):
19 if vrPhysicsService.hasCollisionData(node):
20 vrPhysicsService.restoreCollisionData(node)
21 print("restored: " + node.getName())
22 else:
23 vrPhysicsService.addKinematicObject(node, config)
24 vrPhysicsService.storeCollisionData(node)
25 print("created new: " + node.getName())
26
27 convexHullConf = vrdPhysicsHullConfig()
28
29 for node in (box1, box2, box3):
30 # Uncomment the following line to remove saved collision shapes and
31 # force them to be recreated
32 #vrPhysicsService.clearCollisionData(node)
33 createOrLoadCollisionData(node, convexHullConf)
34
35 vrPhysicsService.setActive(True)
36 #vrPhysicsService.connectDebugger()
37
38 def newSceneCallback():
39 vrPhysicsService.collisionStarted.disconnect()
40 vrPhysicsService.collisionStopped.disconnect()
41 vrPhysicsService.collisionContinues.disconnect()
42
43 setNewSceneCB(newSceneCallback)
44
45 # Collision callbacks for highlighting the colliding nodes
46
47 def collisionStart(nodeInfo):
48 r1 = nodeInfo.getCollidingRootNode1()
49 r2 = nodeInfo.getCollidingRootNode2()
50 r1.getParent().setEnabled(True)
51 r2.getParent().setEnabled(True)
52
53 def collisionStopped(nodeInfo):
54 r1 = nodeInfo.getCollidingRootNode1()
55 r2 = nodeInfo.getCollidingRootNode2()
56 r1.getParent().setEnabled(False)
57 r2.getParent().setEnabled(False)
58
59 def collisionContinues(nodeInfo):
60 r1 = nodeInfo.getCollidingRootNode1()
61 r2 = nodeInfo.getCollidingRootNode2()
62 r1.getParent().setEnabled(True)
63 r2.getParent().setEnabled(True)
64
65
66 vrPhysicsService.collisionStarted.connect(collisionStart)
67 vrPhysicsService.collisionStopped.connect(collisionStopped)
68 vrPhysicsService.collisionContinues.connect(collisionContinues)