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

Author Topic: Direction of collision  (Read 2978 times)

0 Members and 1 Guest are viewing this topic.

FellowCoder

  • Newbie
  • *
  • Posts: 8
    • View Profile
Direction of collision
« on: January 17, 2020, 08:02:44 am »
So, in my project i used "Sprite.getGlobalBounds().intersects(sprite2.getGlobalBounds())" to see if the player was collinding with the floor, beeing able to jump again or not, but if it collides with the wall it will be able to jump again, so how can i make it to be able to identify the direction of the collisions?


Also i am not using perfect squares as walls or floors in this so i cant just subtract the position to know the direction...

Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: Direction of collision
« Reply #1 on: January 18, 2020, 10:31:51 am »
The direction you can get from your movement vector (You can compute the angle of the movement vector which would give you the direction). However, this is likely not failsafe.

You need a way of finding out if your sprite2 (or Sprite) is the wall or the floor. Then you know for sure if it was the floor or a wall.

If I understand you correctly, you are using a single sprite which serves as a wall and moves into the floor as well, correct? In that case, using the direction still isn't failsafe. You could either fall onto the wall with a pretty steep angle, but still be hitting the wall, or you could touch the floor with a pretty flat angle, but still touch the floor.

I would suggest maybe using the height of the player as a metric, let's say the player is standing on the floor. The difference of the y coordinate and the bottom of the floor is maybe 100px. If you detect a collision, check the height difference of the player. If it's < 110px you hit the floor, else the wall.

Another option would be separating the sprite from multiple bounding boxes, or splitting the sprite into two separate ones, so that they still look connected in-game, but are in reality two separate sprites.

That's my thought process without seeing any actual code.
« Last Edit: January 18, 2020, 10:33:42 am by Raincode »

FellowCoder

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Direction of collision
« Reply #2 on: January 20, 2020, 02:14:01 am »
This method would work if the map was just a rectangle with nothing inside, but wouldn´t work in a platform game like Mario

The ideia I have is to get the first y pixels of every rectangle and the variable isAbleToJump will be true once the player collides with this pixels... so when the player collides in the side of a rectangle isAbleToJump will continue beeing false( isAbleToJump will be false after the player jumps)

But I still think that this can be made easier, do you have any other idea?


fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: Direction of collision
« Reply #3 on: January 20, 2020, 11:01:18 am »
You need to calculate what's called a 'collision manifold', which not only tells you the direction of the collision but also the amount of overlap. This can be done relatively easily with circles and AABBs (see here) - for non-square shapes SAT (separating axis theorem) is a common approach. Once you have your manifold how you react to the collision is up to you :)