Objects "sliding" with polygon collision detection

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
testtext_
Prole
Posts: 2
Joined: Fri Mar 06, 2015 1:30 am

Objects "sliding" with polygon collision detection

Post by testtext_ »

When using polygons and circles for collisions between platforms and other objects, is there a way to have objects fall when in the air, but not "slide" down sloped platforms (this is using my own collision detection code, not love.physics)? This happens because the object is "popped out" of the platform at an angle when it is sloped, causing it to move sideways, but I can't think of a good way to fix it that doesn't cause other problems:

1. Stop applying gravity when on the ground. This will make it impossible to tell when the object walked off the edge of the platform.

2. Once the object hits a platform, start moving it in the direction of the previous frame's collision instead of adding gravity. This will cause the object to "stick" to the platform, which is not a big problem, except objects will start running down walls even when they are supposed to fall off of the sharp corner of a platform.

3. Add code to "pop out" the object in a specific direction (in this case up), instead of the shortest way. This would be ideal, until the object hits a wall, and suddenly reappears at the top of the wall.

What is the recommended way to handle this? I have a demo attached that demonstrates the issue:
Attachments
example.love
(4.68 KiB) Downloaded 138 times
User avatar
Skeiks
Citizen
Posts: 51
Joined: Wed Jan 28, 2015 1:51 pm

Re: Objects "sliding" with polygon collision detection

Post by Skeiks »

testtext_ wrote:When using polygons and circles for collisions between platforms and other objects, is there a way to have objects fall when in the air, but not "slide" down sloped platforms (this is using my own collision detection code, not love.physics)? This happens because the object is "popped out" of the platform at an angle when it is sloped, causing it to move sideways, but I can't think of a good way to fix it that doesn't cause other problems:
What are you doing to detect collisions, and what is your final goal? I don't have the ability to look through your .love too well at the moment but it seems like you might be using too complex a solution for what you want to do. Complex polygon to polygon collision detection isn't really necessary in a lot of game situations. You might be able to accomplish what you want by abstracting the circle shape down to a set of points instead of using (what I assume to be) a distance based algorithm.

1. Stop applying gravity when on the ground. This will make it impossible to tell when the object walked off the edge of the platform.
My preferred solution for platformers is to ALWAYS apply gravity, and when an object is on the ground I push it up using a while loop and set the vertical speed of the object to 0. Here's some psuedocode. Imagine p1 is the player object, the circle in your demo. In my scenario, p1.x is the center the of the circle, and p1.y is the bottom of the circle.

Image

Code: Select all

p1.ySpeed = p1.ySpeed + p1.gravity
p1.y = p1.y + p1.ySpeed
while(collideWithWorld(p1.x, p1.y))
   p1.y = p1.y-1
   p1.ySpeed = 0  
end
If you want to stop applying gravity when the object is on the ground, you could just check a few pixels below for a collision with another object. When there is no collision, the object is not on the ground. This solution would apply to your method as well. Either this, or you might have to start applying friction to objects in your physics engine. I've never done it but I've heard it's a pain.
2. Once the object hits a platform, start moving it in the direction of the previous frame's collision instead of adding gravity. This will cause the object to "stick" to the platform, which is not a big problem, except objects will start running down walls even when they are supposed to fall off of the sharp corner of a platform.
True.

3. Add code to "pop out" the object in a specific direction (in this case up), instead of the shortest way. This would be ideal, until the object hits a wall, and suddenly reappears at the top of the wall.
About abstracting the object down to points, if you have 4 points for an object, then you 'pop' it out in whatever direction the points are in. If the left point is hit, pop the object out to the right. If the top point is hit, pop it down.

Does that help any? I might be able to give more helpful advice if I knew what you were trying to create. Also hope I didn't just explain a bunch of stuff you already knew haha.
testtext_
Prole
Posts: 2
Joined: Fri Mar 06, 2015 1:30 am

Re: Objects "sliding" with polygon collision detection

Post by testtext_ »

Skeiks wrote:What are you doing to detect collisions, and what is your final goal? I don't have the ability to look through your .love too well at the moment but it seems like you might be using too complex a solution for what you want to do. Complex polygon to polygon collision detection isn't really necessary in a lot of game situations. You might be able to accomplish what you want by abstracting the circle shape down to a set of points instead of using (what I assume to be) a distance based algorithm.
Maybe using points could work better for this, but I already had this polygon collision library, so I thought that using it could be easier than separately implementing it with points/rays.
Skeiks wrote:
2. Once the object hits a platform, start moving it in the direction of the previous frame's collision instead of adding gravity. This will cause the object to "stick" to the platform, which is not a big problem, except objects will start running down walls even when they are supposed to fall off of the sharp corner of a platform.
True.
I guess that one solution to that would be to reset the position if the slope is too large, and start applying gravity again.
Skeiks wrote:My preferred solution for platformers is to ALWAYS apply gravity, and when an object is on the ground I push it up using a while loop and set the vertical speed of the object to 0.
...
About abstracting the object down to points, if you have 4 points for an object, then you 'pop' it out in whatever direction the points are in. If the left point is hit, pop the object out to the right. If the top point is hit, pop it down.
I might just do that (although that method isn't without issues, either). As I said above, my main reason for using polygons was that I already had it implemented for something else.
Skeiks wrote:Does that help any?
It helped a little; I'm thinking of how to implement movement in a platformer and I'm probably really overthinking it.

But my main reason for this post was that I just became curious if there's a known solution for polygon "sliding" with gravity, since physics engines use polygons and don't seem to suffer from this.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 3 guests