flerpyderp
Member
Trying to make a simple Wolfenstein/Doom clone, and I can't find many resources on the topic of movement in FPS games in Game Maker. The tutorials that I've watched ignored the problem of incorrect diagonal movement speed entirely, and I've relied mainly on looking in threads about top-down game movement to try to find a solution.
The square root of 2 approach I used here doesn't give the correct diagonal speed, and I'm wondering if my approach to making movement for this type of game is fundamentally wrong.
If anyone has experience making this type of movement, please give me a nudge in the right direction.
Code:
//Mouse control
mouseL = mouse_check_button_pressed(mb_left);
mouseR = mouse_check_button_pressed(mb_right);
direction -= sensitivity * (display_mouse_get_x() - display_get_width() / 2);
display_mouse_set(display_get_width() / 2,display_get_height() / 2);
//Keyboard control
keyF = keyboard_check(ord("W"));
keyB = keyboard_check(ord("S"));
keyR = keyboard_check(ord("D"));
keyL = keyboard_check(ord("A"));
keysFB = keyboard_check(ord("W")) - keyboard_check(ord("S"));
keysRL = keyboard_check(ord("D")) - keyboard_check(ord("A"));
//Accelerate
velFB += keysFB * accel;
velRL += keysRL * accel;
//Diagonal
if (keyF && keyR) || (keyF && keyL) || (keyB && keyR) || (keyB && keyL)
{
velFinal = velMax / 1.41421356237;
}
else velFinal = velMax;
//Limit
velFB = clamp(velFB,-velFinal,velFinal);
velRL = clamp(velRL,-velFinal,velFinal);
//Decelerate
if (keysFB == 0) && (abs(velFB) >= accel)
{
velFB -= sign(velFB) * accel;
if (velFB > -accel) && (velFB < accel) velFB = 0;
}
if (keysRL == 0) && (abs(velRL) >= accel)
{
velRL -= sign(velRL) * accel;
if (velRL > -accel) && (velRL < accel) velRL = 0;
}
//Translate velocities to x and y
var velX = lengthdir_x(velFB,direction) + lengthdir_x(velRL,direction - 90);
var velY = lengthdir_y(velFB,direction) + lengthdir_y(velRL,direction - 90);
//Collision
if (!place_meeting(x+velX,y,par_solid))
{
x += velX;
}
else
{
while (!place_meeting(x+sign(velX),y,par_solid))
{
x += sign(velX);
}
velX = 0;
}
if (!place_meeting(x,y+velY,par_solid))
{
y += velY;
}
else
{
while (!place_meeting(x,y+sign(velY),par_solid))
{
y += sign(velY);
}
velY = 0;
}
If anyone has experience making this type of movement, please give me a nudge in the right direction.