Update ball bounce collisions

This commit is contained in:
sam-astro 2022-01-14 07:54:20 -05:00 committed by GitHub
parent 735a20c224
commit 61a403bc1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,7 +4,7 @@ int SCREENH = 600
int scoreOne = 0 int scoreOne = 0
int scoreTwo = 0 int scoreTwo = 0
float ballSpeed = -3 float ballSpeed = 3
float paddleMoveSpeed = 7 float paddleMoveSpeed = 7
@ -95,49 +95,58 @@ func Update(deltaTime)
ballSpr.position += ballVelocity ballSpr.position += ballVelocity
HandleBallBounce()
// Finally draws all of the sprites // Finally draws all of the sprites
CPP.Graphics.Draw(ballSpr) CPP.Graphics.Draw(ballSpr)
CPP.Graphics.Draw(lPaddle) CPP.Graphics.Draw(lPaddle)
CPP.Graphics.Draw(rPaddle) CPP.Graphics.Draw(rPaddle)
HandleBallBounce()
} }
func HandleBallBounce() func HandleBallBounce()
{ {
float ballX = ballSpr.position.x float ballX = ballSpr.position.x
float ballY = ballSpr.position.y float ballY = ballSpr.position.y
// Checks if the ball is on the same X coordinate as the left paddle float scaleY = ballSpr.scale.y
if ballX <= lPaddle.position.x
// Checks if the ball is touching the ceiling
if ballY - scaleY <= 0
{ {
// Then check if ball is lower than the top edge ballVelocity = NVec2(ballVelocity.x, -ballVelocity.y)
float positionAdjustedOffset = lPaddle.position.y return 0
positionAdjustedOffset += lPaddle.scale.y }
if ballY <= positionAdjustedOffset // Checks if the ball is touching the floor
{ if ballY + scaleY >= SCREENH
// Finally check if ball is higher than the bottom edge {
positionAdjustedOffset = lPaddle.position.y ballVelocity = NVec2(ballVelocity.x, -ballVelocity.y)
positionAdjustedOffset -= lPaddle.scale.y return 0
print positionAdjustedOffset }
if ballY >= positionAdjustedOffset
{ // Checks if colliding with left paddle
float difference = lPaddle.position.y bool coll = Colliding(ballSpr, lPaddle)
difference -= ballY if coll == true
{
// float normalizedRelativeIntersectionY = (difference/(lPaddle.scale.y/2)) float difference = lPaddle.position.y
// float bounceAngle = normalizedRelativeIntersectionY * 1.3089 difference -= ballY
float normalizedRelativeIntersectionY = (difference/(lPaddle.scale.y/2))
// float ballVx = ballSpeed*Cos(bounceAngle) float bounceAngle = normalizedRelativeIntersectionY * 1.30899694
// float ballVy = ballSpeed*-Sin(bounceAngle) float ballVx = ballSpeed*Cos(bounceAngle)
float ballVy = ballSpeed*-Sin(bounceAngle)
// // Reflect horizontally ballVelocity = NVec2(ballVx, ballVy)
// if difference < 10 return 0
// { }
// float newX = Sin(DegToRad()) // Checks if colliding with right paddle
// ballVelocity bool coll = Colliding(ballSpr, rPaddle)
// } if coll == true
} {
} float difference = rPaddle.position.y
difference -= ballY
float normalizedRelativeIntersectionY = (difference/(rPaddle.scale.y/2))
float bounceAngle = normalizedRelativeIntersectionY * 1.30899694
float ballVx = ballSpeed*Cos(bounceAngle)
float ballVy = ballSpeed*-Sin(bounceAngle)
ballVelocity = NVec2(ballVx, ballVy)
return 0
} }
} }
@ -145,4 +154,4 @@ func Colliding(a, b)
{ {
bool b = CPP.Physics.AxisAlignedCollision(a, b) bool b = CPP.Physics.AxisAlignedCollision(a, b)
return b return b
} }