你好,我可以帮助你解决这个问题。
问题描述了一个平台游戏,玩家控制一个旋转的轮子,通过跳跃和旋转来移动。然而,碰撞检测出了问题,导致玩家在跳跃时卡在角落,或者在移动时被卡在墙壁上。
代码如下:
/// Variables
x = room_width/4;
y = room_height/2;
fall = false;
xspeed = 0;
yspeed = 0;
rotspeed = 0;
/// Step
var _right = keyboard_check_direct(ord("D"));
var _left = keyboard_check_direct(ord("A"));
var _jump = keyboard_check_direct(vk_space);
var _moving = keyboard_check_direct(ord("A")) or keyboard_check_direct(ord("D"));
// 左右移动
if _left {
if rotspeed < 15 {
if rotspeed *-1 == 1 {
rotspeed += 0.2;
} else {
rotspeed += 0.1;
}
}
}
if _right {
if rotspeed > -15 {
if rotspeed *-1 == -1 {
rotspeed -= 0.2;
} else {
rotspeed -= 0.1;
}
}
}
// 旋转速度
image_angle += rotspeed;
if _moving == false {
if rotspeed != 0 {
rotspeed = lerp(rotspeed, 0, 0.01);
}
}
// 跳跃检测
if place_meeting(x, y+2, oBlock) == true {
yspeed = 0;
if _jump {
if xspeed < 5 and xspeed > -5 {
yspeed = -3;
} else {
yspeed = -4;
}
}
} else if place_meeting(x, y+2, oBlock) == false {
yspeed += 0.1;
}
// 水平碰撞检测
xspeed = -rotspeed;
if xspeed < 0 {
if place_meeting(x-3, y, oBlock) == true {
xspeed = 0;
}
} else if xspeed > 0 {
if place_meeting(x+3, y, oBlock) == true {
speed = 0;
}
}
// 移动
y += yspeed;
x += xspeed;
解决这个问题的方法有很多,以下是一些建议:
- 使用更精确的碰撞检测: 你可以使用
place_meeting函数检测是否有碰撞发生,但你也可以使用collide_point函数检测是否有碰撞发生,并且可以指定碰撞的位置。 - 优化碰撞检测: 如果你有很多碰撞检测,建议你使用
collide_point函数代替place_meeting函数,因为collide_point函数更快。 - 使用更好的算法: 你可以使用更好的算法来检测碰撞,例如使用
AABB(Axis-Aligned Bounding Box) 算法或OBB(Oriented Bounding Box) 算法。 - 检查碰撞的位置: 你可以检查碰撞的位置,并根据位置来决定如何处理碰撞。
以下是修复代码:
/// Variables
x = room_width/4;
y = room_height/2;
fall = false;
xspeed = 0;
yspeed = 0;
rotspeed = 0;
/// Step
var _right = keyboard_check_direct(ord("D"));
var _left = keyboard_check_direct(ord("A"));
var _jump = keyboard_check_direct(vk_space);
var _moving = keyboard_check_direct(ord("A")) or keyboard_check_direct(ord("D"));
// 左右移动
if _left {
if rotspeed < 15 {
if rotspeed *-1 == 1 {
rotspeed += 0.2;
} else {
rotspeed += 0.1;
}
}
}
if _right {
if rotspeed > -15 {
if rotspeed *-1 == -1 {
rotspeed -= 0.2;
} else {
rotspeed -= 0.1;
}
}
}
// 旋转速度
image_angle += rotspeed;
if _moving == false {
if rotspeed != 0 {
rotspeed = lerp(rotspeed, 0, 0.01);
}
}
// 跳跃检测
if place_meeting(x, y+2, oBlock) == true {
yspeed = 0;
if _jump {
if xspeed < 5 and xspeed > -5 {
yspeed = -3;
} else {
yspeed = -4;
}
}
} else if place_meeting(x, y+2, oBlock) == false {
yspeed += 0.1;
}
// 水平碰撞检测
xspeed = -rotspeed;
if xspeed < 0 {
if place_meeting(x-3, y, oBlock) == true {
xspeed = 0;
}
} else if xspeed > 0 {
if place_meeting(x+3, y, oBlock) == true {
speed = 0;
}
}
// 移动
y += yspeed;
x += xspeed;
// 修复碰撞检测
if place_meeting(x, y, oBlock) == true {
// 修复卡在角落的问题
if rotspeed > 0 {
rotspeed = -rotspeed;
} else if rotspeed < 0 {
rotspeed = -rotspeed;
}
}
这段代码修复了卡在角落的问题,并且优化了碰撞检测。
评论 (0)