ya2 · news · projects · code · about

instantiation
authorFlavio Calva <f.calva@gmail.com>
Sat, 8 Jan 2022 17:12:19 +0000 (18:12 +0100)
committerFlavio Calva <f.calva@gmail.com>
Sat, 8 Jan 2022 17:12:19 +0000 (18:12 +0100)
assets/fonts/Hanken-Book.ttf [new file with mode: 0644]
pmachines/items/box.py
pmachines/scene.py
prj.org

diff --git a/assets/fonts/Hanken-Book.ttf b/assets/fonts/Hanken-Book.ttf
new file mode 100644 (file)
index 0000000..c690722
Binary files /dev/null and b/assets/fonts/Hanken-Book.ttf differ
index c0419bba0ce8b84840c7f8f6f2d8d28a9d29e889..493ae2a26766d046a5df3d773c1e7b5eb7ff7120 100644 (file)
@@ -1,15 +1,19 @@
-from panda3d.core import CullFaceAttrib
+from panda3d.core import CullFaceAttrib, Point3, NodePath, Point2, Texture
 from panda3d.bullet import BulletBoxShape, BulletRigidBodyNode
 from panda3d.bullet import BulletBoxShape, BulletRigidBodyNode
+from direct.gui.OnscreenText import OnscreenText
+
 
 class Box:
 
 
 class Box:
 
-    def __init__(self, world):
+    def __init__(self, world, plane_node, count, cb_inst):
         self._world = world
         self._world = world
+        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._np = render.attach_new_node(self.node)
         shape = BulletBoxShape((.5, .5, .5))
         self.node = BulletRigidBodyNode('box')
         self.node.add_shape(shape)
         self._np = render.attach_new_node(self.node)
-        self._np.set_pos(0, 0, 1)
         world.attach_rigid_body(self.node)
         model = loader.load_model('assets/gltf/box/box.gltf')
         model.flatten_light()
         world.attach_rigid_body(self.node)
         model = loader.load_model('assets/gltf/box/box.gltf')
         model.flatten_light()
@@ -17,7 +21,40 @@ class Box:
         self._set_outline_model()
         self._start_drag_pos = None
         self._prev_rot_info = None
         self._set_outline_model()
         self._start_drag_pos = None
         self._prev_rot_info = None
+        self._instantiated = False
         taskMgr.add(self.on_frame, 'on_frame')
         taskMgr.add(self.on_frame, 'on_frame')
+        #self._set_side()
+        taskMgr.doMethodLater(.01, lambda task: self._set_side(), 'a')
+        # i get weird values in the first frame; i could restore this approach
+        # when the hook with the events of the window is up
+
+    def _set_side(self):
+        p_from, p_to = Point3(), Point3()    # in camera coordinates
+        base.camLens.extrude((-1, 1), p_from, p_to)
+        p_from = render.get_relative_point(base.cam, p_from)  # global coords
+        p_to = render.get_relative_point(base.cam, p_to)  # global coords
+        for hit in self._world.ray_test_all(p_from, p_to).get_hits():
+            if hit.get_node() == self._plane_node:
+                pos = hit.get_hit_pos()
+        bounds = self._np.get_tight_bounds()
+        dpos = bounds[1][0], 0, -bounds[1][2]
+        self._np.set_pos(pos + dpos)
+        new_node = NodePath('temp')
+        new_node.set_pos(pos + dpos + (bounds[1][0], bounds[1][1], -bounds[1][2]))
+        coord3d = new_node.get_pos(base.cam)
+        coord2d = Point2()
+        base.camLens.project(coord3d, coord2d)
+        coord_r2d = Point3(coord2d[0], 0, coord2d[1])
+        coord_a2d = base.aspect2d.get_relative_point(render2d, coord_r2d)
+        font = base.loader.load_font('assets/fonts/Hanken-Book.ttf')
+        font.clear()
+        font.set_pixels_per_unit(60)
+        font.set_minfilter(Texture.FTLinearMipmapLinear)
+        font.set_outline((0, 0, 0, 1), .8, .2)
+        self._txt = OnscreenText(
+            str(self._count), pos=(coord_a2d[0], coord_a2d[2]),
+            font=font, scale=0.06, fg=(.9, .9, .9, 1))
+        new_node.remove_node()
 
     def _set_outline_model(self):
         self._outline_model = loader.load_model('assets/gltf/box/box.gltf')
 
     def _set_outline_model(self):
         self._outline_model = loader.load_model('assets/gltf/box/box.gltf')
