package main // veja: // The relationship between chaos, fractal and physics // https://www.youtube.com/watch?v=C5Jkgvw-Z6E import ( // "fmt" "fmt" "math" "math/rand" "strconv" // "math/rand" rl "github.com/gen2brain/raylib-go/raylib" ) type magnet struct { pos rl.Vector2 speed rl.Vector2 color rl.Color // force float32 // radius float32 } // type ball struct { // pos rl.Vector2 // originGrid rl.Vector2 // radius float32 // speed rl.Vector2 // steps int // } // parà‚metros const initalMagnetCount = 100 const windowWidth = 700 const windowHeight = 700 const magnetRadius = 5 const gravConst float32 = 10 // func insideArea(pos rl.Vector2) (bool, bool) { // return pos.X > 0 && pos.X < windowWidth, pos.Y > 0 && pos.Y < windowHeight // } func (m *magnet) update(magnets []*magnet) { magnetsPull := rl.Vector2{} for _, magnet := range magnets { if m == magnet { continue } direction := rl.Vector2Normalize(rl.Vector2Subtract(m.pos, magnet.pos)) distance := rl.Vector2Distance(magnet.pos, m.pos) if (distance < magnetRadius*2) { // direction = rl.Vector2Negate(direction) continue } distanceSquared := math.Pow(float64(distance), 2) force := gravConst / float32(distanceSquared) acceleration := rl.Vector2Scale(direction, -force) magnetsPull = rl.Vector2Add(magnetsPull, acceleration) } m.speed = rl.Vector2Add(m.speed, magnetsPull) newPos := rl.Vector2Add(m.speed, m.pos) // x, y := insideArea(newPos) // if !x { newPos.X = -newPos.X; m.speed.X = 0 } // if !y { newPos.Y = -newPos.Y; m.speed.Y = 0 } m.pos = newPos } func randomMagnet() *magnet { newMagnet := magnet{ pos: rl.Vector2{ X: float32(rand.Intn(windowWidth - 100) + 50), Y: float32(rand.Intn(windowHeight - 100) + 50), }, color: rl.NewColor( uint8(rand.Int()), uint8(rand.Int()), uint8(rand.Int()), 255, ), } return &newMagnet } func createMagnets(n int) []*magnet { var magnets []*magnet for i := 0; i < n; i++ { magnets = append(magnets, randomMagnet()) } return magnets } func main() { rl.InitWindow( windowWidth, windowHeight, "barnes hut", ) defer rl.CloseWindow() rl.SetTargetFPS(60) magnets := createMagnets(initalMagnetCount) for !rl.WindowShouldClose() { rl.BeginDrawing() rl.ClearBackground(rl.Black) rl.DrawFPS(0, 0) // func rl.DrawText(text string, posX int32, posY int32, fontSize int32, col color.RGBA) rl.DrawText(strconv.Itoa(len(magnets)), 0, 20, 20, rl.White) { if rl.IsKeyPressed(rl.KeyR) { magnets = createMagnets(len(magnets)) } if rl.IsKeyPressed(rl.KeyW) { magnets = append(magnets, createMagnets(100)...) } } for _, magnet := range magnets { magnet.update(magnets) rl.DrawCircleV(magnet.pos, magnetRadius+1, rl.White) rl.DrawCircleV(magnet.pos, magnetRadius, magnet.color) } rl.EndDrawing() } fmt.Println("hello world") }