Collisions kinda work.
parent
6a982acd5a
commit
9b6dd0d900
|
@ -2,7 +2,6 @@ package breakout
|
|||
|
||||
import (
|
||||
"gitea.wisellama.rocks/Wisellama/carpy-breakout/pkg/globjects"
|
||||
"github.com/go-gl/mathgl/mgl32"
|
||||
)
|
||||
|
||||
type Ball struct {
|
||||
|
@ -38,43 +37,47 @@ func (self *Ball) GetAABB() *globjects.AABB {
|
|||
}
|
||||
|
||||
func (self *Ball) HandlePaddleCollision(paddle *Paddle) {
|
||||
//intersects := self.Box.GetAABB().Intersects(paddle.GetAABB())
|
||||
aabb := self.GetAABB()
|
||||
p := paddle.GetAABB()
|
||||
|
||||
intersectsArray := self.Box.GetAABB().IntersectsArray(paddle.GetAABB())
|
||||
intersects := aabb.Intersects(p)
|
||||
|
||||
if intersectsArray[1] {
|
||||
// TODO special bounce based on where it hit
|
||||
//self.Box.Velocity = self.Box.Velocity.Mul(-1)
|
||||
self.Box.Velocity = mgl32.Vec3{0, 0, 0}
|
||||
if intersects {
|
||||
// Bounce off of things
|
||||
self.Box.Velocity = self.Box.Velocity.Mul(-1)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *Ball) HandleTargetCollisions(targets *Targets) {
|
||||
intersects := false
|
||||
aabb := self.GetAABB()
|
||||
for _, brick := range targets.Bricks {
|
||||
if !brick.Broken() {
|
||||
intersects = self.Box.GetAABB().Intersects(brick.Box.GetAABB())
|
||||
b := brick.GetAABB()
|
||||
|
||||
intersects = aabb.Intersects(b)
|
||||
|
||||
if intersects {
|
||||
brick.Break()
|
||||
|
||||
// Bounce off of things
|
||||
//self.Box.Velocity = self.Box.Velocity.Mul(-1)
|
||||
self.Box.Velocity = mgl32.Vec3{0, 0, 0}
|
||||
self.Box.Velocity = self.Box.Velocity.Mul(-1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (self *Ball) HandleBoxCollisions(objects []*globjects.Box) {
|
||||
func (self *Ball) HandleSideWallsCollisions(sidewalls *SideWalls) {
|
||||
intersects := false
|
||||
for _, object := range objects {
|
||||
intersects = self.Box.GetAABB().Intersects(object.GetAABB())
|
||||
|
||||
aabb := self.GetAABB()
|
||||
boxes := sidewalls.Boxes
|
||||
for _, box := range boxes {
|
||||
intersects = aabb.Intersects(box.GetAABB())
|
||||
|
||||
if intersects {
|
||||
// Bounce off of things
|
||||
//self.Box.Velocity = self.Box.Velocity.Mul(-1)
|
||||
self.Box.Velocity = mgl32.Vec3{0, 0, 0}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,9 +31,7 @@ func (b *Brick) Update() {
|
|||
}
|
||||
|
||||
func (b *Brick) GLDraw() {
|
||||
if !b.broken {
|
||||
b.Box.GLDraw()
|
||||
}
|
||||
b.Box.GLDraw()
|
||||
}
|
||||
|
||||
func (b *Brick) GLInit(glProgram uint32) {
|
||||
|
@ -44,12 +42,6 @@ func (b *Brick) ToggleWireframe() {
|
|||
b.Box.ToggleWireframe()
|
||||
}
|
||||
|
||||
func (b *Brick) SDLDraw() {
|
||||
if !b.broken {
|
||||
// TODO
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Brick) GetAABB() *globjects.AABB {
|
||||
return b.Box.GetAABB()
|
||||
}
|
||||
|
@ -60,4 +52,12 @@ func (b *Brick) Broken() bool {
|
|||
|
||||
func (b *Brick) Break() {
|
||||
b.broken = true
|
||||
|
||||
clear := globjects.NewMaterial()
|
||||
|
||||
clear.Color = mgl32.Vec4{0, 0, 0, 0}
|
||||
clear.Shininess = 0
|
||||
clear.TextureOn = false
|
||||
|
||||
b.Box.Material = clear
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ type GameWindow struct {
|
|||
Ball *Ball
|
||||
Targets *Targets
|
||||
SideWalls *SideWalls
|
||||
AABB *globjects.AABB
|
||||
AABB *globjects.AABB // This AABB represents the inside playable area
|
||||
|
||||
keystates map[sdl.Keycode]bool
|
||||
mouseMoved bool
|
||||
|
@ -207,16 +207,12 @@ func (w *GameWindow) Update() {
|
|||
w.Ball.Update()
|
||||
w.Ball.HandlePaddleCollision(w.Paddle)
|
||||
w.Ball.HandleTargetCollisions(w.Targets)
|
||||
w.Ball.HandleBoxCollisions(w.SideWalls.Boxes)
|
||||
w.Ball.HandleSideWallsCollisions(w.SideWalls)
|
||||
|
||||
w.Paddle.Update()
|
||||
w.ensurePaddleBoundary()
|
||||
}
|
||||
|
||||
func (w *GameWindow) GetAABB() *globjects.AABB {
|
||||
return w.AABB
|
||||
}
|
||||
|
||||
func (w *GameWindow) HandleEvents() {
|
||||
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
||||
switch e := event.(type) {
|
||||
|
@ -340,8 +336,9 @@ func (w *GameWindow) handleGameKeyboardMovement() {
|
|||
func (w *GameWindow) ensurePaddleBoundary() {
|
||||
b := w.Paddle.Box
|
||||
offset := b.Width / 2.0
|
||||
furthestLeft := w.GetAABB().BottomLeft.X() + offset
|
||||
furthestRight := w.GetAABB().TopRight.X() - offset
|
||||
inside := w.AABB
|
||||
furthestLeft := inside.BottomLeft.X() + offset
|
||||
furthestRight := inside.TopRight.X() - offset
|
||||
|
||||
if b.Translation.X() < furthestLeft {
|
||||
w.Paddle.Box.Translation[0] = furthestLeft
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package globjects
|
||||
|
||||
import (
|
||||
"gitea.wisellama.rocks/Wisellama/carpy-breakout/pkg/mathhelpers"
|
||||
"github.com/go-gl/mathgl/mgl32"
|
||||
)
|
||||
|
||||
|
@ -20,22 +21,40 @@ func NewAABB(topRight, bottomLeft mgl32.Vec3) *AABB {
|
|||
}
|
||||
|
||||
func NewAABBFromBox(box *Box) *AABB {
|
||||
pos := box.Translation
|
||||
|
||||
xOffset := box.Width / 2.0
|
||||
yOffset := box.Height / 2.0
|
||||
zOffset := box.Depth / 2.0
|
||||
|
||||
offset := mgl32.Vec3{
|
||||
offset := mgl32.Vec4{
|
||||
xOffset,
|
||||
yOffset,
|
||||
zOffset,
|
||||
1,
|
||||
}
|
||||
|
||||
topLeft := pos.Add(offset)
|
||||
bottomRight := pos.Sub(offset)
|
||||
// Apply the rotation to the offset vector
|
||||
rotatedOffset := box.GetRotationMatrix().Mul4x1(offset)
|
||||
|
||||
return NewAABB(topLeft, bottomRight)
|
||||
// Use the offset and the negation as the top right and bottom left
|
||||
topRight := rotatedOffset
|
||||
bottomLeft := rotatedOffset.Mul(-1)
|
||||
|
||||
// Then get the axis-align version of this box using min/max
|
||||
max := mgl32.Vec3{
|
||||
mathhelpers.Max32f(topRight.X(), bottomLeft.X()),
|
||||
mathhelpers.Max32f(topRight.Y(), bottomLeft.Y()),
|
||||
mathhelpers.Max32f(topRight.Z(), bottomLeft.Z()),
|
||||
}
|
||||
|
||||
min := mgl32.Vec3{
|
||||
mathhelpers.Min32f(topRight.X(), bottomLeft.X()),
|
||||
mathhelpers.Min32f(topRight.Y(), bottomLeft.Y()),
|
||||
mathhelpers.Min32f(topRight.Z(), bottomLeft.Z()),
|
||||
}
|
||||
|
||||
// Then move it into position based on the translation
|
||||
pos := box.Translation
|
||||
return NewAABB(max.Add(pos), min.Add(pos))
|
||||
}
|
||||
|
||||
func (self *AABB) Intersects(other *AABB) bool {
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
package globjects
|
||||
|
||||
import (
|
||||
"gitea.wisellama.rocks/Wisellama/carpy-breakout/pkg/glhelpers"
|
||||
gl "github.com/go-gl/gl/v3.1/gles2"
|
||||
"github.com/go-gl/mathgl/mgl32"
|
||||
|
||||
"gitea.wisellama.rocks/Wisellama/carpy-breakout/pkg/glhelpers"
|
||||
)
|
||||
|
||||
type Box struct {
|
||||
|
@ -23,7 +22,8 @@ type Box struct {
|
|||
GLVertexArrayID uint32
|
||||
GLVertexBufferID uint32
|
||||
Wireframe bool
|
||||
AABB *AABB
|
||||
|
||||
baseAABB *AABB
|
||||
}
|
||||
|
||||
func NewBox(
|
||||
|
@ -71,7 +71,8 @@ func NewBox(
|
|||
}
|
||||
|
||||
box.VertexArray = box.GetVertexArray()
|
||||
box.AABB = NewAABBFromBox(&box)
|
||||
|
||||
box.baseAABB = NewAABBFromBox(&box)
|
||||
|
||||
return &box
|
||||
}
|
||||
|
@ -96,20 +97,40 @@ func (b *Box) Update() {
|
|||
b.Translation = b.Translation.Add(b.Velocity)
|
||||
}
|
||||
|
||||
func (b *Box) GLDraw() {
|
||||
// GetModelMatrix applies the current rotation and translation to give you a model matrix.
|
||||
// Multiplying the original positions by this matrix will give you the new position.
|
||||
// It is also used in the OpenGL shaders directly.
|
||||
func (b *Box) GetModelMatrix() *mgl32.Mat4 {
|
||||
if b.Rotation.LenSqr() == 0 && b.Translation.LenSqr() == 0 {
|
||||
model := mgl32.Ident4()
|
||||
return &model
|
||||
}
|
||||
|
||||
// Apply rotation
|
||||
rotate := b.GetRotationMatrix()
|
||||
|
||||
// Apply translation
|
||||
translate := mgl32.Translate3D(b.Translation.X(), b.Translation.Y(), b.Translation.Z())
|
||||
|
||||
model := translate.Mul4(*rotate)
|
||||
|
||||
return &model
|
||||
}
|
||||
|
||||
func (b *Box) GetRotationMatrix() *mgl32.Mat4 {
|
||||
rotateX := mgl32.HomogRotate3D(b.Rotation.X(), mgl32.Vec3{1, 0, 0})
|
||||
rotateY := mgl32.HomogRotate3D(b.Rotation.Y(), mgl32.Vec3{0, 1, 0})
|
||||
rotateZ := mgl32.HomogRotate3D(b.Rotation.Z(), mgl32.Vec3{0, 0, 1})
|
||||
rotate := rotateX.Mul4(rotateY)
|
||||
rotate = rotate.Mul4(rotateZ)
|
||||
|
||||
// Apply translation
|
||||
translate := mgl32.Translate3D(b.Translation.X(), b.Translation.Y(), b.Translation.Z())
|
||||
return &rotate
|
||||
}
|
||||
|
||||
model := translate.Mul4(rotate)
|
||||
func (b *Box) GLDraw() {
|
||||
model := b.GetModelMatrix()
|
||||
|
||||
glhelpers.SetUniformMatrix4f(b.GLProgram, "model", &model)
|
||||
glhelpers.SetUniformMatrix4f(b.GLProgram, "model", model)
|
||||
|
||||
b.Material.GLDraw(b.GLProgram)
|
||||
|
||||
|
@ -167,18 +188,6 @@ func (b *Box) ToggleWireframe() {
|
|||
b.Wireframe = !b.Wireframe
|
||||
}
|
||||
|
||||
// func (b *Box) SDLDraw(renderer *sdl.Renderer) {
|
||||
// rect := &sdl.Rect{
|
||||
// X: int32(b.Translation.X()),
|
||||
// Y: int32(b.Translation.Y()),
|
||||
// W: int32(b.Width),
|
||||
// H: int32(b.Height),
|
||||
// }
|
||||
|
||||
// // TODO sdl.Do()
|
||||
// renderer.DrawRect(rect)
|
||||
// }
|
||||
|
||||
func (b *Box) SetXVelocity(v float32) {
|
||||
b.Velocity[0] = v
|
||||
}
|
||||
|
@ -196,5 +205,5 @@ func (b *Box) SetYRotationVelocity(v float32) {
|
|||
}
|
||||
|
||||
func (b *Box) GetAABB() *AABB {
|
||||
return b.AABB
|
||||
return NewAABBFromBox(b)
|
||||
}
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
package globjects
|
||||
|
||||
import (
|
||||
"github.com/go-gl/mathgl/mgl32"
|
||||
|
||||
"gitea.wisellama.rocks/Wisellama/carpy-breakout/pkg/glhelpers"
|
||||
"github.com/go-gl/mathgl/mgl32"
|
||||
)
|
||||
|
||||
// DirectionalLight implements effectively an infinitely far away point light.
|
||||
|
|
|
@ -3,11 +3,10 @@ package globjects
|
|||
import (
|
||||
"log"
|
||||
|
||||
gl "github.com/go-gl/gl/v3.1/gles2"
|
||||
"github.com/go-gl/mathgl/mgl32"
|
||||
|
||||
"gitea.wisellama.rocks/Wisellama/carpy-breakout/pkg/glhelpers"
|
||||
"gitea.wisellama.rocks/Wisellama/carpy-breakout/pkg/mathhelpers"
|
||||
gl "github.com/go-gl/gl/v3.1/gles2"
|
||||
"github.com/go-gl/mathgl/mgl32"
|
||||
)
|
||||
|
||||
type Face struct {
|
||||
|
|
|
@ -5,5 +5,4 @@ type GLObject interface {
|
|||
GLDraw()
|
||||
GLInit(glProgram uint32)
|
||||
ToggleWireframe()
|
||||
GetAABB() *AABB
|
||||
}
|
||||
|
|
|
@ -27,3 +27,19 @@ func AngleBetweenVectors(v1, v2 mgl32.Vec3) (float32, error) {
|
|||
|
||||
return float32(angle), nil
|
||||
}
|
||||
|
||||
func Max32f(a, b float32) float32 {
|
||||
if a >= b {
|
||||
return a
|
||||
} else {
|
||||
return b
|
||||
}
|
||||
}
|
||||
|
||||
func Min32f(a, b float32) float32 {
|
||||
if a <= b {
|
||||
return a
|
||||
} else {
|
||||
return b
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue