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

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
Cisco Talos Blog
Cisco Talos Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
V2EX
博客园 - 三生石上(FineUI控件)
Martin Fowler
Martin Fowler
WordPress大学
WordPress大学
D
Docker
S
SegmentFault 最新的问题
博客园 - 聂微东
美团技术团队
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
M
MIT News - Artificial intelligence
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The GitHub Blog
The GitHub Blog
GbyAI
GbyAI
L
LangChain Blog
Vercel News
Vercel News
博客园 - 叶小钗
MongoDB | Blog
MongoDB | Blog
Stack Overflow Blog
Stack Overflow Blog
H
Help Net Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
Engineering at Meta
Engineering at Meta
T
Threat Research - Cisco Blogs
T
Threatpost
Scott Helme
Scott Helme
T
Tailwind CSS Blog
Latest news
Latest news
Stack Overflow Blog
Stack Overflow Blog
Blog — PlanetScale
Blog — PlanetScale
The Register - Security
The Register - Security
罗磊的独立博客
P
Proofpoint News Feed
腾讯CDC
S
Schneier on Security
雷峰网
雷峰网
A
About on SuperTechFans
T
Tenable Blog
F
Full Disclosure
Cyberwarzone
Cyberwarzone
博客园_首页
有赞技术团队
有赞技术团队
K
Kaspersky official blog

文章列表

埃氮幂の命名空间 博客一周年记 P13020 [GESP202506 八级] 遍历计数 题解 Moonlark(Nonebot2+Python)命令式聊天机器人插件开发记录 使用Github Actions定制Github个人主页 P3435 [POI 2006] OKR-Periods of Words 题解 Qt Troubleshoot(三) Qt Troubleshoot(二) Qt Troubleshoot (一) 埃氮幂の命名空间 自己动手写三维引擎(一) 自己动手写二维物理引擎(一) 埃氮幂の命名空间 Markdown与外挂标签语法测试 Hexo博客0氪建站记录(下) Hexo博客0氪建站记录(中) Hexo博客0氪建站记录(上)
埃氮幂の命名空间
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()

自己动手写二维物理引擎(零)

作者

Admibrill

发布于

2024-09-28

更新于

2024-09-28

许可协议