From f73b022e654cfa5fa7ee454a418f24661f046fa6 Mon Sep 17 00:00:00 2001 From: objnull Date: Mon, 27 May 2024 14:31:32 -0400 Subject: [PATCH] Added basic square mode --- go.mod | 8 ++++++ go.sum | 4 +++ main.go | 47 ++++++++++++++++++++++++++++++ square.go | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ window.go | 64 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 208 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 square.go create mode 100644 window.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b6b8b80 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module yt-phys + +go 1.22.3 + +require ( + github.com/go-gl/gl v0.0.0-20231021071112-07e5d0ea2e71 // indirect + github.com/go-gl/glfw/v3.3/glfw v0.0.0-20240506104042-037f3cc74f2a // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d746710 --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +github.com/go-gl/gl v0.0.0-20231021071112-07e5d0ea2e71 h1:5BVwOaUSBTlVZowGO6VZGw2H/zl9nrd3eCZfYV+NfQA= +github.com/go-gl/gl v0.0.0-20231021071112-07e5d0ea2e71/go.mod h1:9YTyiznxEY1fVinfM7RvRcjRHbw2xLBJ3AAGIT0I4Nw= +github.com/go-gl/glfw/v3.3/glfw v0.0.0-20240506104042-037f3cc74f2a h1:vxnBhFDDT+xzxf1jTJKMKZw3H0swfWk9RpWbBbDK5+0= +github.com/go-gl/glfw/v3.3/glfw v0.0.0-20240506104042-037f3cc74f2a/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= diff --git a/main.go b/main.go new file mode 100644 index 0000000..1f19b98 --- /dev/null +++ b/main.go @@ -0,0 +1,47 @@ +// Package +package main + +import ( + "math/rand" +) + +// Libraries + +// Varaibles +var squares = []*Square{} + +// Functions +func onCollide(s *Square) { + var nSquare Square + nSquare.Init() + + nSquare.x = s.x - (s.vx * 0.5) + nSquare.y = s.y - (s.vy * 0.5) + nSquare.vx = s.vx * (rand.Float32() * 1.2) + nSquare.vy = s.vy * (rand.Float32() * 1.2) + + squares = append(squares, &nSquare) +} +func onUpdate(w *Window) { + for _, square := range squares { + go square.Update(onCollide) + square.Draw() + } +} + +// Entry Point +func main() { + // Creating the Window + var window Window + + // Starting the window + window.Init("YT Physics", 1024, 1024) + + // Creating objects + var nSquare Square + nSquare.Init() + squares = append(squares, &nSquare) + + // Running the app + window.Run(onUpdate) +} diff --git a/square.go b/square.go new file mode 100644 index 0000000..4ca5e07 --- /dev/null +++ b/square.go @@ -0,0 +1,85 @@ +// Package +package main + +// Libraries +import ( + "math/rand" + + "github.com/go-gl/gl/v2.1/gl" +) + +// Structures +type Square struct { + x, y float32 + vx, vy float32 + r, g, b float32 + cTrigger bool +} + +// Interfaces +func (c *Square) Init() { + c.x = 0 + c.y = 0 + c.vx = 0.005 + c.vy = 0.005 + + c.r = rand.Float32() + c.g = rand.Float32() + c.b = rand.Float32() +} +func (c *Square) Update(onCollide func(s *Square)) { + // Movement + c.x += c.vx + c.y += c.vy + + // STAY INSIDE BOUNDS! + if c.x > 1 { + c.x = 0.9 + c.vx *= -1 + } else if c.x < -1 { + c.x = -0.9 + c.vx *= -1 + } else if c.y > 1 { + c.y = 0.9 + c.vy *= -1 + } else if c.y < -1 { + c.y = -0.9 + c.vy *= -1 + } + + // Can we trigger a collision? + if c.cTrigger { + // Collision Detection + if c.x+0.05 > 1 || c.x-0.05 < -1 { + c.vx *= -1 + + go onCollide(c) + } + if c.y+0.1 > 1 || c.y-0.1 < -1 { + c.vy *= -1 + + go onCollide(c) + } + } else { + if (c.x < 0.9 && c.x > -0.9) && (c.y < 0.9 && c.y > -0.9) { + c.cTrigger = true + } + } +} +func (c *Square) Draw() { + gl.LoadIdentity() + + gl.Translatef(c.x, c.y, 0) + gl.Color3f(c.r, c.g, c.b) + + gl.Translatef(-0.05, -0.05, 0) + + gl.Begin(gl.QUADS) + + gl.Vertex2f(0, 0) + gl.Vertex2f(0.1, 0) + gl.Vertex2f(0.1, 0.1) + gl.Vertex2f(0, 0.1) + + gl.End() +} diff --git a/window.go b/window.go new file mode 100644 index 0000000..1817033 --- /dev/null +++ b/window.go @@ -0,0 +1,64 @@ +// Package +package main + +// Libraries +import ( + "fmt" + "log" + "runtime" + + "github.com/go-gl/gl/v2.1/gl" + "github.com/go-gl/glfw/v3.3/glfw" +) + +// Structures +type Window struct { + title string + width, height int + window *glfw.Window +} + +// Interfaces +func (w *Window) Init(title string, _w int, _h int) { + // Setting variables + w.title = title + w.width = _w + w.height = _h + + // DBEUG + fmt.Println(": ENGINE STARTING") + + // Locking thread + runtime.LockOSThread() + + // Starting GLFW + err := glfw.Init() + if err != nil { + panic(err) + } + + // Creating a window + w.window, err = glfw.CreateWindow(w.width, w.height, w.title, nil, nil) + if err != nil { + panic(err) + } + w.window.MakeContextCurrent() + + // Staring OpenGL + if err := gl.Init(); err != nil { + log.Fatalln(err) + } + + gl.ClearColor(0, 0, 0, 1) +} +func (w *Window) Run(execute func(w *Window)) { + for !w.window.ShouldClose() { + w.window.SwapBuffers() + glfw.PollEvents() + gl.Clear(gl.COLOR_BUFFER_BIT) + execute(w) + } +} +func (w *Window) Stop() { + glfw.Terminate() +}