collision works

main
Sean Hickey 2022-01-01 22:03:07 -08:00
parent 9b6dd0d900
commit db5a0aa6e8
4 changed files with 76 additions and 22 deletions

View File

@ -43,8 +43,18 @@ func (self *Ball) HandlePaddleCollision(paddle *Paddle) {
intersects := aabb.Intersects(p)
if intersects {
// Bounce off of things
self.Box.Velocity = self.Box.Velocity.Mul(-1)
newVelocity := self.Box.Velocity
if aabb.HorizontalCollision(p) {
newVelocity[0] = -1 * newVelocity[0]
}
if aabb.VerticalCollision(p) {
// Change differently based on which part of the paddle we hit
// TODO
newVelocity[1] = -1 * newVelocity[1]
}
self.Box.Velocity = newVelocity
}
}
@ -58,10 +68,10 @@ func (self *Ball) HandleTargetCollisions(targets *Targets) {
intersects = aabb.Intersects(b)
if intersects {
// TODO points
brick.Break()
// Bounce off of things
self.Box.Velocity = self.Box.Velocity.Mul(-1)
self.UpdateVelocityFromCollision(aabb, b)
}
}
}
@ -73,11 +83,25 @@ func (self *Ball) HandleSideWallsCollisions(sidewalls *SideWalls) {
aabb := self.GetAABB()
boxes := sidewalls.Boxes
for _, box := range boxes {
intersects = aabb.Intersects(box.GetAABB())
b := box.GetAABB()
intersects = aabb.Intersects(b)
if intersects {
// Bounce off of things
//self.Box.Velocity = self.Box.Velocity.Mul(-1)
self.UpdateVelocityFromCollision(aabb, b)
}
}
}
func (self *Ball) UpdateVelocityFromCollision(us, them *globjects.AABB) {
newVelocity := self.Box.Velocity
if us.HorizontalCollision(them) {
newVelocity[0] = -1 * newVelocity[0]
}
if us.VerticalCollision(them) {
newVelocity[1] = -1 * newVelocity[1]
}
self.Box.Velocity = newVelocity
}

View File

@ -32,7 +32,6 @@ type GameWindow struct {
mouseMoved bool
running bool
fullscreen bool
wireframe bool
freelook bool
}
@ -40,7 +39,6 @@ func NewGameWindow(title string) (*GameWindow, error) {
gameWindow := GameWindow{
running: true,
fullscreen: false,
wireframe: false,
freelook: false,
keystates: make(map[sdl.Keycode]bool),
}
@ -164,11 +162,6 @@ func (w *GameWindow) ToggleWireframe() {
for _, o := range w.GLObjects {
o.ToggleWireframe()
}
w.Paddle.ToggleWireframe()
w.Ball.ToggleWireframe()
w.wireframe = !w.wireframe
}
func (w *GameWindow) GLDraw() {

View File

@ -41,15 +41,15 @@ func NewAABBFromBox(box *Box) *AABB {
// 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()),
mathhelpers.Maxf32(topRight.X(), bottomLeft.X()),
mathhelpers.Maxf32(topRight.Y(), bottomLeft.Y()),
mathhelpers.Maxf32(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()),
mathhelpers.Minf32(topRight.X(), bottomLeft.X()),
mathhelpers.Minf32(topRight.Y(), bottomLeft.Y()),
mathhelpers.Minf32(topRight.Z(), bottomLeft.Z()),
}
// Then move it into position based on the translation
@ -78,3 +78,32 @@ func (self *AABB) IntersectsArray(other *AABB) []bool {
return boolArray
}
// HorizontalCollisions returns true if the our box hit the 'other' box on the X axis.
// This is expected to be called after checking for a general intersection with Intersects()
func (self *AABB) HorizontalCollision(other *AABB) bool {
right := self.TopRight.X() - other.BottomLeft.X()
left := self.BottomLeft.X() - other.TopRight.X()
right = mathhelpers.Absf32(right)
left = mathhelpers.Absf32(left)
min := mathhelpers.Minf32(right, left)
return mathhelpers.Absf32(min) < 0.1
}
func (self *AABB) VerticalCollision(other *AABB) bool {
top := self.TopRight.Y() - other.BottomLeft.Y()
bottom := self.BottomLeft.Y() - other.TopRight.Y()
top = mathhelpers.Absf32(top)
bottom = mathhelpers.Absf32(bottom)
min := mathhelpers.Minf32(top, bottom)
return mathhelpers.Absf32(min) < 0.1
}

View File

@ -28,7 +28,7 @@ func AngleBetweenVectors(v1, v2 mgl32.Vec3) (float32, error) {
return float32(angle), nil
}
func Max32f(a, b float32) float32 {
func Maxf32(a, b float32) float32 {
if a >= b {
return a
} else {
@ -36,10 +36,18 @@ func Max32f(a, b float32) float32 {
}
}
func Min32f(a, b float32) float32 {
func Minf32(a, b float32) float32 {
if a <= b {
return a
} else {
return b
}
}
func Absf32(a float32) float32 {
if a < 0 {
a = a * -1
}
return a
}