基于 Golang 的粒子物理引擎
rudransh61/Physix-go
master BranchesTags Go to file Code
Folders and files
Name| Name| Last commit message| Last commit date
---|---|---|---
Latest commit
History
50 Commits
dynamics| dynamics
example_gifs| example_gifs
examples| examples
pkg| pkg
project| project
CODE_STRUCTURE.md| CODE_STRUCTURE.md
CONTRIBUTING.md| CONTRIBUTING.md
LICENCE.md| LICENCE.md
Phi 6.png| Phi 6.png
README.md| README.md
example.gif| example.gif
example1.gif| example1.gif
example2.gif| example2.gif
go.mod| go.mod
go.sum| go.sum
main.go| main.go
View all files
Repository files navigation
Physix.go
一个简单的 GoLang 物理引擎 ☻
[](https://github.com/rudransh61/</rudransh61/Physix-go/blob/master/Phi 6.png>)
Platformer |
RigidBody |
Soft Body
---|---|---
Particle System |
Particle System 2 |
Bounce
Collision |
Collision |
Circular Motion
Projectile
Introduction
Physix.go 是一个简单、易用且快速的物理引擎,使用 GoLang 编写。它提供了高效执行物理计算的函数,包括基于粒子的物理模拟。
Features
- 向量计算
- 物理计算
- 弹性动力学
- 易于与 Ebiten.go 结合使用
Getting Started
Prerequisites
- 必须安装 GoLang。
- 必须安装 Ebiten。
Installation
git clone https://github.com/rudransh61/Physix.go
或者使用 go get
安装:
go get github.com/rudransh61/Physix.go
然后从 ./examples
文件夹运行示例文件。 例如:
go run ./examples/ex4.go # 这是一个简单的圆周运动
Documentation
Vectors
package main
import (
//...其他导入
"github.com/rudransh61/Physix-go/pkg/vector"
)
To make a vector
var MyVector = vector.Vector{X: 30, Y: 20}
// X 是向量的 x 分量,Y 是向量的 y 分量
Using Function
var NewVec = vector.NewVector(x, y)
Add Vector
var NewVector = Vec1.Add(Vec2)
Subtract Vector
var NewVector = Vec1.Sub(Vec2)
Inner Product of 2 Vectors
var DotProduct = Vec1.InnerProduct(Vec2)
Scale a Vector by a scalar
var ScaledVector = Vec1.Scale(num)
Magnitude of a Vector
var Magnitude = Vec1.Magnitude()
Normalize a Vector
var NormalizeVector = Vec1.Normalize()
Distance between Heads of 2 Vectors
var distance = vector.Distance(Vec1, Vec2)
Perpendicular Vector of a given Vector
var Orthogonal_Vector = vector.Orthogonal(Vec1)
RigidBody
要创建 RigidBody 的实例,你需要提供所有必需的字段。 首先,导入这些文件:
import (
"github.com/rudransh61/Physix-go/dynamics/physics"
"github.com/rudransh61/Physix-go/pkg/rigidbody"
)
Example:
ball = &rigidbody.RigidBody{
Position: vector.Vector{X: 400, Y: 100},
Velocity: vector.Vector{X: 0, Y: 2},
Mass: 1,
Force: vector.Vector{X: 0, Y: 5},
IsMovable: true,
Shape: "Circle", // Example shape
Radius: 10, // Required for Circle
}
要更新 RigidBody 的位置,请在循环中使用 ApplyForce :
for i := 0; i < 100; i++ {
physix.ApplyForce(ball, vector.Vector{X: 10, Y: 0}, dt) // Apply force
// .. other code
}
要访问或更改 Force , Velocity , Position :
ball.Velocity // 获取球的速度,作为 vector.Vector
ball.Position.X += 5 // 将球在 X 方向上的位置增加 5
Collision Detection
- 矩形-矩形碰撞
- 圆形-圆形碰撞
Rectangle Collision
rect1 = &rigidbody.RigidBody{
Position: vector.Vector{X: 100, Y: 200},
Velocity: vector.Vector{X: 50, Y: -50},
Mass: 1.0,
Shape: "Rectangle",
Width: 100,
Height: 90,
IsMovable: true,
}
rect2 = &rigidbody.RigidBody{
Position: vector.Vector{X: 400, Y: 300},
Velocity: vector.Vector{X: 60, Y: 50},
Mass: 2.0,
Shape: "Rectangle",
Width: 70,
Height: 70,
IsMovable: true,
}
现在你想检测它们之间的碰撞:
if collision.RectangleCollided(rect1, rect2) {
fmt.Println("Collided!")
}
并且如果你想在此碰撞中根据 Momentum Conservation 和 Energy Conservation 添加一个反弹效果:
if collision.RectangleCollided(rect1, rect2) {
float64 e = 0.9999999999; // e 是碰撞中的恢复系数
collision.BounceOnCollision(rect1, rect2, e) // 注意:e<1 有点小故障,会变得不稳定,请自行承担风险使用 :)
}
Circle Collision
if collision.CircleCollided(rect1, rect2) {
fmt.Println("Collided!")
}
并使用相同的 BounceOnCollision
函数进行反弹。
Springs
弹簧可用于模拟刚体之间的弹性连接。 要创建弹簧,你需要定义两个刚体并指定弹簧的刚度和阻尼属性。 Example:
spring := spring.NewSpring(ballA, ballB, stiffness, damping)
要在你的模拟循环中应用弹簧力:
spring.ApplyForce()
这将基于胡克定律和阻尼将力施加到连接的刚体。
Examples
Some Dynamics
Physics
要更新我们的 entity ,我们有两个函数: ApplyForce 和 ApplyForcePolygon ,顾名思义,一个用于 RigidBody ,一个用于多边形。
此函数将向前移动一帧或“dt”时间(这是两帧之间的时间)。
注意: 为了编写良好的代码,请在顶部全局定义 dt
(通常为 0.1)。
ball = &rigidbody.RigidBody{
Position: vector.Vector{X: 400, Y: 100},
Velocity: vector.Vector{X: 0, Y: 2},
Mass: 1,
Force: vector.Vector{X: 0, Y: 5},
IsMovable: true,
}
physix.ApplyForce(ball, vector.Vector{X: 10, Y: 0}, dt) // To apply force on the rigid body
要在代码中获取这两种实用程序,请导入此文件:
import (
...
"github.com/rudransh61/Physix-go/dynamics/physics"
...
)
Contributing
欢迎新的贡献者! 如果你对其工作方式有任何疑问,你可以通过提出 issue 来询问我们。
License
Acknowledgments
灵感来自 Coding Train - Daniel Shiffman
About
A simple Physics engine in GoLang
Topics
game go golang gamedev games physics collision physics-engine movement hacktoberfest physics-2d 2d 2d-graphics freedevs
Resources
License
Stars
Watchers
Forks
Releases 1
Release v1.0 Latest Mar 17, 2024
Packages 0
No packages published
Languages
Footer
Footer navigation
You can’t perform that action at this time.