# Math - Create plane from opposing normals

Discussion in 'Scripting' started by HiddenMonk, Jul 25, 2016.

1. ### HiddenMonk

Joined:
Dec 19, 2014
Posts:
987
First let me show you an image to work with.

So the goal is to find the red normal. The reason behind needing this is for character physics. When you collide into a wall, you get its normal, project your velocity onto it, and then continue moving. This allows you to slide against walls, but there is an issue - What happens when you end up with opposing normals? You are going to reach a point where you cannot move any more, but the character physics will not realize that you are in a spot where its basically a flat ground or flat wall even though the normals say its some angled normal. One way to handle this might be to have some code that checks if you didnt move after a few tries and then just zero out the velocity, but I would like to instead try and take these opposing normals and create a fixed normal that will allow our velocity to project itself onto it causing it to no longer be troubled by the opposing normals.
(Though, this idea might also have a flaw that can be seen in example 2. If our velocity was large enough, it would project onto the newly fixed red normal, and then continue moving towards the other slanted surface and start going up it. Im open for ideas to handle this physics issue =)).

My first thought was to use the average of the 2 normals, but that will not always work. In example 2, taking the average normal will work since the 2 normals are the same, but just flipped. However, example 1 demonstrates how the average normal is not what we want.

If anyone has any clue on how to go about doing this, I'd appreciate your help =)

Last edited: Jul 25, 2016
2. ### mgear

Joined:
Aug 3, 2010
Posts:
6,236
No idea.. but i think handmade hero had pretty similar topics while back

like,

and see the other videos related to collisions also

Joined:
Nov 24, 2010
Posts:
942
Wouldn't be easier to ray-cast in the direction of the projected velocity and stop if there's something in the way?

4. ### HiddenMonk

Joined:
Dec 19, 2014
Posts:
987
He seems to be dealing with a tile based method, but I only watched a little and skimmed through the rest since its 90 min long. Ill try and see his other videos as well.

The images show the surfaces touching at a point, but they dont have to be. In other words, theres no guarantee a cast will hit anything. I could maybe just detect if there are conflicting normals and set velocity to zero, but id prefer not to just go setting things to zero when I might not be completely sure if it was necessary. The goal is the help the character physics system do its thing.

I think the problem is how I am looking at this. I say that I dont want to use the average normal in the first example, but allow it in the second example. I am choosing the normal that I want depending on the situation such as a character walking into something versus falling into something. I think I am able to us any normal that is equal or between the negative projected velocity (purple lines). In the first example I chose to use the exact negative of the velocity projected on the up vector surface since it made the most sense in that situation. In other words, I think I need more info to decide what it is I want to happen, such as using my characters up vector and what not.
Or maybe something like this...
Code (CSharp):
1. Vector3.ProjectOnPlane(-Vector3.ProjectOnPlane(velocity, opposingNormalThatVelocityWillPenetrate), Vector3.Cross(opposingNormal1, opposingNormal2)).normalized;
Or better yet..
Code (CSharp):
1. Vector3.ProjectOnPlane(-velocity, Vector3.Cross(hitNormal, previousHitNormal)).normalized;

Last edited: Jul 26, 2016
unityunity