playMaker

Author Topic: Directional Collision Detection  (Read 1824 times)

MadboyJames

  • Playmaker Newbie
  • *
  • Posts: 18
  • Str: 9/ Dex:11/ Con:10/ Int:13/ Wis: 12/ Cha: 12
Directional Collision Detection
« on: January 03, 2018, 01:47:27 PM »
Hi, I'm trying to figure out how to make my character do a "wall push" animation if they are being moved right up against a wall. I am using "Collision 2D Event" and "Get Collision 2D Info" in order to figure out when the player is colliding with a wall. The only problem here is it's not quite working. I want to know when the player has a collision on the left or right side (X:-1 or X:1, respectively). In "Contact Normal" I can get what side of the terrain the player has hit, but not what side of the player is being hit. I cannot use the information from the terrain contact points to extrapolate the side of the player because the terrain contact points do not update in the manner I need. essentially, if I am colliding with the top of the terrain, and the left side, then the "contact normal" will be X:0 Y:1. If I then jump and collide with with left side, and only the left side, then the "contact normal" will be X:1 Y:0. When I fall back down to the ground, it will be X:1 Y:0 until I release the "move left" key. When I do this (without any change in position) the "contact normal" becomes X:0 Y:1.

Is there a better way to detect collisions or do what I want to do in a different manner? Any help would be appreciated.
Inexperience is just the beginning of greatness.

MadboyJames

  • Playmaker Newbie
  • *
  • Posts: 18
  • Str: 9/ Dex:11/ Con:10/ Int:13/ Wis: 12/ Cha: 12
Re: Directional Collision Detection
« Reply #1 on: January 04, 2018, 08:04:51 AM »
Bump.
Inexperience is just the beginning of greatness.

Doh

  • Full Member
  • ***
  • Posts: 124
Re: Directional Collision Detection
« Reply #2 on: January 04, 2018, 12:39:39 PM »
If I was attempting this I'd use fairly short length raycasts. I'd cast out left and right to detect obstacles, the quantity and placement would depend on the complexity and variance of the geometry the player will come into contact with. You may also be able to do the same thing with triggers (a left and a right trigger).

I'd also suggest that you consider using a layer mask and assign wall gameobjects to the specified layer, this should prevent the character siding up against unintended objects.

MadboyJames

  • Playmaker Newbie
  • *
  • Posts: 18
  • Str: 9/ Dex:11/ Con:10/ Int:13/ Wis: 12/ Cha: 12
Re: Directional Collision Detection
« Reply #3 on: January 04, 2018, 10:30:13 PM »
Gotcha. I didn't consider the layer bit, or the raycasts. Thank you!

Are the raycasts worth it (resource wise) for this task, or would I be better dropping the whole mechanic? The game is not going to be super flashy all-in-all, so I think a little expenditure for this little bit of polish should be alright, but what's your perception?
Inexperience is just the beginning of greatness.

Doh

  • Full Member
  • ***
  • Posts: 124
Re: Directional Collision Detection
« Reply #4 on: January 16, 2018, 07:05:23 AM »
Sorry I haven't replied sooner, I've been away for a week and a bit.

I use a variety of raycasts in my current project. I had 5 constantly checking for the distance to the ground (though I recently swapped it for a spherecast) and I hadn't noticed any negative performance issues. I also have various other rays cast on isolated occasions.

It really comes down to the performance budget of your individual circumstance.

Try to limit the distance you cast.
In some uses you may be able to limit how often the rays are cast.
Lastly try an exaggerated test. Create a dummy object with an FSM set to raycast constantly and duplicate it or the action many times. If your findings make you think your most complex scenario cannot be handled then give triggers a shot.

If it were me, I'd guess that this will be worth it and I suspect the performance impact will be minimal. This is a method I've come across in a good few character controllers.