ya2 · news · projects · support us · follow us · lists · code · about

ghosts for instances
authorFlavio Calva <f.calva@gmail.com>
Thu, 13 Jan 2022 18:54:48 +0000 (19:54 +0100)
committerFlavio Calva <f.calva@gmail.com>
Thu, 13 Jan 2022 18:54:48 +0000 (19:54 +0100)
pmachines/items/box.py
pmachines/scene.py
prj.org

index 90d7d3fea14def966b51d7eb24a9f41fe541e786..2e65777442d2971631254bed99365e22cb878543 100644 (file)
@@ -1,5 +1,5 @@
 from panda3d.core import CullFaceAttrib, Point3, NodePath, Point2, Texture
-from panda3d.bullet import BulletBoxShape, BulletRigidBodyNode
+from panda3d.bullet import BulletBoxShape, BulletRigidBodyNode, BulletGhostNode
 from direct.gui.OnscreenText import OnscreenText
 from lib.lib.p3d.gfx import P3dGfxMgr
 
@@ -10,14 +10,14 @@ class Box:
         self._plane_node = plane_node
         self._count = count
         self._cb_inst = cb_inst
-        shape = BulletBoxShape((.5, .5, .5))
-        self.node = BulletRigidBodyNode('box')
-        self.node.add_shape(shape)
+        self._shape = BulletBoxShape((.5, .5, .5))
+        self.node = BulletGhostNode('box')
+        self.node.add_shape(self._shape)
         self._np = render.attach_new_node(self.node)
-        world.attach_rigid_body(self.node)
-        model = loader.load_model('assets/gltf/box/box.gltf')
-        model.flatten_light()
-        model.reparent_to(self._np)
+        world.attach_ghost(self.node)
+        self._model = loader.load_model('assets/gltf/box/box.gltf')
+        self._model.flatten_light()
+        self._model.reparent_to(self._np)
         self._set_outline_model()
         self._start_drag_pos = None
         self._prev_rot_info = None
@@ -83,6 +83,8 @@ class Box:
         return task.cont
 
     def play(self):
+        if not self._instantiated:
+            return
         self._world.remove_rigid_body(self.node)
         self.node.set_mass(1)
         self._world.attach_rigid_body(self.node)
@@ -91,6 +93,16 @@ class Box:
         self._start_drag_pos = pos, self._np.get_pos()
         loader.load_sfx('assets/audio/sfx/grab.ogg').play()
         if not self._instantiated:
+            self._world.remove_ghost(self.node)
+            pos = self._np.get_pos()
+            self._np.remove_node()
+            self.node = BulletRigidBodyNode('box')
+            self.node.add_shape(self._shape)
+            self._np = render.attach_new_node(self.node)
+            self._world.attach_rigid_body(self.node)
+            self._model.reparent_to(self._np)
+            self._np.set_pos(pos)
+            self._set_outline_model()
             self._instantiated = True
             self._txt.destroy()
             self._count -= 1
index 2d5838addfce89d6f4608230f74b9a0bd83d7d95..7ad90be1dad417210055d9e184d45c6859da919e 100644 (file)
@@ -110,7 +110,7 @@ class Scene(DirectObject):
         self._mouse_plane_node.addShape(shape)
         #np = render.attachNewNode(self._mouse_plane_node)
         #self._world.attachRigidBody(self._mouse_plane_node)
-        self._world.attachGhost(self._mouse_plane_node)
+        self._world.attach_ghost(self._mouse_plane_node)
 
     def _get_hits(self):
         if not base.mouseWatcherNode.has_mouse(): return []
diff --git a/prj.org b/prj.org
index f12568da463690a517e3693d7cdb43d4ae6b1eb5..85e8a4628bfe0a5965a94586fc2f88069826dfe9 100644 (file)
--- a/prj.org
+++ b/prj.org
@@ -1,6 +1,5 @@
 * issues
 * todo
-** manage the physics of the to-be-instantiated with a ghost
 ** instructions
 ** main menu
 ** implement the operations of the buttons