ya2 · news · projects · code · about

rotation
[pmachines.git] / pmachines / items / box.py
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)