Collision help with decimals

C

Cabra

Guest
Hello everyone. I'm having trouble to understand how collision detection works, I'm really beginner and not being the english my main leanguage, manuals doesn't help.

Well, with that said, I'm trying to emulate Super Mario World physics, for now I only have the horizontal movement partially done and it's working fine. The problem comes when I try to make de colision detection with a wall, I tried a lot of things but nothing worked. From my experience (maybe I'm wrong), I've seen that Game Maker doesn't handle very well decimals numbers, so I'm trying to convert those decimals numbers into integers to make it pixel-perfect.

So from my logic, I say for example: "If I collide with a wall from the left, set my horizontal speed to 0, move my player -1 pixel to that specific wall and also convert my X axis number into an integer". But I don't have idea how to take it to the practice, there's still a lot of things that I need to learn from GML so a help wouldn't hurt. Also, I leave you the code and a gif so you see better what I have done.

(Oh, I almost forgot, I'm using a version of Game Maker 8 that I took from my school because I don't really have the money to pay for the Studio 2 and I'm really beginner, I'm using this version because I want to expand my knowledge and after that buy Studio 2)

Create event:

Code:
xsp = 0;
vsp = 0;

acc = 0.1;
maxSpeed = 1.7;
slide = 0;
Step event:

Code:
//Variables
x += xsp;
y += vsp;

kcright = keyboard_check(vk_right);
kcleft = keyboard_check(vk_left);

slide = acc * 3;

//Sprites
if (xsp != 0) {

    sprite_index = sp_marioRun;
    image_speed = 0.15;

    }
    
else {

    sprite_index = sp_marioQuiet;

    }

//Físicas horizontales
if (xsp > maxSpeed) || (xsp < (maxSpeed * -1)) || !(kcright || kcleft) {

    xsp -= (sign (xsp) * acc);
    
    }
    
//Derecha
if (kcright) {

    image_xscale = 1;

    if (xsp < 0) {
    
        xsp += slide;
        sprite_index = sp_marioSlide;
        
        }
    
    else {
    
        xsp += acc;
    
        }
        
    }
    
//Izquierda
else if (kcleft) {

    image_xscale = -1;

    if (xsp > 0) {
    
        xsp -= slide;
        sprite_index = sp_marioSlide;
    
        }
    
    else {
    
        xsp -= acc;
    
        }
        
    }
This is what I have:

ezgif-6-9c88165f6548.gif
 
R

robproctor83

Guest
Here see this tutorial, it explains it in good detail.


*edit - And, if you wanted to take a step further

 
C

Cabra

Guest
Here see this tutorial, it explains it in good detail.


*edit - And, if you wanted to take a step further

Thanks for you reply! Ok, now, here's the thing.

Before I saw a few videos of Shaun explaining how colisions works, and well, he just reinforced as I thought they worked. But, in his logic, you need to check if a object is about to collide, so I guees it means that you really never touch the wall. But I wonder, is not the same to say "if I' hit the wall"? What I'm trying to say, for me i'ts the same result but programmed in other way.
 
R

robproctor83

Guest
Well, there different ways you can do this and this is just one of them. The way this works essentially is you only check if a collisions is going to happen, not if a collisions is happening. It does this by checking the each vector and it's corresponding speed to see if it will have a collision in the next step and if there is a collision about to happen instead of moving the character the full distance of their movement speed you just move the player as close to the future collision as possible.
 
C

Cabra

Guest
Well, there different ways you can do this and this is just one of them. The way this works essentially is you only check if a collisions is going to happen, not if a collisions is happening. It does this by checking the each vector and it's corresponding speed to see if it will have a collision in the next step and if there is a collision about to happen instead of moving the character the full distance of their movement speed you just move the player as close to the future collision as possible.
Allright, it's an intuitive way to do it too I guess, but how can I make that in code?
 
Top