Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Collision detection: two identical cases - how to solve?  (Read 2422 times)

0 Members and 1 Guest are viewing this topic.

bolbozaur

  • Newbie
  • *
  • Posts: 4
    • View Profile
Collision detection: two identical cases - how to solve?
« on: November 23, 2019, 07:21:50 pm »
I am looking for a conceptual solution to my problem. It's a simple platformer-alike game where player can move horizontally during free-fall.

Consider those two cases:


In the first case, from game experience point of view, the player should land on top of the box; and in the other case he hit the left edge, hence the player should fall down.

However, from my code point of view ("real behaviour"), both those collision detection cases are identical. I am not sure how to separate them.

In both cases the vertical velocity is positive (falling down) and the user is moving with some fixed positive horizontal velocity. (moving right)

From a collision-standpoint the two cases are identical, I think. How can I tell whether I should put the player on top of it or let him fall?
« Last Edit: November 23, 2019, 07:23:26 pm by bolbozaur »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Collision detection: two identical cases - how to solve?
« Reply #1 on: November 24, 2019, 09:07:57 am »
They are not identical. In the first case, the player "enters" the collision body by the top edge, while on the second, it enters from the left edge. So you should not detect "static" collisions (ie. just checking which shape is in another at every frame), but do this more dynamically, by finding the entry point of the player's trajectory inside the collided shape. This can be as easy as computing the intersection point between a segment (current_player_pos - previous_player_pos) and the box.

I've never written such collision detection, so I won't give you more details, and of course I may be wrong ;) but I'm pretty sure that's the way to go.
Laurent Gomila - SFML developer

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: Collision detection: two identical cases - how to solve?
« Reply #2 on: November 24, 2019, 11:02:12 pm »
This can be solved by calculating a 'collision manifold', which describes from which direction a collision occurs. I wrote a short SFML specific article about it here. There are a few more resources linked at the bottom too.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Collision detection: two identical cases - how to solve?
« Reply #3 on: December 01, 2019, 11:20:36 pm »
Although the solutions above are more comprehensive, a less-comprehensive but possibly more simple approach - since the collisions are all axis-aligned rectangles, you could just compare the differences between the vertical edges and between the horizontal edges to see which has 'entered' the most.

By vertical edges, I mean the length between the right edge of the player and the left edge of the platform, and by horizontal edges, I mean the length between the bottom edge of the player and the top edge of the platform. If horizontal length is greater, place on top, otherwise fall.
Chances are, the collision detection code will probably be comparing these edges already so it's just a matter of storing the lengths and then you can work with those.
Of course, you'll need to use different edges for different corners.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Collision detection: two identical cases - how to solve?
« Reply #4 on: December 02, 2019, 06:34:44 am »
Quote
since the collisions are all axis-aligned rectangles, you could just compare the differences between the vertical edges and between the horizontal edges to see which has 'entered' the most
I can imagine situations where this doesn't work, especially when player collides near the corner with a high velocity.
Laurent Gomila - SFML developer

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Collision detection: two identical cases - how to solve?
« Reply #5 on: December 02, 2019, 09:43:38 am »
Absolutely.

This would be for times when you can be sure that such high velocities would not be present (or a small enough time step used to provide required cover).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything