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 ballVelocity = NVec2(ballVelocity.x, -ballVelocity.y)
positionAdjustedOffset = lPaddle.position.y return 0
positionAdjustedOffset -= lPaddle.scale.y }
print positionAdjustedOffset
if ballY >= positionAdjustedOffset // Checks if colliding with left paddle
bool coll = Colliding(ballSpr, lPaddle)
if coll == true
{ {
float difference = lPaddle.position.y float difference = lPaddle.position.y
difference -= ballY difference -= ballY
float normalizedRelativeIntersectionY = (difference/(lPaddle.scale.y/2))
// float normalizedRelativeIntersectionY = (difference/(lPaddle.scale.y/2)) float bounceAngle = normalizedRelativeIntersectionY * 1.30899694
// float bounceAngle = normalizedRelativeIntersectionY * 1.3089 float ballVx = ballSpeed*Cos(bounceAngle)
float ballVy = ballSpeed*-Sin(bounceAngle)
// float ballVx = ballSpeed*Cos(bounceAngle) ballVelocity = NVec2(ballVx, ballVy)
// float ballVy = ballSpeed*-Sin(bounceAngle) return 0
// // Reflect horizontally
// if difference < 10
// {
// float newX = Sin(DegToRad())
// ballVelocity
// }
}
} }
// Checks if colliding with right paddle
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
} }
} }