@@ -42,6 +79,13 @@ class Box:
     def on_click_l(self, pos):
         self._start_drag_pos = pos, self._np.get_pos()
         loader.load_sfx('assets/audio/sfx/grab.ogg').play()
     def on_click_l(self, pos):
         self._start_drag_pos = pos, self._np.get_pos()
         loader.load_sfx('assets/audio/sfx/grab.ogg').play()
+        if not self._instantiated:
+            self._instantiated = True
+            self._txt.destroy()
+            self._count -= 1
+            if self._count:
+                box = Box(self._world, self._plane_node, self._count, self._cb_inst)
+                self._cb_inst(box)
 
     def on_click_r(self, pos):
         self._prev_rot_info = pos, self._np.get_pos(), self._np.get_r()
 
     def on_click_r(self, pos):
         self._prev_rot_info = pos, self._np.get_pos(), self._np.get_r()
index f67c45b75587804f3ca316eb6455df4e94c548eb..5d046e9b925b15b50b30d140e1562d75f43590cb 100644 (file)
@@ -23,7 +23,7 @@ class Scene(DirectObject):
         self._set_input()
         self._set_mouse_plane()
         Background()
         self._set_input()
         self._set_mouse_plane()
         Background()
-        self.items = [Box(world)]
+        self.items = [Box(world, self._mouse_plane_node, 3, self.cb_inst)]
         taskMgr.add(self.on_frame, 'on_frame')
 
     def _set_camera(self):
         taskMgr.add(self.on_frame, 'on_frame')
 
     def _set_camera(self):
@@ -111,8 +111,7 @@ class Scene(DirectObject):
 
     def _get_hits(self):
         if not base.mouseWatcherNode.has_mouse(): return []
 
     def _get_hits(self):
         if not base.mouseWatcherNode.has_mouse(): return []
-        p_from = Point3()  # in camera coordinates
-        p_to = Point3()    # in camera coordinates
+        p_from, p_to = Point3(), Point3()    # in camera coordinates
         base.camLens.extrude(base.mouseWatcherNode.get_mouse(), p_from, p_to)
         p_from = render.get_relative_point(base.cam, p_from)  # global coords
         p_to = render.get_relative_point(base.cam, p_to)  # global coords
         base.camLens.extrude(base.mouseWatcherNode.get_mouse(), p_from, p_to)
         p_from = render.get_relative_point(base.cam, p_from)  # global coords
         p_to = render.get_relative_point(base.cam, p_to)  # global coords
@@ -153,6 +152,9 @@ class Scene(DirectObject):
             [itm.on_mouse_move(pos) for itm in self.items]
         return task.cont
 
             [itm.on_mouse_move(pos) for itm in self.items]
         return task.cont
 
+    def cb_inst(self, item):
+        self.items += [item]
+
     def on_play(self):
         [itm.play() for itm in self.items]
 
     def on_play(self):
         [itm.play() for itm in self.items]
 
diff --git a/prj.org b/prj.org
index 311ee94b199864a8da56997c8da5a91cbb126c5d..f4c05e216f90cbac72fc183fbecbebc02a0ac6e3 100644 (file)
--- a/prj.org
+++ b/prj.org
@@ -1,8 +1,7 @@
 * issues
 * todo
 * issues
 * todo
-** instantiation of the objects
-*** model in the left side with a counter
-*** when you click a new object is created iff counter > 0
+** update the to-be-instantiated's pos if the windows is updated
+** manage the physics of the to-be-instantiated with a ghost
 ** instructions
 ** main menu
 ** implement the operations of the buttons
 ** instructions
 ** main menu
 ** implement the operations of the buttons