pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

<legend id='BWb3Q'><style id='BWb3Q'><dir id='BWb3Q'><q id='BWb3Q'></q></dir></style></legend>
    1. <tfoot id='BWb3Q'></tfoot>
      <i id='BWb3Q'><tr id='BWb3Q'><dt id='BWb3Q'><q id='BWb3Q'><span id='BWb3Q'><b id='BWb3Q'><form id='BWb3Q'><ins id='BWb3Q'></ins><ul id='BWb3Q'></ul><sub id='BWb3Q'></sub></form><legend id='BWb3Q'></legend><bdo id='BWb3Q'><pre id='BWb3Q'><center id='BWb3Q'></center></pre></bdo></b><th id='BWb3Q'></th></span></q></dt></tr></i><div class="jr7vlxx" id='BWb3Q'><tfoot id='BWb3Q'></tfoot><dl id='BWb3Q'><fieldset id='BWb3Q'></fieldset></dl></div>

    2. <small id='BWb3Q'></small><noframes id='BWb3Q'>

      • <bdo id='BWb3Q'></bdo><ul id='BWb3Q'></ul>

        避免鼠標移動的 QGraphicsItem 形狀的碰撞

        Avoiding collisions of QGraphicsItem shapes moved by the mouse(避免鼠標移動的 QGraphicsItem 形狀的碰撞)
        • <bdo id='abW9i'></bdo><ul id='abW9i'></ul>
          <tfoot id='abW9i'></tfoot>
          <legend id='abW9i'><style id='abW9i'><dir id='abW9i'><q id='abW9i'></q></dir></style></legend>

            <small id='abW9i'></small><noframes id='abW9i'>

          • <i id='abW9i'><tr id='abW9i'><dt id='abW9i'><q id='abW9i'><span id='abW9i'><b id='abW9i'><form id='abW9i'><ins id='abW9i'></ins><ul id='abW9i'></ul><sub id='abW9i'></sub></form><legend id='abW9i'></legend><bdo id='abW9i'><pre id='abW9i'><center id='abW9i'></center></pre></bdo></b><th id='abW9i'></th></span></q></dt></tr></i><div class="f5nvfln" id='abW9i'><tfoot id='abW9i'></tfoot><dl id='abW9i'><fieldset id='abW9i'></fieldset></dl></div>

                    <tbody id='abW9i'></tbody>
                  本文介紹了避免鼠標移動的 QGraphicsItem 形狀的碰撞的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  此處提出了一個有趣的討論,該討論是關于在 QGraphicsScene 中防止由 QGraphicsEllipseItems 構成的圓的碰撞.這個問題將范圍縮小到 2 個碰撞項目,但更大的目標仍然存在,對于任意數量的碰撞怎么辦?

                  An interesting discussion was raised here about preventing collisions of circles, made of QGraphicsEllipseItems, in a QGraphicsScene. The question narrowed the scope to 2 colliding items but the larger goal still remained, what about for any number of collisions?

                  這是期望的行為:

                  • 當一個項目被拖動到其他項目上時,它們不應重疊,而是應該在這些項目周圍移動,盡可能靠近鼠標.
                  • 如果它被其他物品阻擋,它不應該傳送".
                  • 應該是平穩且可預測的運動.

                  隨著為圓在移動時找到最佳安全"位置變得越來越復雜,我想展示另一種使用物理模擬器來實現它的方法.

                  As this becomes increasingly complex to find the best "safe" position for the circle while it’s moving I wanted to present another way to implement this using a physics simulator.

                  推薦答案

                  鑒于上面描述的行為,它是 2D 剛體物理的一個很好的候選者,也許沒有它可以做到,但很難做到完美.我在這個例子中使用 pymunk 因為我很熟悉它,但是相同的概念也適用于其他庫.

                  Given the behavior described above it’s a good candidate for 2D rigid body physics, maybe it can be done without but it would be difficult to get it perfect. I am using pymunk in this example because I’m familiar with it but the same concepts will work with other libraries.

                  場景有一個運動體來表示鼠標,而圓圈最初由靜態體表示.當一個圓圈被選中時,它會切換到一個動態物體,并通過一個阻尼彈簧約束到鼠標.它的位置隨著空間在每個超時間隔上按給定時間步更新而更新.

                  The scene has a kinematic body to represent the mouse and the circles are represented by static bodies initially. While a circle is selected it switches to a dynamic body and is constrained to the mouse by a damped spring. Its position is updated as the space is updated by a given time step on each timeout interval.

                  該項目實際上并未以與未啟用 ItemIsMovable 標志相同的方式移動,這意味著它不再隨鼠標立即移動.它非常接近,但有一點延遲,盡管您可能更喜歡這樣來更好地了解它對碰撞的反應.(即便如此,您也可以微調參數,讓它比我移動得更快/更靠近鼠標**).

                  The item is not actually moved in the same way as the ItemIsMovable flag is not enabled, which means it no longer moves instantly with the mouse. It’s very close but there’s a small delay, although you may prefer this to better see how it reacts to collisions. (Even so, you can fine-tune the parameters to have it move faster/closer to the mouse than I did**).

                  另一方面,碰撞得到了完美的處理,并且已經支持其他類型的形狀.

                  On the other hand, the collisions are handled perfectly and will already support other kinds of shapes.

                  import sys
                  from PyQt5.QtWidgets import *
                  from PyQt5.QtCore import *
                  from PyQt5.QtGui import *
                  import pymunk
                  
                  class Circle(QGraphicsEllipseItem):
                  
                      def __init__(self, r, **kwargs):
                          super().__init__(-r, -r, r * 2, r * 2, **kwargs)
                          self.setFlag(QGraphicsItem.ItemIsSelectable)
                          self.static = pymunk.Body(body_type=pymunk.Body.STATIC)
                          self.circle = pymunk.Circle(self.static, r)
                          self.circle.friction = 0
                          mass = 10
                          self.dynamic = pymunk.Body(mass, pymunk.moment_for_circle(mass, 0, r))
                          self.updatePos = lambda: self.setPos(*self.dynamic.position, dset=False)
                  
                      def setPos(self, *pos, dset=True):
                          super().setPos(*pos)
                          if len(pos) == 1:
                              pos = pos[0].x(), pos[0].y()
                          self.static.position = pos
                          if dset:
                              self.dynamic.position = pos
                  
                      def itemChange(self, change, value):
                          if change == QGraphicsItem.ItemSelectedChange:
                              space = self.circle.space
                              space.remove(self.circle.body, self.circle)
                              self.circle.body = self.dynamic if value else self.static
                              space.add(self.circle.body, self.circle)
                          return super().itemChange(change, value)
                  
                      def paint(self, painter, option, widget):
                          option.state &= ~QStyle.State_Selected
                          super().paint(painter, option, widget)
                  
                  
                  class Scene(QGraphicsScene):
                  
                      def __init__(self, *args, **kwargs):
                          super().__init__(*args, **kwargs)
                          self.space = pymunk.Space()
                          self.space.damping = 0.02
                          self.body = pymunk.Body(body_type=pymunk.Body.KINEMATIC)
                          self.space.add(self.body)
                          self.timer = QTimer(self, timerType=Qt.PreciseTimer, timeout=self.step)
                          self.selectionChanged.connect(self.setConstraint)
                  
                      def setConstraint(self):
                          selected = self.selectedItems()
                          if selected:
                              shape = selected[0].circle
                              if not shape.body.constraints:
                                  self.space.remove(*self.space.constraints)
                                  spring = pymunk.DampedSpring(
                                      self.body, shape.body, (0, 0), (0, 0),
                                      rest_length=0, stiffness=100, damping=10)
                                  spring.collide_bodies = False
                                  self.space.add(spring)
                  
                      def step(self):
                          for i in range(10):
                              self.space.step(1 / 30)
                          self.selectedItems()[0].updatePos()
                  
                      def mousePressEvent(self, event):
                          super().mousePressEvent(event)
                          if self.selectedItems():
                              self.body.position = event.scenePos().x(), event.scenePos().y()
                              self.timer.start(1000 / 30)
                              
                      def mouseMoveEvent(self, event):            
                          super().mouseMoveEvent(event)
                          if self.selectedItems():
                              self.body.position = event.scenePos().x(), event.scenePos().y()
                          
                      def mouseReleaseEvent(self, event):
                          super().mouseReleaseEvent(event)
                          self.timer.stop()
                  
                      def addCircle(self, x, y, radius):
                          item = Circle(radius)
                          item.setPos(x, y)
                          self.addItem(item)
                          self.space.add(item.circle.body, item.circle)
                          return item
                  
                  
                  if __name__ == '__main__':
                      app = QApplication(sys.argv)
                      scene = Scene(0, 0, 1000, 800)
                      for i in range(7, 13):
                          item = scene.addCircle(150 * (i - 6), 400, i * 5)
                          item.setBrush(Qt.GlobalColor(i))    
                      view = QGraphicsView(scene, renderHints=QPainter.Antialiasing)
                      view.show()
                      sys.exit(app.exec_())
                  

                  **可以調整如下:

                  • 彈簧剛度阻尼
                  • 身體質量慣性矩
                  • 空間阻尼
                  • Space.step 時間步長/每個 QTimer 超時的調用次數
                  • QTimer 間隔
                  • Spring stiffness and damping
                  • Body mass and moment of inertia
                  • Space damping
                  • Space.step time step / how many calls per QTimer timeout
                  • QTimer interval

                  這篇關于避免鼠標移動的 QGraphicsItem 形狀的碰撞的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  How to bind a function to an Action from Qt menubar?(如何將函數綁定到 Qt 菜單欄中的操作?)
                  PyQt progress jumps to 100% after it starts(PyQt 啟動后進度躍升至 100%)
                  How to set yaxis tick label in a fixed position so that when i scroll left or right the yaxis tick label should be visible?(如何將 yaxis 刻度標簽設置在固定位置,以便當我向左或向右滾動時,yaxis 刻度標簽應該可見
                  `QImage` constructor has unknown keyword `data`(`QImage` 構造函數有未知關鍵字 `data`)
                  Change x-axis ticks to custom strings(將 x 軸刻度更改為自定義字符串)
                  How to show progress bar while saving file to excel in python?(如何在python中將文件保存為excel時顯示進度條?)

                  <small id='Tw0Tk'></small><noframes id='Tw0Tk'>

                      <bdo id='Tw0Tk'></bdo><ul id='Tw0Tk'></ul>
                    • <tfoot id='Tw0Tk'></tfoot>

                        <tbody id='Tw0Tk'></tbody>

                          <i id='Tw0Tk'><tr id='Tw0Tk'><dt id='Tw0Tk'><q id='Tw0Tk'><span id='Tw0Tk'><b id='Tw0Tk'><form id='Tw0Tk'><ins id='Tw0Tk'></ins><ul id='Tw0Tk'></ul><sub id='Tw0Tk'></sub></form><legend id='Tw0Tk'></legend><bdo id='Tw0Tk'><pre id='Tw0Tk'><center id='Tw0Tk'></center></pre></bdo></b><th id='Tw0Tk'></th></span></q></dt></tr></i><div class="x7zrjrx" id='Tw0Tk'><tfoot id='Tw0Tk'></tfoot><dl id='Tw0Tk'><fieldset id='Tw0Tk'></fieldset></dl></div>
                            <legend id='Tw0Tk'><style id='Tw0Tk'><dir id='Tw0Tk'><q id='Tw0Tk'></q></dir></style></legend>
                            主站蜘蛛池模板: 培训中心-海南香蕉蛋糕加盟店技术翰香原中心官网总部 | 米顿罗计量泵(科普)——韬铭机械 | 纸塑分离机-纸塑分离清洗机设备-压力筛-碎浆机厂家金双联环保 | 大流量卧式砂磨机_强力分散机_双行星双动力混合机_同心双轴搅拌机-莱州市龙跃化工机械有限公司 | OpenI 启智 新一代人工智能开源开放平台 | 立式硫化罐-劳保用品硫化罐-厂家直销-山东鑫泰鑫硫化罐厂家 | 河南卓美创业科技有限公司-河南卓美防雷公司-防雷接地-防雷工程-重庆避雷针-避雷器-防雷检测-避雷带-避雷针-避雷塔、机房防雷、古建筑防雷等-山西防雷公司 | 起好名字_取个好名字_好名网免费取好名在线打分 | PE拉伸缠绕膜,拉伸缠绕膜厂家,纳米缠绕膜-山东凯祥包装 | 自动记录数据电子台秤,记忆储存重量电子桌称,设定时间记录电子秤-昆山巨天 | 深圳善跑体育产业集团有限公司_塑胶跑道_人造草坪_运动木地板 | 悬浮拼装地板_篮球场木地板翻新_运动木地板价格-上海越禾运动地板厂家 | 铝机箱_铝外壳加工_铝外壳厂家_CNC散热器加工-惠州市铂源五金制品有限公司 | 物流公司电话|附近物流公司电话上门取货 | 数控走心机-走心机价格-双主轴走心机-宝宇百科 | 动力配电箱-不锈钢配电箱-高压开关柜-重庆宇轩机电设备有限公司 聚天冬氨酸,亚氨基二琥珀酸四钠,PASP,IDS - 远联化工 | 湖南印刷厂|长沙印刷公司|画册印刷|挂历印刷|台历印刷|杂志印刷-乐成印刷 | LHH药品稳定性试验箱-BPS系列恒温恒湿箱-意大利超低温冰箱-上海一恒科学仪器有限公司 | 合肥花魁情感婚姻咨询中心_挽回爱情_修复婚姻_恋爱指南 | 砖机托板价格|免烧砖托板|空心砖托板厂家_山东宏升砖机托板厂 | 物流公司电话|附近物流公司电话上门取货 | 有机肥设备生产制造厂家,BB掺混肥搅拌机、复合肥设备生产线,有机肥料全部加工设备多少钱,对辊挤压造粒机,有机肥造粒设备 -- 郑州程翔重工机械有限公司 | 钢绞线万能材料试验机-全自动恒应力两用机-混凝土恒应力压力试验机-北京科达京威科技发展有限公司 | HDPE土工膜,复合土工膜,防渗膜价格,土工膜厂家-山东新路通工程材料有限公司 | 水平垂直燃烧试验仪-灼热丝试验仪-漏电起痕试验仪-针焰试验仪-塑料材料燃烧检测设备-IP防水试验机 | 商用绞肉机-熟肉切片机-冻肉切丁机-猪肉开条机 - 广州市正盈机械设备有限公司 | 吸音板,隔音板,吸音材料,吸音板价格,声学材料 - 佛山诺声吸音板厂家 | 上海冠顶工业设备有限公司-隧道炉,烘箱,UV固化机,涂装设备,高温炉,工业机器人生产厂家 | 脉冲布袋除尘器_除尘布袋-泊头市净化除尘设备生产厂家 | 风淋室生产厂家报价_传递窗|送风口|臭氧机|FFU-山东盛之源净化设备 | 北京燃气公司 用户服务中心 | 货车视频监控,油管家,货车油管家-淄博世纪锐行电子科技 | 国产离子色谱仪,红外分光测油仪,自动烟尘烟气测试仪-青岛埃仑通用科技有限公司 | 自动部分收集器,进口无油隔膜真空泵,SPME固相微萃取头-上海楚定分析仪器有限公司 | 卡诺亚轻高定官网_卧室系统_整家定制_定制家居_高端定制_全屋定制加盟_定制家具加盟_定制衣柜加盟 | 飞行者联盟-飞机模拟机_无人机_低空经济_航空技术交流平台 | 网架支座@球铰支座@钢结构支座@成品支座厂家@万向滑动支座_桥兴工程橡胶有限公司 | 木材烘干机,木炭烘干机,纸管/佛香烘干设备-河南蓝天机械制造有限公司 | 信阳网站建设专家-信阳时代网联-【信阳网站建设百度推广优质服务提供商】信阳网站建设|信阳网络公司|信阳网络营销推广 | 螺旋压榨机-刮泥机-潜水搅拌机-电动泥斗-潜水推流器-南京格林兰环保设备有限公司 | 除尘器布袋骨架,除尘器滤袋,除尘器骨架,电磁脉冲阀膜片,卸灰阀,螺旋输送机-泊头市天润环保机械设备有限公司 |