Windows Perfect Collision Detection

D

DarthTenebris

Guest
Hello there my fellow programmers!

I've been working on a project by following a youtube video, but with a few modifications I made myself.
The problem I'm having is that when my character hits a block he isn't supposed to be standing on (standing, which means hitting the block from any other side doesn't matter), the game is supposed to restart. However, the game only restarts when he is fully on the block. What is supposed to happen is, that when he touches even the edge of the block, the game is supposed to restart. Any idea what I'm doing wrong? Please help...

Here is my current code:
Code:
if !place_meeting(x,y + 1,obj_blockRed)
    {
    room_restart();
    }
 

Yal

šŸ§ *penguin noises*
GMC Elder
Right now, the code checks whether there's a block 1 pixel below the hero (that's what same x, y +1 means). You could always check for three positions at once and restart the room if either of them has a block there...
Code:
if (place_meeting(x,y + 1,obj_blockRed) or place_meeting(x + 1,y,obj_blockRed) or place_meeting(x - 1,y + 1,obj_blockRed))
    {
    room_restart();
    }
 
G

gamedev4life

Guest
what yal said.
the "y+1" means 1 pixel below the object youre coding for, because add to the y value/axis moves the player down because the origin (0,0) is the very top left corner of the room.

this is an invaluable resource (https://docs.yoyogames.com/) for learning gml. whenever you do the tutorials and you come across something you dont understand, look it up in the gm manual. thats what im doing and its helping a lot
 
D

DarthTenebris

Guest
@Yal That doesn't work...
Glitch.png
As you can see, the player (full red) is not supposed to be able to walk on the yellow tile, unless he is also yellow. (The same thing applies on the other colors, red, yellow, and blue). If the player's bottom left corner is on the yellow block then the game restarts. What I need is both bottom left and right corners, if touching a block that is not the same color as the player is, the game restarts. I can't think of how to do that :(
 
F

F_Clowder

Guest
Go read up on the bbox functions, those might help you out with your particular issue.
 
D

DarthTenebris

Guest
@gamedev4life I have my sprite origin set to the center (16,16; sprite width and height is 32x32), and it works for the gravity code.
Gravity:
Code:
if place_free(x,y + 1)
    {
    gravity = 1;
    }
else
    {
    gravity = 0;
    }
 
D

DarthTenebris

Guest
With some extra code for debugging, I found that the player can go up to x=223.5;y=352.39 and the first yellow block's x=224; y=384
I can't think of anything right now, my mind is so messed up. I made sure everything had their sprite origin set to the center of the sprite (center button at the sprite editor).
Usually what gets me is that I understand the logic, but I can't implement the code.

@F_Clowder I read it. Yet I don't understand a single thing. Can you explain more about it (I found it very hard to understand)?
 

NightFrost

Member
What is the player sprite's mask like? When the collision test offsets it by y+1 it should be going downwards by 1 pixel and - in situation displayed in your sreenshot - overlap both red and yellow box, giving collisions for both colors.

Come to think of it, how are your collision checks arranged? They do check for collision with every box color in succession? The behavior you describe above suggests as long as collision with red box fires, checking for a collision with yellow box is not made.
 
D

DarthTenebris

Guest
@NightFrost The player and blocks are all boxes. 32x32, no mask modifications made, default settings. And it's a full image, covers the whole area.
Unfortunately, as for the collision, it doesn't seem quite right to me. I was expecting a collision on red and yellow box, but it turned out to be wrong.
 
D

DarthTenebris

Guest
WAIT...
idk why but either me changing a little bit of code or me restarting game maker studio has fixed the problem! :O
Anyways thnx for helping!

*i'll be back if i need more help lol

EDIT:
@NightFrost Thanks for pointing me in the right direction! I quickly found the problem and fixed it :)
 
Last edited by a moderator:
Top