Need Advice for Movement Code

Trex0n

Member
So the past couple months I've been spending what time and patience I could muster trying to figure out how I should handle my movement, specifically in regards to real numbers, angles and collisions. Ideally I would like to program a collision system that would enable me to have real value or floating numbers for the player's position, without issues. However, as you can imagine, things haven't really been working out in practice. Specifically in regard to corner collisions. Currently the movement I have is the basic Spalding code (H. Collisions, Apply Hspeed, V. Collisions, Apply Vspeed).

Now what I want to know is first, whether I could reasonably program what I want with my limited skillset, and second, whether or not it's actually worth it. If there is a better option around this, I'm willing to look into it. The main reason I want to have non-integer compatible collisions is for the sake of angular movement and accuracy, however, if I should be going about this another way, I'd like to know.

I appreciate any help I can get!
//Collision Functions (object in this is just oSolid)
GML:
function IndigoPhysics_HorizontalCollision(Object) {
    //Collision
    if (place_meeting(RoundTowardsZero(x) + RoundFromZero(xVelocity),y,Object)) {
        x = RoundTowardsZero(x);

        while (!place_meeting(x + sign(xVelocity),y + sign(yVelocity),Object)) {
            x += sign(xVelocity);
        }
      
        xVelocity = 0;
    }
}

function IndigoPhysics_VerticalCollision(Object) {
    //Collision
    if (place_meeting(x,RoundTowardsZero(y) + RoundFromZero(yVelocity),Object)) {
        y = RoundTowardsZero(y);

        while (!place_meeting(x,y + sign(yVelocity),Object)) {
            y += sign(yVelocity);
        }
      
        yVelocity = 0;
    }
  
    //Grounding
    if (place_meeting(x,y + 1,Object) && yVelocity == 0) Grounded = true else if (yVelocity > 0) Grounded = false; 
}
Player Step Event
GML:
IndigoPhysics_HorizontalCollision(oSolid);
x += xVelocity;
IndigoPhysics_VerticalCollision(oSolid);
y += yVelocity;
So this is my main collision code. For context Indigo Physics is just a name for this group of functions. The function RoundFromZero(x) ceils positive numbers and floors negative numbers, RoundTowardsZero(x) does the opposite. Those functions are used to accuratly handle real value positions and velocities prior to them being floored through the place_meeting function. The //Grounding part is just to tell when I can jump.
 
Last edited:

Nocturne

Friendly Tyrant
Forum Staff
Admin
Now what I want to know is first, whether I could reasonably program what I want with my limited skillset, and second, whether or not it's actually worth it.
This, I'm afraid, is an impossible question to answer! Your "limited skillset" may actually be quite advanced!!! Who knows? Also, while you might consider it limited just now, it'll constantly grow and improve, especially doing things like you want to do and challenging yourself. :)

As for it being worth it, again, that depends on you and the game you are making. For a precision platformer, for example, then YES it is absolutely worth it! However for a top down shooter, it probably isn't...

One way you can go about dealing with this issue is to store the fractional position in a variable at the start of each step, and then add it on when the total of the fractions each step goes above 1. SO, you have a variable like x_frac set to 0 on create. At the start of the step event, you use the "frac" function on "x" to get the fraction of the position and you add it to x_frac, then you store the floored x position which you'll use to perform your movement. However, before moving you check x_frac to see if it's over 1 and if it is then you add 1 to x and start again... This is essentially how I've coded my current wip platform game... :)

That said, it would be great if you could clarify on this:
However, as you can imagine, things haven't really been working out in practice. Specifically in regard to corner collisions.
Most collision systems should work just fine (and use float positions, as that's what GM defaults to) without having to do any funky maths stuff with fractions... so before diving further down that rabbit-hole, it would be best to explore what your issue actually IS first! Feel free to post some code for us to look at and explain the actual problem in more detail. :)
 

Trex0n

Member
This, I'm afraid, is an impossible question to answer! Your "limited skillset" may actually be quite advanced!!! Who knows? Also, while you might consider it limited just now, it'll constantly grow and improve, especially doing things like you want to do and challenging yourself. :)

As for it being worth it, again, that depends on you and the game you are making. For a precision platformer, for example, then YES it is absolutely worth it! However for a top down shooter, it probably isn't...

One way you can go about dealing with this issue is to store the fractional position in a variable at the start of each step, and then add it on when the total of the fractions each step goes above 1. SO, you have a variable like x_frac set to 0 on create. At the start of the step event, you use the "frac" function on "x" to get the fraction of the position and you add it to x_frac, then you store the floored x position which you'll use to perform your movement. However, before moving you check x_frac to see if it's over 1 and if it is then you add 1 to x and start again... This is essentially how I've coded my current wip platform game... :)

That said, it would be great if you could clarify on this:

Most collision systems should work just fine (and use float positions, as that's what GM defaults to) without having to do any funky maths stuff with fractions... so before diving further down that rabbit-hole, it would be best to explore what your issue actually IS first! Feel free to post some code for us to look at and explain the actual problem in more detail. :)
Thanks for the response, I really appreciate it. At the moment I'm not sure if I want to make a 'precision platformer', I just want to be able to stop the player from colliding into walls if their position or velocity isn't an integer. If you could, what benefits would the fraction thing have? Could you maybe elaborate on how it works, I've never used anything like it before. (Also I added some of the relevant code above)

Thanks again :)
 
Top