Skip to content

Commit 17ffa7f

Browse files
committed
modified: main.go new file: phi.go
1 parent 3350398 commit 17ffa7f

File tree

2 files changed

+71
-97
lines changed

2 files changed

+71
-97
lines changed

main.go

Lines changed: 25 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,34 @@
33
package main
44

55
import (
6-
"math"
7-
"math/rand"
8-
96
"github.com/gen2brain/raylib-go/raylib"
107
)
118

12-
const G float32 = 50
13-
14-
type planet struct {
15-
pos rl.Vector2
16-
radius float32
17-
velocity rl.Vector2
18-
acc rl.Vector2
19-
mass float32
20-
color rl.Color
21-
}
22-
23-
func newPlanet(pos rl.Vector2, radius float32, velocity rl.Vector2, acc rl.Vector2, mass float32, color rl.Color) planet {
24-
return planet{pos, radius, velocity, acc, mass, color}
25-
}
26-
func (p *planet) DrawPlanet() {
27-
rl.DrawCircle(int32(p.pos.X), int32(p.pos.Y), p.radius, p.color)
28-
}
29-
func (p *planet) calcAcc(op *planet) rl.Vector2 {
30-
r := rl.Vector2Subtract(op.pos, p.pos)
31-
if rl.Vector2Length(r) <= 300 {
32-
return rl.Vector2Zero()
33-
}
34-
g := (G * op.mass) / float32(math.Pow(float64(rl.Vector2Length(r)), 2))
35-
return rl.Vector2Scale(rl.Vector2Normalize(r), g)
36-
}
37-
func (p *planet) updateVelocity() {
38-
p.velocity = rl.Vector2Add(p.velocity, p.acc)
39-
}
40-
func (p *planet) updatePos() {
41-
p.pos = rl.Vector2Add(p.pos, p.velocity)
42-
}
43-
func (p *planet) updateAcc(planets []planet) {
44-
addAcc := rl.Vector2Zero()
45-
for i := range len(planets) {
46-
// Skip the planet itself
47-
if p == &(planets)[i] {
48-
continue
9+
func cameraControl(camera *rl.Camera2D){
10+
scroll := rl.GetMouseWheelMove()
11+
if scroll > 0 {
12+
camera.Zoom += 0.1 // Zoom in
13+
} else if scroll < 0 {
14+
camera.Zoom -= 0.1 // Zoom out
15+
}
16+
if rl.IsKeyPressed(rl.KeyK) {
17+
camera.Zoom += 0.05 // Zoom in
18+
}
19+
if rl.IsKeyPressed(rl.KeyJ) {
20+
camera.Zoom -= 0.05 // Zoom in
21+
}
22+
if rl.IsKeyDown(rl.KeyW) {
23+
camera.Offset.Y += 10
24+
}
25+
if rl.IsKeyDown(rl.KeyS) {
26+
camera.Offset.Y -= 10
27+
}
28+
if rl.IsKeyDown(rl.KeyA) {
29+
camera.Offset.X += 10
30+
}
31+
if rl.IsKeyDown(rl.KeyD) {
32+
camera.Offset.X -= 10
4933
}
50-
addAcc = rl.Vector2Add(addAcc, p.calcAcc(&(planets)[i]))
51-
}
52-
p.acc = addAcc
5334
}
5435
func main() {
5536
// Initialize window
@@ -59,37 +40,7 @@ func main() {
5940
// Set target FPS
6041
rl.SetTargetFPS(60)
6142
planets := []planet{}
62-
sun := newPlanet(rl.NewVector2(500, 350), 50, rl.Vector2Zero(), rl.Vector2Zero(), 1000, rl.Yellow)
63-
planets = append(planets, sun)
64-
65-
// Planet Data: [radius, mass, distance from Sun, orbital velocity, color]
66-
planetData := [][]float32{
67-
// name: radius, mass, distance from Sun, orbital velocity, color
68-
{10, 0.33, 58, 0.07, 255}, // Mercury
69-
{12, 4.87, 108, 0.05, 255}, // Venus
70-
{13, 5.97, 150, 0.03, 0}, // Earth
71-
{11, 0.642, 228, 0.02, 255}, // Mars
72-
{35, 1898, 778, 0.01, 255}, // Jupiter
73-
{30, 568, 1427, 0.009, 255}, // Saturn
74-
{25, 86.8, 2871, 0.006, 255}, // Uranus
75-
{25, 102, 4495, 0.004, 255}, // Neptune
76-
}
7743

78-
// Create planets for the solar system
79-
for _, p := range planetData {
80-
// Simplified orbital velocity using the formula (G * Sun's mass / distance)^(1/2)
81-
orbitalVelocity := float32(math.Sqrt(float64(G * sun.mass / p[2]))) // Orbital velocity based on distance
82-
83-
// Calculate planet's initial position based on distance from Sun
84-
planetPos := rl.NewVector2(sun.pos.X+p[2], sun.pos.Y)
85-
86-
// Color randomization
87-
color := rl.Color{R: uint8(rand.Intn(255)), G: uint8(rand.Intn(255)), B: uint8(rand.Intn(255)), A: 255}
88-
89-
// Create the planet and add to the planets array
90-
planet := newPlanet(planetPos, p[0], rl.NewVector2(0, orbitalVelocity), rl.Vector2Zero(), p[1], color)
91-
planets = append(planets, planet)
92-
}
9344
camera := rl.NewCamera2D(rl.NewVector2(500, 350), rl.Vector2Zero(), 0, 0.1)
9445

9546
for !rl.WindowShouldClose() {
@@ -104,30 +55,7 @@ func main() {
10455
rl.BeginDrawing()
10556
rl.ClearBackground(rl.RayWhite)
10657
rl.BeginMode2D(camera)
107-
scroll := rl.GetMouseWheelMove()
108-
if scroll > 0 {
109-
camera.Zoom += 0.1 // Zoom in
110-
} else if scroll < 0 {
111-
camera.Zoom -= 0.1 // Zoom out
112-
}
113-
if rl.IsKeyPressed(rl.KeyK) {
114-
camera.Zoom += 0.05 // Zoom in
115-
}
116-
if rl.IsKeyPressed(rl.KeyJ) {
117-
camera.Zoom -= 0.05 // Zoom in
118-
}
119-
if rl.IsKeyDown(rl.KeyW) {
120-
camera.Offset.Y += 10
121-
}
122-
if rl.IsKeyDown(rl.KeyS) {
123-
camera.Offset.Y -= 10
124-
}
125-
if rl.IsKeyDown(rl.KeyA) {
126-
camera.Offset.X += 10
127-
}
128-
if rl.IsKeyDown(rl.KeyD) {
129-
camera.Offset.X -= 10
130-
}
58+
cameraControl(&camera)
13159
// Draw planets
13260
for _, p := range planets {
13361
p.DrawPlanet()

phi.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
import(
3+
"math"
4+
"github.com/gen2brain/raylib-go/raylib"
5+
)
6+
const G float32 = 50
7+
type planet struct {
8+
pos rl.Vector2
9+
radius float32
10+
velocity rl.Vector2
11+
acc rl.Vector2
12+
mass float32
13+
color rl.Color
14+
}
15+
16+
func newPlanet(pos rl.Vector2, radius float32, velocity rl.Vector2, acc rl.Vector2, mass float32, color rl.Color) planet {
17+
return planet{pos, radius, velocity, acc, mass, color}
18+
}
19+
func (p *planet) DrawPlanet() {
20+
rl.DrawCircle(int32(p.pos.X), int32(p.pos.Y), p.radius, p.color)
21+
}
22+
func (p *planet) calcAcc(op *planet) rl.Vector2 {
23+
r := rl.Vector2Subtract(op.pos, p.pos)
24+
if rl.Vector2Length(r) <= 300 {
25+
return rl.Vector2Zero()
26+
}
27+
g := (G * op.mass) / float32(math.Pow(float64(rl.Vector2Length(r)), 2))
28+
return rl.Vector2Scale(rl.Vector2Normalize(r), g)
29+
}
30+
func (p *planet) updateVelocity() {
31+
p.velocity = rl.Vector2Add(p.velocity, p.acc)
32+
}
33+
func (p *planet) updatePos() {
34+
p.pos = rl.Vector2Add(p.pos, p.velocity)
35+
}
36+
func (p *planet) updateAcc(planets []planet) {
37+
addAcc := rl.Vector2Zero()
38+
for i := range len(planets) {
39+
// Skip the planet itself
40+
if p == &(planets)[i] {
41+
continue
42+
}
43+
addAcc = rl.Vector2Add(addAcc, p.calcAcc(&(planets)[i]))
44+
}
45+
p.acc = addAcc
46+
}

0 commit comments

Comments
 (0)