distancia ao quadrado

This commit is contained in:
silva guimaraes 2023-08-15 23:59:20 -03:00
parent 035770a3b6
commit 8411609b6a

50
main.go
View file

@ -8,7 +8,7 @@ import (
// "fmt" // "fmt"
"fmt" "fmt"
"math" "math"
"math/rand" // "math/rand"
rl "github.com/gen2brain/raylib-go/raylib" rl "github.com/gen2brain/raylib-go/raylib"
) )
@ -32,20 +32,19 @@ type ball struct {
// parâmetros // parâmetros
const windowWidth = 700 const windowWidth = 700
const windowHeight = 700 const windowHeight = 700
const magnetRadius = 8 const gridDivision = 8
const magnetForce = 10
const magnetsDistance float32 = 80
const gridDivision = 10
const verticalGrids = windowHeight/gridDivision const verticalGrids = windowHeight/gridDivision
const horizontalGrids = windowWidth/gridDivision const horizontalGrids = windowWidth/gridDivision
const ballMass float32 = 1 // sensivel const magnetsDistance float32 = 80
const gravConst float32 = 1 // sensivel const magnetRadius = 10
const gravConst float32 = 200
const magnetsAngle = 0 const magnetsAngle = 0
const luminance = false const luminance = false
const hideBalls = false const hideBalls = false
const hideMagnets = false const hideMagnets = false
var startingBallSpeed = rl.Vector2{X: -0.5, Y: 0.5} // joga a bola em alguma diração var startingBallSpeed = rl.Vector2{X: 0, Y: 0} // joga a bola em alguma diração
// antes que ela comece a ser atraida pelos ímãs // antes que ela comece a ser atraida pelos ímãs
var cameraOffset = rl.Vector2{X: 0, Y: 0}
func (ball *ball) update(magnets []magnet) (bool, rl.Color) { func (ball *ball) update(magnets []magnet) (bool, rl.Color) {
@ -56,7 +55,8 @@ func (ball *ball) update(magnets []magnet) (bool, rl.Color) {
rl.Vector2Subtract(ball.pos, magnet.pos), rl.Vector2Subtract(ball.pos, magnet.pos),
) )
distance := rl.Vector2Distance(magnet.pos, ball.pos) distance := rl.Vector2Distance(magnet.pos, ball.pos)
force := gravConst * (ballMass * magnet.force) / distance distanceSquared := float32(math.Pow(float64(distance), 2))
force := gravConst / distanceSquared
acceleration := rl.Vector2Scale(direction, -force) acceleration := rl.Vector2Scale(direction, -force)
@ -77,10 +77,15 @@ func (ball *ball) update(magnets []magnet) (bool, rl.Color) {
} }
// var colors = []rl.Color{
// rl.Red, rl.Green, rl.Blue, rl.Yellow, rl.NewColor(0, 255, 255, 255),
// rl.Magenta, rl.White,
// }
func main() { func main() {
rl.InitWindow( rl.InitWindow(
windowWidth, windowHeight, windowWidth, windowHeight,
"raylib [core] example - basic window", "magnets",
) )
defer rl.CloseWindow() defer rl.CloseWindow()
// rl.SetTargetFPS(60) // rl.SetTargetFPS(60)
@ -89,23 +94,28 @@ func main() {
{ {
color: rl.Red, color: rl.Red,
radius: magnetRadius, radius: magnetRadius,
force: magnetForce, // force: magnetForce,
}, },
{ {
color: rl.Green, color: rl.Green,
radius: magnetRadius, radius: magnetRadius,
force: magnetForce, // force: magnetForce,
}, },
{ {
color: rl.Blue, color: rl.Blue,
radius: magnetRadius, radius: magnetRadius,
force: magnetForce, // force: magnetForce,
},
{
color: rl.Yellow,
radius: magnetRadius,
// force: magnetForce,
}, },
} }
// pocisionar imas no meio da tela // posiciona imas em volta de um circulo
xCenter := float32(windowWidth / 2) xCenter := float32(windowWidth / 2) + cameraOffset.X
yCenter := float32(windowHeight / 2) yCenter := float32(windowHeight / 2) + cameraOffset.Y
for i := range magnets { for i := range magnets {
angle := 2.0 * math.Pi * float64(i) / float64(len(magnets)) + magnetsAngle angle := 2.0 * math.Pi * float64(i) / float64(len(magnets)) + magnetsAngle
@ -145,10 +155,10 @@ func main() {
}) })
} }
} }
rand.Shuffle(len(fallingBallOrder), func(i, j int) { // rand.Shuffle(len(fallingBallOrder), func(i, j int) {
fallingBallOrder[i], fallingBallOrder[j] = // fallingBallOrder[i], fallingBallOrder[j] =
fallingBallOrder[j], fallingBallOrder[i] // fallingBallOrder[j], fallingBallOrder[i]
}) // })
for _, randomGrid := range fallingBallOrder { for _, randomGrid := range fallingBallOrder {
x := randomGrid.X * gridSize.X + gridSize.X/2 x := randomGrid.X * gridSize.X + gridSize.X/2