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

推荐订阅源

博客园 - 【当耐特】
K
Kaspersky official blog
V
Vulnerabilities – Threatpost
Hacker News - Newest:
Hacker News - Newest: "LLM"
Security Archives - TechRepublic
Security Archives - TechRepublic
S
Secure Thoughts
I
Intezer
TaoSecurity Blog
TaoSecurity Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Spread Privacy
Spread Privacy
A
About on SuperTechFans
NISL@THU
NISL@THU
The GitHub Blog
The GitHub Blog
Hugging Face - Blog
Hugging Face - Blog
S
Security @ Cisco Blogs
S
SegmentFault 最新的问题
G
Google Developers Blog
B
Blog
N
News and Events Feed by Topic
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Google DeepMind News
Google DeepMind News
V2EX - 技术
V2EX - 技术
V
Visual Studio Blog
MyScale Blog
MyScale Blog
Webroot Blog
Webroot Blog
Vercel News
Vercel News
IT之家
IT之家
Microsoft Security Blog
Microsoft Security Blog
Last Week in AI
Last Week in AI
Y
Y Combinator Blog
S
Security Affairs
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Stack Overflow Blog
Stack Overflow Blog
P
Proofpoint News Feed
L
Lohrmann on Cybersecurity
博客园 - 叶小钗
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Know Your Adversary
Know Your Adversary
T
Tailwind CSS Blog
F
Fortinet All Blogs
D
DataBreaches.Net
博客园 - Franky
博客园_首页
H
Heimdal Security Blog
宝玉的分享
宝玉的分享
阮一峰的网络日志
阮一峰的网络日志
Attack and Defense Labs
Attack and Defense Labs
Project Zero
Project Zero
雷峰网
雷峰网

埃氮幂の命名空间

埃氮幂の命名空间 埃氮幂の命名空间 埃氮幂の命名空间 埃氮幂の命名空间 埃氮幂の命名空间 埃氮幂の命名空间 埃氮幂の命名空间 埃氮幂の命名空间 埃氮幂の命名空间 埃氮幂の命名空间 埃氮幂の命名空间 埃氮幂の命名空间 埃氮幂の命名空间 埃氮幂の命名空间 埃氮幂の命名空间 埃氮幂の命名空间 埃氮幂の命名空间
埃氮幂の命名空间
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()