惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
Netflix TechBlog - Medium
N
News and Events Feed by Topic
Latest news
Latest news
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
The Hacker News
The Hacker News
T
The Exploit Database - CXSecurity.com
S
Schneier on Security
Cisco Talos Blog
Cisco Talos Blog
Hacker News: Ask HN
Hacker News: Ask HN
Simon Willison's Weblog
Simon Willison's Weblog
W
WeLiveSecurity
H
Hacker News: Front Page
L
LINUX DO - 热门话题
T
Tor Project blog
N
News | PayPal Newsroom
S
Secure Thoughts
小众软件
小众软件
Recent Commits to openclaw:main
Recent Commits to openclaw:main
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
AWS News Blog
AWS News Blog
Security Latest
Security Latest
MyScale Blog
MyScale Blog
Webroot Blog
Webroot Blog
Cyberwarzone
Cyberwarzone
Engineering at Meta
Engineering at Meta
腾讯CDC
T
Tailwind CSS Blog
SecWiki News
SecWiki News
博客园 - 司徒正美
Microsoft Azure Blog
Microsoft Azure Blog
P
Palo Alto Networks Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
V2EX
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
B
Blog RSS Feed
大猫的无限游戏
大猫的无限游戏
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Martin Fowler
Martin Fowler
P
Proofpoint News Feed
GbyAI
GbyAI
C
Check Point Blog
博客园_首页
PCI Perspectives
PCI Perspectives
月光博客
月光博客
TaoSecurity Blog
TaoSecurity Blog
aimingoo的专栏
aimingoo的专栏
酷 壳 – CoolShell
酷 壳 – CoolShell
IT之家
IT之家

埃氮幂の命名空间

埃氮幂の命名空间 埃氮幂の命名空间 埃氮幂の命名空间 埃氮幂の命名空间 埃氮幂の命名空间 埃氮幂の命名空间 埃氮幂の命名空间 埃氮幂の命名空间 埃氮幂の命名空间 埃氮幂の命名空间 埃氮幂の命名空间 埃氮幂の命名空间 埃氮幂の命名空间 埃氮幂の命名空间 埃氮幂の命名空间 埃氮幂の命名空间 埃氮幂の命名空间
埃氮幂の命名空间
2024-09-28 · via 埃氮幂の命名空间

前言

为什么要写物理引擎

其实在很久以前就用过别人的物理引擎了,可是这些引擎的功能不满足我的需求,比如说不支持流体的运动,以及实现把一个物体切割成两部分等,还可以制造燃烧,爆炸等效果,别人的物理引擎都没有。所以我决定自己先用python写一款物理引擎,后面再写成JavaScript来实现网页上的效果。

本文的编号是零,意思是这是写物理引擎前的准备工作。

物理引擎的结构

组成物理引擎的是窗口(Window),力(Force),物体(Object)和效果(Effect)。

窗口就是一个物理场,它记录了很多物理常量(如重力加速度等)

同时,它也是物体的载体,可以控制物体是否能跑到窗口外面去。

力可以使物体运动。会考虑重力、引力、浮力等。

物体分为三种:刚体(solid)、流体(liquid)、和关节(joint)

刚体是指在运动中和受力作用后,形状和大小不变,而且内部各点的相对位置不变的物体。

刚体可以分为线段(segment)球体(其实就是一个圆,circle)和多边形(polygon)。

每个刚体都会考虑碰撞。

流体分为牛顿流体和非牛顿流体。

关节可以约束两个刚体,可以伸缩。

效果则包含劈裂、燃烧等

开始

新建文件

在同一个目录下新建physics.pydemo.pyphysics.py使我们的物理引擎。demo.py则用来测试

代码

physics.py

PYTHON
import pygame
import threading
from pygame.locals import *
import sys
pygame.init()
# pygame.draw.circle(window, (0, 0, 255),[300,300], 170, 0)
# pygame.draw.polygon(window, (255,0,0), [[300,300],[100,400],[100,300]])
# pygame.draw.line(window, (0, 0, 0), [100, 300], [500, 300], 5)
class Window():
    def __init__(self,width,height,caption):
        self.width=width
        self.height=height
        self.caption=caption
        self.surface=pygame.display.set_mode((self.width,self.height))
        self.bodies=[]
    def run(self):
        pygame.display.set_caption(self.caption)
        flag=True
        while flag:
            pygame.display.update()
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    flag=False
class Object():
    def __init__(self):
        pass
class Solid(Object):
    def __init__(self):
        pass
class Liquid(Object):
    def __init__(self):
        pass
class Joint(Object):
    def __init__(self):
        pass

demo.py

PY
import physics
window1=physics.Window(1000,600,'123')
window1.run()