I am working on a game(a platformer) that is physics and momentum based. I want to make it so that the player(a single dot) can bounce off of the lines with correct physics, but to do that, I need to know which line it is touching. Knowing if it is touching a line or not is simple. I just have to check for a white dot where the player is. The problem is that I cannot find out which line the player is touching. I thought of making a ton of rects along each line and testing for the player, but that would cause too much lag. Does anyone have any ideas? :/
|
You could do it as you did above (get the distance to the line and if it's less than some number, they are colliding), or you could see if the player's position is a point on the line. If so, they are colliding. The downside of doing it this way is you have to do intra-frame collision checking (ie if the players speed is 2 pixels, you could miss an intersection with a line 1 pixel wide), IE project the player's path for the next frame as a line segment, and if that line segment intersect a line, the player will intersect with the line within the next frame, so go to that point in time and calculate the collision and how far the players moves afterwards but before the next frame. As I understand your method the "less than" will cover the situation where a player goes past a line (since the distance will be negative), but that's assuming you cover distance with respect to the orientation of the line (eg lines at the top of the screen are "upside-down" and lines at the bottom are "right-side up;" therefore, being above the top line and below the bottom line will be a "negative distance." Of course, this is all moot if the player will only ever move one pixel at a time. :) |
|||||||||||||||||
|
I figured it out. Since lines can be seen as equations, you can detect if the player(or whatever)'s rise is equal(or similar) to the rise of the line. To convert a line to a segment, just make pygame check to see if the player is colliding with a rect that's two corners are the start and end points of the line. Make sure the rect has a positive side though, or Here is how I calculated it:
First, I took checked if the player(or whatever) was in range of the line. (using the previously mentioned method) Then I calculated the rise of the line by dividing the Y length by the X length. I did the same thing for the player, but finding the length of a line from the line's starting point to the player.
Next I subtracted the line rise from the player rise. Then I found the absolute of that. If the absolute value(the difference in the rises of the lines) was less than a specific number(based on the width of the line), it was considered a collision. I used It is recommended that you check for each pixel the player moves to see if it is colliding. |
||||
|