small collision bug

P

piksil_demon

Guest
honestly with how much i use this forum i might aswell have you guys program the whole damn game lol.
anyways, heres my problem. when my characters sprite hits the wall, i cant move until the sprite is no longer hitting it.. however, if i touch it at a certain angle, it makes movement impossible. here it my character code so you can play around with it a bit

//variables
var up, down, left, right, arrow;
up = keyboard_check (ord('W'));
down = keyboard_check (ord('S'));
left = keyboard_check (ord('A'));
right = keyboard_check (ord('D'));

(ps, in this case spd is = 3)

//player movemnt
image_angle = point_direction(x, y, mouse_x, mouse_y);

if right {
speed = spd; direction = 0;
}
if up {
speed = spd; direction = 90;
}
if left {
speed = spd; direction = 180;
}
if down {
speed = spd; direction = 270;
}

if up and right {
speed = spd; direction = 45;
}
if up and left {
speed = spd; direction = 135;
}
if down and left {
speed = spd; direction = 225;
}
if down and right {
speed = spd; direction = 315;
}

if! up and !down and !left and !right {speed=0};

//collision
if instance_place (x,y,obj_enemy_parent) {hp -= global.enatk;}
if instance_place (x,y, obj_wall)speed = 0;

i know that in theory i need to make it be able to detect where the wall is, and only prevent movement in that direction, but i cant figure out howto do that without rewriting the whole thing.
 
T

TDSrock

Guest
Let me encase that code in a code lock before moving on:
Code:
//variables
var up, down, left, right, arrow;
up = keyboard_check (ord('W'));
down = keyboard_check (ord('S'));
left = keyboard_check (ord('A'));
right = keyboard_check (ord('D'));

(ps, in this case spd is = 3)

//player movemnt
image_angle = point_direction(x, y, mouse_x, mouse_y);

if right {
speed = spd; direction = 0;
}
if up {
speed = spd; direction = 90;
}
if left {
speed = spd; direction = 180;
}
if down {
speed = spd; direction = 270;
}

if up and right {
speed = spd; direction = 45;
}
if up and left {
speed = spd; direction = 135;
}
if down and left {
speed = spd; direction = 225;
}
if down and right {
speed = spd; direction = 315;
}

if! up and !down and !left and !right {speed=0};

//collision
if instance_place (x,y,obj_enemy_parent) {hp -= global.enatk;}
if instance_place (x,y, obj_wall)speed = 0;
EDIT:
First off, a note on what you wrote. you keek on saying speed = spd. Why not just edit your if! up and !down and !left and !right {speed=0}; to include an else block and set the speed to spd there? Simplify things quite a bit for you.

The problem:
So what's going on is that you are looking for an instance where the object currently is isntead of where the he will be in the next step. So instead of doing
Code:
if(instance_place(x,y,obj_enemy_parent){
//collision stuff
}
do this:
Code:
if(instance_place(x + *horizontal movementspeed* ,y + * vertical movementspeed* ,obj_enemy_parent){
//collision stuff
}
How to get those two variables? Well there is the function lenghtdir_x and lengthdir_y.

The importance of this is visualized extremely well in THIS video.
Note that the video I linked to explains the following code:
Code:
//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;
 
Last edited by a moderator:
P

piksil_demon

Guest
Let me encase that code in a code lock before moving on:
Code:
//variables
var up, down, left, right, arrow;
up = keyboard_check (ord('W'));
down = keyboard_check (ord('S'));
left = keyboard_check (ord('A'));
right = keyboard_check (ord('D'));

(ps, in this case spd is = 3)

//player movemnt
image_angle = point_direction(x, y, mouse_x, mouse_y);

if right {
speed = spd; direction = 0;
}
if up {
speed = spd; direction = 90;
}
if left {
speed = spd; direction = 180;
}
if down {
speed = spd; direction = 270;
}

if up and right {
speed = spd; direction = 45;
}
if up and left {
speed = spd; direction = 135;
}
if down and left {
speed = spd; direction = 225;
}
if down and right {
speed = spd; direction = 315;
}

if! up and !down and !left and !right {speed=0};

//collision
if instance_place (x,y,obj_enemy_parent) {hp -= global.enatk;}
if instance_place (x,y, obj_wall)speed = 0;
EDIT:
First off, a note on what you wrote. you keek on saying speed = spd. Why not just edit your if! up and !down and !left and !right {speed=0}; to include an else block and set the speed to spd there? Simplify things quite a bit for you.

The problem:
So what's going on is that you are looking for an instance where the object currently is isntead of where the he will be in the next step. So instead of doing
Code:
if(instance_place(x,y,obj_enemy_parent){
//collision stuff
}
do this:
Code:
if(instance_place(x + *horizontal movementspeed* ,y + * vertical movementspeed* ,obj_enemy_parent){
//collision stuff
}
How to get those two variables? Well there is the function lenghtdir_x and lengthdir_y.

The importance of this is visualized extremely well in THIS video.
Note that the video I linked to explains the following code:
Code:
//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;
before i go and try to implement this, will this code work if my character gets put in a corner, and for 8 directional movement?
 
T

TDSrock

Guest
The code Shaun explains works quite well for movement that is not faster then the width of your wall objects. Watch the video I linked too and you should be able to answer the question you posed yourself.

Yes it will work perfectly fine, again as-long as the velocity values don't exceed the width of your wall object
 
P

piksil_demon

Guest
The code Shaun explains works quite well for movement that is not faster then the width of your wall objects. Watch the video I linked too and you should be able to answer the question you posed yourself.

Yes it will work perfectly fine, again as-long as the velocity values don't exceed the width of your wall object
it dosnt answer it because it dosnt tell me HOW to. the only way i can think to implement it is to rewrite my whole movement code which is out of the question
 
P

piksil_demon

Guest
do you know how to implement that without having to rewrite my code? i BARLEY figured out how to make the code as is lol
 
Top