• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

collision problem

S

stepup2000

Guest
i know this is some very basic stuff but cant seem to wrap my head around it. im making a top down game andtThis is the code i use for movement/collision:

vspeed = 0;
hspeed = 0;
if(keyboard_check(ord("W"))) vspeed -= 4
image_speed = 1
if(keyboard_check(ord("S"))) vspeed += 4;
image_speed = 1
if(keyboard_check(ord("A"))) hspeed -= 4;
image_speed = 1
if(keyboard_check(ord("D"))) hspeed += 4;
image_speed = 1

speed = clamp(speed, -4, 4);
if place_meeting(x + hspeed, y, obj_wall)
{
hspeed = 0
}
if place_meeting(x, y + vspeed, obj_wall)
{
vspeed = 0
}

this code worked perfectly until i changed the way my character works. because instead of rotating the image angle when the character had to rotate i made a draw event and let the character draw the correct sprite and then rotate it the right way. but because of this reason i dont use a sprite anymore so that means he doesnt have a hitbox anymore. So right now everything goed trough the character. so i added this code to add a collisionbox:

sprite_collision_mask(1, false, 0, x - 13, y -13, x + 17, y + 13, 1, 0);

but this doesnt solve the problem. does anyone has a idea? thanks in advance.
 

Carloskhard

Member
First thing to point is that you are not writing the image_speed function inside the brackets so is alway executing and doing so 4 times,which is stupid.The right way should be like this:
vspeed = 0;
hspeed = 0;
if(keyboard_check(ord("W"))){
vspeed -= 4
image_speed = 1
}
if(keyboard_check(ord("S"))){
vspeed += 4;
image_speed = 1
}
if(keyboard_check(ord("A"))){
hspeed -= 4;
image_speed = 1
}
if(keyboard_check(ord("D"))){
hspeed += 4;
image_speed = 1
}
or even better, like this:
W=keyboard_check(ord("W")
S=keyboard_check(ord("S")
D=keyboard_check(ord("D")
A=keyboard_check(ord("A")

vspeed = S-W;
hspeed = D-A;
This last one saves resources and its better in general.

Second thing is using variables for values like speed value instead of just the number.This way you will be able to change stuff better in the future and even make dinamic changes in real time.So the result will be like this:

vspeed = 0;
hspeed = 0;
if(keyboard_check(ord("W"))){
vspeed -= speed
image_speed = img_spd;
}
if(keyboard_check(ord("S"))){
vspeed += speed;
image_speed = img_spd;
}
if(keyboard_check(ord("A"))){
hspeed -= speed;
image_speed = img_spd;
}
if(keyboard_check(ord("D"))){
hspeed += speed;
image_speed = img_spd;
}

And for the rest,I rather recommend you watching some tutorials for movement and sprites since I think this is very basic stuff and you should review a full video to understand it better.
I recomend you this one:
Hope it was helpful.
 
Last edited:
S

stepup2000

Guest
oh i see thank you, do you have any idea why the collision is busted tho?
 
S

stepup2000

Guest
the video didnt help sadly enough, i do understand how the movement works. But the reason why my collision doesnt work is because my character doesnt have a collision box (because he contantly changes sprite when you pick up another weapon.) Because of this i tried to seperate the sprite and collision box my letting the object draw the sprite in the draw event and add on top off that a collision box (that is consistant during all the different sprites) but it doesnt work (i guess because i made a mistake in adding the collision box but i dont know what is wrong)


draw event:
draw_sprite_ext(index, -1, x, y, 1, 1, rotation, c_white, 1) // i set rotation in another line of code but this is irrelevant right now

step step:
sprite_collision_mask(1, false, 0, x - 13, y -13, x + 17, y + 13, 1, 0); // the squere i need for collision
vspeed = 0;
hspeed = 0;
if(keyboard_check(ord("W"))) vspeed -= 4;{
image_speed = 1
}
if(keyboard_check(ord("S"))) vspeed += 4;{
image_speed = 1
}
if(keyboard_check(ord("A"))) hspeed -= 4;{
image_speed = 1
}
if(keyboard_check(ord("D"))) hspeed += 4;{
image_speed = 1
}
speed = clamp(speed, -4, 4);
if place_meeting(x + hspeed, y, blok)
{
hspeed = 0
}
if place_meeting(x, y + vspeed, blok)
{
vspeed = 0
}
 
S

stepup2000

Guest
The thing is you don't explain much about your setup for sprite drawing so I cannot guess, but I'm pretty sure you'll solve it following a tutorial for the matter.
oke i found out what the problem was, i used the wrong code.... isntead of changing the sprite mask i had to give it a completly different mask with the function mask_index = ....
it works perfectly now so thanks for your help anyway,
 

Carloskhard

Member
oke i found out what the problem was, i used the wrong code.... isntead of changing the sprite mask i had to give it a completly different mask with the function mask_index = ....
it works perfectly now so thanks for your help anyway,
I'm happy you solved it :)
I recommend you checking twice and debugging before asking in forums since its easier for you to find the problem debugging than for people that don't know the code and dont have access to it.
And if you end up asking for help, be sure to give as much info as possible!
Just a tip :)
 
S

stepup2000

Guest
I'm happy you solved it :)
I recommend you checking twice and debugging before asking in forums since its easier for you to find the problem debugging than for people that don't know the code and dont have access to it.
And if you end up asking for help, be sure to give as much info as possible!
Just a tip :)
Yeah i understand thanks, the problem was in de information i provided tho :p (I used sprite_collision_mask instead of mask_index) but i understand it can be very hard when you only have part of the code. I had to narrow it down tho because its a huge project and there is a lot of irrelevant information
 
Top