top down movement collission

B

brrod

Guest
im having troubles with my collision with walls for my top down game. this is my code:

vspeed = 0;
hspeed = 0;
if(keyboard_check(ord("W"))) vspeed -= 4;
if !audio_is_playing(so_footsteps) {
audio_play_sound(so_footsteps, 1, false)
}
if(keyboard_check(ord("S"))) vspeed += 4;
if !audio_is_playing(so_footsteps) {
audio_play_sound(so_footsteps, 1, false)
}
if(keyboard_check(ord("A"))) hspeed -= 4;
if !audio_is_playing(so_footsteps) {
audio_play_sound(so_footsteps, 1, false)
}
if(keyboard_check(ord("D"))) hspeed += 4;
if !audio_is_playing(so_footsteps) {
audio_play_sound(so_footsteps, 1, false)
}
speed = clamp(speed, -4, 4);

and the collision with the wall is:

speed = 0
direction = 0

the problem with this is when im colliding with a wall i wont be able to move towards another point. lets say im pressing w and d at the same time and my back is facing the wall. i should still be able to move towards the right, but this doesnt work because he puts my speed at 0. I understand the problem but I dont know how to fix this. Im very new to game maker so any help is helpfull. thanks in advance
 
T

ThePropagation

Guest
the proper code on the player object is

Code:
if place_meeting(x + hspeed, y, objWall)
{
     hspeed = 0
}

if place_meeting(x, y + vspeed, objWall)
{
     vspeed = 0
}
 
B

brrod

Guest
the proper code on the player object is

Code:
if place_meeting(x + hspeed, y, objWall)
{
     hspeed = 0
}

if place_meeting(x, y + vspeed, objWall)
{
     vspeed = 0
}
thanks for your reply it works, but now i have the problem that i get stuck in the wall when i rotate my character (because the hitbox rotates and gets stuck in the wall)
 

flerpyderp

Member
thanks for your reply it works, but now i have the problem that i get stuck in the wall when i rotate my character (because the hitbox rotates and gets stuck in the wall)
I'm guessing that you're rotating the character's sprite using image_angle. Instead, create a variable in the create event to store the angle the character is facing. Then, in the draw event, use draw_sprite_ext and use the variable you created for the "rot" argument. Now when you want to rotate the sprite, use this variable instead of image_angle. This will rotate the sprite without rotating the hitbox.
 
B

brrod

Guest
I'm guessing that you're rotating the character's sprite using image_angle. Instead, create a variable in the create event to store the angle the character is facing. Then, in the draw event, use draw_sprite_ext and use the variable you created for the "rot" argument. Now when you want to rotate the sprite, use this variable instead of image_angle. This will rotate the sprite without rotating the hitbox.
yeah i tried that now and it works perfectly, now the only thing left with this method is that i cant get the animation to work, even if i put image_speed = 1
 

Joe Ellis

Member
the proper code on the player object is

Code:
if place_meeting(x + hspeed, y, objWall)
{
     hspeed = 0
}

if place_meeting(x, y + vspeed, objWall)
{
     vspeed = 0
}
That's the most basic and most of the time the worst way you can do collision, the player will stop dead every time it touches a wall, rather than sliding along it like pretty much every game does, I definitely wouldn't tell people that's the proper way in future
 
B

brrod

Guest
That's the most basic and most of the time the worst way you can do collision, the player will stop dead every time it touches a wall, rather than sliding along it like pretty much every game does, I definitely wouldn't tell people that's the proper way in future
what would you recommend then? cuz i really dont know ho i could do that
 
Last edited by a moderator:
Top