Collision Example 3

collision3.py
 1# © 2024 Autodesk, Inc. All rights reserved.
 2
 3# Collsion demo
 4
 5newScene()
 6
 7#####################################################
 8# define special event class that only triggers     #
 9# when all supplied collision objects are colliding #
10#####################################################
11class CollisionAnd(vrAEBase):
12    def __init__(self, cols):
13        vrAEBase.__init__(self)
14        self.addLoop()
15        self.cols = cols
16        self.setActive(true)
17    def recEvent(self, state):
18        vrAEBase.recEvent(self, state)
19    def loop(self):
20        if self.isActive() == true:
21            collide = 0
22            l = len(self.cols)
23            for i in range(l):
24                if not self.cols[i].isColliding():
25                    break
26                else:
27                    collide += 1
28            if collide == l:
29                self.callAllConnected()
30
31
32########################
33# create some geometry #
34########################
35obja = createBox(1, 1, 1, 1, 1, 1, 1, 0, 0, 0)
36obja.makeTransform()
37obja.setTranslation(-5, 0, 0)
38obja.setName("Object_A")
39
40objb = createBox(1, 1, 1, 1, 1, 1, 0, 1, 0, 0)
41objb.makeTransform()
42objb.setTranslation(5, 0, 0)
43objb.setName("Object_B")
44
45objc = createBox(1, 1, 1, 1, 1, 1, 0, 0, 1, 0)
46objc.makeTransform()
47objc.setTranslation(-5, 5, 0)
48objc.setName("Object_C")
49
50objd = createBox(1, 1, 1, 1, 1, 1, 0, 1, 1, 0)
51objd.makeTransform()
52objd.setTranslation(5, 5, 0)
53objd.setName("Object_D")
54
55updateScene()
56
57#################################
58# create some collision objects #
59#################################
60colla = vrCollision([obja], [objb])
61collb = vrCollision([objc], [objd])
62
63#########################################
64# instantiate special collision object  #
65# that only prints some special message #
66# when obja collides with objb and objc #
67# with objd                             #
68#########################################
69collab = CollisionAnd([colla, collb])
70collab.connect("print 'WUMM!'")
71
72print("just move Object_A so it collides with Object_B\n and Object_C so it collides with Object_D.")