Added basic square mode
This commit is contained in:
parent
f8c2e8d33b
commit
f73b022e65
5 changed files with 208 additions and 0 deletions
8
go.mod
Normal file
8
go.mod
Normal file
|
@ -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
|
||||||
|
)
|
4
go.sum
Normal file
4
go.sum
Normal file
|
@ -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=
|
47
main.go
Normal file
47
main.go
Normal file
|
@ -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)
|
||||||
|
}
|
85
square.go
Normal file
85
square.go
Normal file
|
@ -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()
|
||||||
|
}
|
64
window.go
Normal file
64
window.go
Normal file
|
@ -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()
|
||||||
|
}
|
Loading…
Reference in a new issue