H
Hollywoo
Guest
I have a game I'm working on with 8-direction movement. This is the current code for movement:
This allows for movement in the chosen direction without diagonal movement being faster than normal horizontal and vertical movement.
My issue is, the collision detection method I would normally use, of just checking which way I'm moving and, if the distance is less than the rate at which I'd be moving that way, moving up to the object and stopping, doesn't seem to be working right. Instead I stop a bit away from it, and if I slide along the edge trying to push toward it, I'll inch closer and then get stuck.
Any suggestions for what I can put in those "else" sections to fix my collisions?
Code:
///Input | Movement | Shooting
//Input for movement
up = keyboard_check(ord('W'));
left = keyboard_check(ord('A'));
down = keyboard_check(ord('S'));
right = keyboard_check(ord('D'));
xDirection = right - left;
yDirection = down - up;
dir = point_direction(0, 0, xDirection, yDirection);
//Movement and Collisions
if (up || left || right || down)
{
if !place_meeting(x + lengthdir_x(walkSpeed, dir), y, Solid)
{
x += lengthdir_x(walkSpeed, dir);
}
else
{
while (!place_meeting(x + sign(xDirection), y, Solid))
{
x += sign(xDirection);
}
}
if !place_meeting(x, y + lengthdir_y(walkSpeed, dir), Solid)
{
y += lengthdir_y(walkSpeed, dir);
}
else
{
while (!place_meeting(x, y+sign(yDirection), Solid))
{
y += sign(yDirection);
}
}
}
My issue is, the collision detection method I would normally use, of just checking which way I'm moving and, if the distance is less than the rate at which I'd be moving that way, moving up to the object and stopping, doesn't seem to be working right. Instead I stop a bit away from it, and if I slide along the edge trying to push toward it, I'll inch closer and then get stuck.
Any suggestions for what I can put in those "else" sections to fix my collisions?