ya2 · news · projects · code · about

rotation
authorFlavio Calva <f.calva@gmail.com>
Fri, 7 Jan 2022 19:41:20 +0000 (20:41 +0100)
committerFlavio Calva <f.calva@gmail.com>
Fri, 7 Jan 2022 19:41:20 +0000 (20:41 +0100)
pmachines/items/box.py
pmachines/scene.py
prj.org

index 52c577b9984cbbdc0c5e18747f286c09fb26aa48..e7add63e21493bd6a9eab131d205710dff9f0cf6 100644 (file)
@@ -16,6 +16,8 @@ class Box:
         model.reparent_to(self._np)
         self._set_outline_model()
         self._start_drag_pos = None
+        self._start_rot_info = None
+        taskMgr.add(self.on_frame, 'on_frame')
 
     def _set_outline_model(self):
         self._outline_model = loader.load_model('assets/gltf/box/box.gltf')
@@ -28,23 +30,37 @@ class Box:
         self._outline_model.set_color_scale(.4, .4, .4, 1)
         self._outline_model.hide()
 
+    def on_frame(self, task):
+        self._np.set_y(0)
+        return task.cont
+
     def play(self):
         self._world.remove_rigid_body(self.node)
         self.node.set_mass(1)
         self._world.attach_rigid_body(self.node)
 
-    def on_click(self, pos):
+    def on_click_l(self, pos):
         self._start_drag_pos = pos, self._np.get_pos()
 
+    def on_click_r(self, pos):
+        self._start_rot_info = pos, self._np.get_pos(), self._np.get_r()
+
     def on_release(self):
-        self._start_drag_pos = None
+        self._start_drag_pos = self._start_rot_info = None
 
     def on_mouse_on(self):
         self._outline_model.show()
 
     def on_mouse_off(self):
+        if self._start_drag_pos or self._start_rot_info: return
         self._outline_model.hide()
 
     def on_mouse_move(self, pos):
-        if not self._start_drag_pos: return
-        self._np.set_pos(self._start_drag_pos[1] + (pos - self._start_drag_pos[0]))
+        if self._start_drag_pos:
+            d_pos =  pos - self._start_drag_pos[0]
+            self._np.set_pos(self._start_drag_pos[1] + d_pos)
+        if self._start_rot_info:
+            start_vec = self._start_rot_info[0] - self._start_rot_info[1]
+            curr_vec = pos - self._start_rot_info[1]
+            d_angle = curr_vec.signed_angle_deg(start_vec, (0, -1, 0))
+            self._np.set_r(self._start_rot_info[2] + d_angle)
index bd7d9bfaf3d02192ef27163af439899667ff9a62..dbc6282a6dcf22a8764b0b142ecd7d1aa3443a7a 100644 (file)
@@ -41,8 +41,10 @@ class Scene(DirectObject):
         self._set_directional_light('rim light', (75, -30, 0), (.3, .3, .3, 1))
 
     def _set_input(self):
-        self.accept('mouse1', self.on_click)
+        self.accept('mouse1', self.on_click_l)
         self.accept('mouse1-up', self.on_release)
+        self.accept('mouse3', self.on_click_r)
+        self.accept('mouse3-up', self.on_release)
         self.accept('p-up', self.on_play)
 
     def _set_mouse_plane(self):
@@ -63,18 +65,22 @@ class Scene(DirectObject):
         p_to = render.get_relative_point(base.cam, p_to)  # global coords
         return self._world.ray_test_all(p_from, p_to).get_hits()
 
-    def on_click(self):
+    def _on_click(self, method):
         for hit in self._get_hits():
             if hit.get_node() == self._mouse_plane_node:
                 pos = hit.get_hit_pos()
         for hit in self._get_hits():
             for item in [i for i in self.items if hit.get_node() == i.node]:
-                item.on_click(pos)
+                getattr(item, method)(pos)
+
+    def on_click_l(self):
+        self._on_click('on_click_l')
+
+    def on_click_r(self):
+        self._on_click('on_click_r')
 
     def on_release(self):
-        for hit in self._get_hits():
-            for item in [i for i in self.items if hit.get_node() == i.node]:
-                item.on_release()
+        [item.on_release() for item in self.items]
 
     def on_frame(self, task):
         hits = self._get_hits()
diff --git a/prj.org b/prj.org
index 18187c345cd11a9be05d782cb638e83c7c6fc552..a10e4f2762cb5f54a768f9498fd5ea4df9b1babf 100644 (file)
--- a/prj.org
+++ b/prj.org
@@ -1,6 +1,5 @@
 * issues
 * todo
-** objects rotation
 ** buttons
 ** mouse cursor
 ** main menu