登录
首页 >  Golang >  Go问答

Raylib 2D & Go - 在碰撞结束时触发特定操作

来源:stackoverflow

时间:2024-02-24 11:45:22 100浏览 收藏

golang学习网今天将给大家带来《Raylib 2D & Go - 在碰撞结束时触发特定操作》,感兴趣的朋友请继续看下去吧!以下内容将会涉及到等等知识点,如果你是正在学习Golang或者已经是大佬级别了,都非常欢迎也希望大家都能给我建议评论哈~希望能帮助到大家!

问题内容

raylib 新手,正在尝试使用 go 创建一个超级马里奥克隆。我使用 rl.checkcollisionrecs 来检测玩家和管道对象之间的碰撞,该对象使用 aabb 碰撞方法来检测玩家是否首先撞击 x 轴或 y 轴。我面临的问题是,当玩家与 y 碰撞时,玩家的 y 位置设置为与管道 y 位置加上预期的高度相匹配。然而,当玩家离开对撞机时,他们不会落回地面。当矩形现在碰撞时间更长时,如何编写动作代码?

当他们的玩家离开对撞机时,我希望他们落回地面。

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
        }
    }
}

正确答案


问题已解决。 CheckCollisionRecs 正在检查是否存在碰撞,但不检查碰撞何时结束。

我的解决方案是,当检测到碰撞时,我存储静态对象的起始坐标和结束坐标(startPoint = obj.positionX,endPoint = obj.positionX + obj.width)。然后在我的更新函数中,我有一个 if 语句来检查玩家当前的 X 位置是否大于或小于静态对象的起点或终点。

终于介绍完啦!小伙伴们,这篇关于《Raylib 2D & Go - 在碰撞结束时触发特定操作》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>