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位置是否大于或小于静态对象的起点或终点。