-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathparticle_system.go
More file actions
192 lines (175 loc) · 5.29 KB
/
Copy pathparticle_system.go
File metadata and controls
192 lines (175 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/rudransh61/Physix-go/dynamics/collision"
"github.com/rudransh61/Physix-go/dynamics/physics"
"github.com/rudransh61/Physix-go/pkg/rigidbody"
"github.com/rudransh61/Physix-go/pkg/vector"
"image/color"
// "math"
"math/rand"
"time"
// "fmt"
)
var (
balls []*rigidbody.RigidBody
down *rigidbody.RigidBody
right *rigidbody.RigidBody
up *rigidbody.RigidBody
left *rigidbody.RigidBody
dt = 0.001
e = 1.0
)
const (
Mass = 0.0002
Shape = "Circle"
Radius = 10
)
func update() error {
gravity := vector.Vector{X: 0, Y: 15}
for _, ball := range balls {
physix.ApplyForce(ball, gravity, dt)
// checkwall(ball)
if(collision.CircleRectangleCollided(ball, down)){
// collision.PreventCircleRectangleOverlap(ball, down)
collision.BounceOnCollision(ball, down, e)
}
if(collision.CircleRectangleCollided(ball, right)){
// collision.PreventCircleRectangleOverlap(ball, right)
collision.BounceOnCollision(ball, right, e)
}
if(collision.CircleRectangleCollided(ball, up)){
// collision.PreventCircleRectangleOverlap(ball, up)
collision.BounceOnCollision(ball, up, e)
}
if(collision.CircleRectangleCollided(ball, left)){
// collision.PreventCircleRectangleOverlap(ball, left)
collision.BounceOnCollision(ball, left, e)
}
}
for steps:=0;steps<10;steps++{
for i := 0; i < len(balls); i++ {
for j := i + 1; j < len(balls); j++ {
if collision.CircleCollided(balls[i], balls[j]) {
// resolveCollision(balls[i], balls[j])
collision.PreventCircleOverlap(balls[i], balls[j])
}
}
}
for _, ball := range balls {
if(collision.CircleRectangleCollided(ball, down)){
collision.PreventCircleRectangleOverlap(ball, down)
// collision.BounceOnCollision(ball, down, e)
}
if(collision.CircleRectangleCollided(ball, right)){
collision.PreventCircleRectangleOverlap(ball, right)
// collision.BounceOnCollision(ball, right, e)
}
if(collision.CircleRectangleCollided(ball, up)){
collision.PreventCircleRectangleOverlap(ball, up)
// collision.BounceOnCollision(ball, up, e)
}
if(collision.CircleRectangleCollided(ball, left)){
collision.PreventCircleRectangleOverlap(ball, left)
// collision.BounceOnCollision(ball, left, e)
}
}
}
for i := 0; i < len(balls); i++ {
for j := i + 1; j < len(balls); j++ {
if collision.CircleCollided(balls[i], balls[j]) {
// fmt.Println("Collision!")
collision.BounceOnCollision(balls[i], balls[j], e)
}
}
}
return nil
}
func draw(screen *ebiten.Image) {
for _, ball := range balls {
ebitenutil.DrawCircle(screen, ball.Position.X, ball.Position.Y, ball.Radius, color.RGBA{R: 0xff, G: 0, B: 0, A: 0xff})
}
//Boundary
ebitenutil.DrawRect(screen, right.Position.X, right.Position.Y, right.Width, right.Height, color.RGBA{R: 0, G: 0xff, B: 0, A: 0}) // right
ebitenutil.DrawRect(screen, left.Position.X, left.Position.Y, left.Width, left.Height, color.RGBA{R: 0, G: 0xff, B: 0, A: 0}) // left
ebitenutil.DrawRect(screen, up.Position.X, up.Position.Y, up.Width, up.Height, color.RGBA{R: 0, G: 0xff, B: 0, A: 0}) // up
ebitenutil.DrawRect(screen, down.Position.X, down.Position.Y, down.Width, down.Height, color.RGBA{R: 0, G: 0xff, B: 0, A: 0}) // down
}
func main() {
// Set up the window
ebiten.SetWindowSize(800, 800)
ebiten.SetWindowTitle("Bouncing Balls")
rand.Seed(time.Now().UnixNano())
// Initialize rigid bodies (balls)
n := 200 // Change this to the desired number of balls
initializeBalls(n)
// Run the game loop
if err := ebiten.RunGame(&Game{}); err != nil {
panic(err)
}
}
// initializeBalls initializes n balls with common properties
func initializeBalls(n int) {
balls = make([]*rigidbody.RigidBody, n)
for i := 0; i < n; i++ {
balls[i] = &rigidbody.RigidBody{
Position: vector.Vector{X: float64(rand.Intn(200) + 200), Y: float64(rand.Intn(200) + 200)},
Velocity: vector.Vector{X: float64(rand.Intn(20)), Y: float64(rand.Intn(20))},
Mass: Mass,
Shape: Shape,
Radius: Radius,
IsMovable: true,
}
}
down = &rigidbody.RigidBody{
Position: vector.Vector{X: 100, Y: 600},
Velocity: vector.Vector{X: 0, Y: 0},
Mass: 0,
Shape: "Rectangle",
Width: 510,
Height: 10,
IsMovable: false,
}
right = &rigidbody.RigidBody{
Position: vector.Vector{X: 600, Y: 300},
Velocity: vector.Vector{X: 0, Y: 0},
Mass: 0,
Shape: "Rectangle",
Width: 10,
Height: 600,
IsMovable: false,
}
left = &rigidbody.RigidBody{
Position: vector.Vector{X: 100, Y: 300},
Velocity: vector.Vector{X: 0, Y: 0},
Mass: 0,
Shape: "Rectangle",
Width: 10,
Height: 600,
IsMovable: false,
}
up = &rigidbody.RigidBody{
Position: vector.Vector{X: 100, Y: 100},
Velocity: vector.Vector{X: 0, Y: 0},
Mass: 0,
Shape: "Rectangle",
Width: 510,
Height: 10,
IsMovable: false,
}
}
// Game represents the game state.
type Game struct{}
// Update updates the game logic.
func (g *Game) Update() error {
return update()
}
// Draw draws the game.
func (g *Game) Draw(screen *ebiten.Image) {
draw(screen)
}
// Layout returns the game's screen size.
func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
return ebiten.ScreenSizeInFullscreen()
}