• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Windows Game freeze upon colliding with a corner.

A

a_lone_

Guest
I've spent a while getting my movement code with proper acceleration and smooth collisions to work with as a little code as possible, and while I was doing so my ideas changed a lot and the code still has remnants of old organization. At some point in the process, I discovered that whenever I made a pixel-perfect collision with a corner, the game window would freeze and have to be closed using the IDE's stop button, or a task manager end task. I thought I'd fix this probably later, but I still have not been able to locate its source.

Code:
//get input
yin = (keyboard_check(ord("S")) - keyboard_check(ord("W")));
xin = (keyboard_check(ord("A")) - keyboard_check(ord("D")));
inDir = point_direction(0,0,xin,yin);

//round small velocities to 0
if abs(vx) < 0.3 {
    vx = 0;
}
if abs(vy) < 0.3 {
    vy = 0;
}

//friction
vx -= kfrict*vx;
vy -= kfrict*vy;

//acceleration
ax = (((dcos(inDir))*abs(xin)*movespeed)/mass);
ay = (((dsin(inDir))*abs(yin)*movespeed)/mass);

aMag = point_distance(x,y,x+ax,y+ay);
aDir = point_direction(x,y,x+ax,y+ay);

if abs(vy+ay) <= abs(yin*vmax) and abs(vx+ax) <= abs(xin*vmax) {
    vy += ay;
    vx += ax;
} else if abs(vy+ay) <= abs(yin*vmax) {
    vy += ay;
} else if abs(vx+ax) <= abs(xin*vmax) {
    vx += ax;
} else if abs(vy + ay) < abs(vy) {
    vy += ay;
} else if abs(vx + ax) < abs(vx) {
    vx += ax;
}

//collisions and movement

if place_meeting(x-vx,y-vy,oWall) {
    while !place_meeting(x-sign(vx),y-sign(vy),oWall) {
        y-=sign(vy);
        x-=sign(vx);
    }
    if !place_meeting(x,y-vy,oWall) {
        y -= vy;
    }
    if !place_meeting(x-vx,y,oWall) {
        x -= vx;
    }
} else {
    x -= vx;
    y -= vy;
}
Feel free to roast my code as well, like I said it's a bit scatterbrained and could be smaller and more organized.
 
G

Gamedev

Guest
//horizontal collision
if (place_meeting(x+hsp,y,obj_wall))
{
while(!place_meeting(x+sign(hsp),y,obj_wall))
{
x += sign(hsp);
}
hsp = 0;
}
x += hsp;

//vertical collision
if (place_meeting(x,y+vsp,obj_wall))
{
while(!place_meeting(x,y+sign(vsp),obj_wall))
{
y += sign(vsp);
}
vsp = 0;
}
y += vsp;

you can just paste this code to step.

And additionally here is a way do acceleration and friction

Create Event:
//Set Game Variables
hsp = 0;
vsp = 0;
movespeed = 2;
acc = 0.2;
fric = 0.3;
dir = 1;

Step Event:
//Inputs
right = keyboard_check(ord("D"));
left = keyboard_check(ord("A"));
down = keyboard_check(ord("S"));
up = keyboard_check(ord("W"));

movex = right-left;
movey = down-up;

//Accelerate
if movex!=0
{
hsp += acc*movex;
hsp = clamp(hsp,-movespeed,movespeed)
}
if movey!=0
{
vsp += acc*movey;
vsp = clamp(vsp,-movespeed,movespeed)
}

if movex = 0
{
hsp = lerp(hsp,0,fric)
}
if movey = 0
{
vsp = lerp(vsp,0,fric)
}

//horizontal collision
if (place_meeting(x+hsp,y,obj_wall))
{
while(!place_meeting(x+sign(hsp),y,obj_wall))
{
x += sign(hsp);
}
hsp = 0;
}
x += hsp;

//vertical collision
if (place_meeting(x,y+vsp,obj_wall))
{
while(!place_meeting(x,y+sign(vsp),obj_wall))
{
y += sign(vsp);
}
vsp = 0;
}
y += vsp;

Solid here is obj_wall
hsp is hspd
vsp is vsp.
HAVE A NICE DAY :)
 
Last edited by a moderator:
Top