1

new to Raylib and trying to create a Super Mario clone using Go. I am using rl.CheckCollisionRecs to detect collision between the player and a pipe object, which is using the AABB collision method to detect if a player is hitting the X or Y axis first. The problem I am facing is that when the player collides with the Y, the player's Y position is set to match the pipes y position plus its height as expected. However, the player does not fall back to the ground when they leave the collider. How can I code an action when the rectangles are now longer colliding?

When they player leaves the collider, I want them to fall back to the ground.

func drawColliders() {
for _, current_Pipe := range pipes {
    rl.DrawRectangle(current_Pipe.posX, current_Pipe.posY, current_Pipe.width, current_Pipe.height, current_Pipe.Color)

    if rl.CheckCollisionRecs(playerDest, rl.NewRectangle(float32(current_Pipe.posX), float32(current_Pipe.posY), float32(current_Pipe.width), float32(current_Pipe.height))) {

        var xDistance float32
        var yDistance float32

        var dx float32
        var dy float32

        if playerDest.X < float32(current_Pipe.posX) {
            dx = float32(current_Pipe.posX) - playerDest.Width
            isColliding = true

        } else if playerDest.X > float32(current_Pipe.posX-current_Pipe.width) {
            dx = float32(current_Pipe.posX) + float32(current_Pipe.width)
            isColliding = true

        }

        if playerDest.Y < float32(current_Pipe.posY) {
            dy = float32(current_Pipe.posY) - (playerDest.Y + playerDest.Height)
            isColliding = true
        } else if playerDest.Y > float32(current_Pipe.posY) {
            dy = float32(current_Pipe.posY) + (float32(current_Pipe.posY) + float32(current_Pipe.height))
            isColliding = true

        }

        xDistance = dx
        yDistance = dy

        // fmt.Println(xDistance, yDistance)

        var xAxisTimeToCollide float32 = float32(math.Abs(float64(xDistance) / float64(velocityX)))
        var yAxisTimeToCollide float32 = float32(math.Abs(float64(yDistance) / float64(velocityY)))

        // fmt.Println("X Time: ", xAxisTimeToCollide, " | Y Time: ", yAxisTimeToCollide)

        if xAxisTimeToCollide < yAxisTimeToCollide {

            // fmt.Println("Collision on the X axis")
            if playerDest.X < float32(current_Pipe.posX) {
                playerDest.X = float32(current_Pipe.posX) - playerDest.Width
            } else if playerDest.X > float32(current_Pipe.posX-current_Pipe.width) {
                playerDest.X = float32(current_Pipe.posX) + float32(current_Pipe.width)
            }
        } else {
            // fmt.Println("Collsion on the Y axis")
            playerGrounded = true
            playerJumping = false
            velocityY = 0
            playerDest.Y = float32(current_Pipe.posY) - playerDest.Height
        }
    }
}
C-delb
  • 31
  • 3

1 Answers1

0

Problem solved. CheckCollisionRecs is checking if there is a collision, but does not check when a collision ends.

My solution was that when a collision was detected, I stored the static objects starting coordinate and ending coordinate (startPoint = obj.positionX, endPoint = obj.positionX + obj.width). Then in my update function, I have an if statement that checks if the players current X position is more than or less than the static objects start or end point.

C-delb
  • 31
  • 3