Jump to content
  • Advertisement
Sign in to follow this  
Kain5056

y = mx + b for sloped tiles

This topic is 2150 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to implement slopes in my first platform game in C++, and I seem to have some trouble.

I need to calculate the player's y position in a 45 degrees slope using the slope's size and coordinates and the player's x position.

I have found the equation y = m * x + b, but I'm not sure how to use it.

 

I need to find position.y using position.x, tile.x, tile.y, tile_size and player_size (both the player and the tiles are square so their width and their height are equal).

 

I know that m = ( y2 - y1 ) / ( x2 - x1 ) and b = y1 - m * x1

 

But I don't know how to find y1, y2, x1 and x2 based on the slope's degree.

 

I searched Google and found nothing, so if someone more experienced could help I would really appreciate it.

Thank you in advance. smile.png

Share this post


Link to post
Share on other sites
Advertisement

How much math have you had?

 

The math of a line is often taught in middle school math before algebra, so if you needed to look it up it implies you may not have math math skills yet for this.

 

I'm worried that giving a reply in how to compute the values may not help if you don't have the requisite skills.

Share this post


Link to post
Share on other sites

Even though I graduated more than a decade ago, I'm not really that good at math.

I'm willing to give it a try, though. The most I can lose is some of my time. smile.png

 

Here is what I figured out so far:

 

   float x , y1 , y2 , x1 , x2 , m , b;
    x = position.x + size - collision_box_position.x;
    y1 = Tile->y + tile_size;
    y2 = Tile->y;
    x1 = Tile->x + tile_size;
    x2 = Tile->x;
    m = ( y2 - y1 ) / ( x2 - x1 );
    b = y1 - m * x1;

    if( position.y + size >= m * x + b ) position.y = m * x + b - size;

 

With the above code it works quite nicely, but for some reason it is offset horizontally by the player's size. I tried adding and subtracting it, but it dod not work.

Share this post


Link to post
Share on other sites

x = position.x + size - collision_box_position.x;

 

That looks wrong. It depends whether you are approaching the slope from the left or the right side, whether you want to adjust the x position by the player size or not (and whether it is an upwards or downwards slope too). EDIT: I suspect it works for approaching slopes from one direction but not the other, or that it works for upward slopes but not downwards ones.

 

It's probably best to have the logical sprite position to be in the middle at the players feet as well.

Edited by Paradigm Shifter

Share this post


Link to post
Share on other sites

slope = (y2 - y1) / (x2 - x1). Which gives you the number of y pixels to move for each x pixel.

 

For a 45 degree angle, the slope is 1 (1 pixel up for each pixel right)

 

So:

 

position.y = (tile.y + (tilesize - 1) + (position.x - tile.x) * slope) - playersize;

 

or 

 

position.y = (tile.y + (tilesize - 1) - (position.x - tile.x) * slope) - playersize / 2;

 

The first one positions the sprite using the top of the rectangle, the second one uses the center point of the character. Depending on how your tiles are designed, this may put you on top of a pixel.

Edited by cdoty

Share this post


Link to post
Share on other sites

Don't have time to explain the basic functions up through Algebra I, but I can say this:

 

y = mx + b is just an equation for a line. It doesn't solve for anything, really. Just some context:

 

m is the slope of the line.

 

y / x are just used to represent y and x in a coordinate plane. They're fairly abstract, but you can solve for them if you have m, b, and either x or y.

 

y2 / y1 and all of those mean that they're numbers you need to solve the equation.

Share this post


Link to post
Share on other sites

How much math have you had?

 

The math of a line is often taught in middle school math before algebra, so if you needed to look it up it implies you may not have math math skills yet for this.

 

I'm worried that giving a reply in how to compute the values may not help if you don't have the requisite skills.

You'd be surprised by how many people have ADHD and are affected in such a way that they were never able to understand math. I myself struggle with simple addition and subtraction like 14.37 - 10.85, It's going to take me a while to do this in my head and the answer might be wrong in the end.

 

I also can't do fractions,equations, rational expressions, exponentiation and many more things, simply because I couldn't understand them waay back in middle school and earlier which affected me in later grades.

 

Sad to say, you will meet more people like this. They won't necessarily have ADD/ADHD, but they could be affected by something else.

Edited by farmdve

Share this post


Link to post
Share on other sites

Don't think anyone mentioned that b is the starting point on the Y axis. So if the slope was flat (m=0), or if you hadn't gone any distance up or down the slope (x=0) the equation reduces to Y = b.

Edited by Godmil

Share this post


Link to post
Share on other sites

Slope tile collision is deceptively difficult, depending on how many situations you want it to deal with. And indeed, there's very little info on the net about it. Particularly doing anything more than 45 degree tiles where you climb up with the corner of your collision box rather than the center/feet of the character.

 

One of these days I'll write an article on it... when I understand it better myself.

 

45 degree slopes, on the ground only (not ceiling) are probably doable for a relative newbie... I assume you already have solid tile collision working? What approach are you using, separate X and Y movement steps?

 

As for your line equation, for 45 degree tiles the slope is 1, so it cancels out... no multiplication is necessary. X and Y (within the tile) are always equal to eachother, either positively or negatively.

 

So assuming positive X is right, positive Y is down, and you're on a 45 degree slope tile where the bottom left is solid and top right is open... then to stand on the slope, figure your X position within the tile, and set Y within the tile equal to that. For the other direction (bottom right solid, top left open), set Y equal to tilesize - 1 - X. Notice the -1. That's one of the things that makes it so difficult. One direction is always off by 1 pixel from the other.

 

Another problem is that when walking up a slope onto flat ground, with your center point resting on the slope, then the bottom corner of your collision box will ruin into the "wall" of the flat ground tile before you get to the top of the slope.

 

Draw lots of diagrams and study the situations that come up when you're in the air, walking on the ground, walking up a slope, down a slope, whether there's a flat ground tile at the top/bottom of the slope, or a wall... lots of situations, and in my experience, there's no simple, elegant solution.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!