- Home /
Check if the normal of a collision is within a range of angles
Vector3 collisionDifference = collision.normal * Vector3.Dot(playerVelocity, collision.normal);
playerVelocity -= collisionDifference;
I need this line (the law of equal opposite reaction implemented for a FPS game) to only run whilst the collision.normal is within a certain range. In effect, the player is sliding up / down slopes in weird ways. The first way this manifests is that even with a very very low velocity magnitude, the player will eventually begin to slide down slopes, even at angles of only a few degrees.
So what I need to do is treat certain collisions (within a certain angle), as if they were flat ground, and for that, I need to figure out how to evaluate the normal of a collision, and whether or not the slope of the floor / ramp the player is on is steep enough to constitute sliding upon.
How, then, do I translate the normal of a collision into an angle in degrees or radians relative to flat ground? If the player is standing on a 45 degree angle slope, how would I go about checking that angle?
Answer by xthunderduckx · Oct 01, 2021 at 10:41 PM
Fixed it, way easier than I thought it would be.
currentSlope = Vector3.Angle(collision.normal, Vector3.up);
Now I just only apply the changes if the currentSlope is within a certain range.
yep, that too.
Angle() is implemented w/ dot-product() and normalize(). Well, and arccos().
you can take the dot-product approach with you anywhere.
I haven't actually seen the source code for Angle() but I will bet money it's:
Angle(v3f A, v3f B) {
A.normalize();
B.normalize();
return Math.acos(v3f.Dot(A, B));
}
Answer by elenzil · Sep 30, 2021 at 06:43 PM
you can compare the direction of two vectors by using dot product. so you've got your normal and some other vector indicating the 'good' direction, say. be sure to normalize them both. then take the dot product of them. if the dot is equal to 1, they're parallel. if the dot is equal to 0, they're perpendicular. if the dot is equal to -1, they're anti-parallel. if the dot is greater that .707, they're within 45 degrees of each other, and so on. https://chortle.ccsu.edu/vectorlessons/vch09/vch09_6.html
Your answer
Follow this Question
Related Questions
3D enemies keep sliding on the terrain 1 Answer
Better isGrounded Solution / Detect angle of ramp 1 Answer
Character Controller continues moving upwards on slopes after horizontal movement has stopped 1 Answer
Two objects on the same Y level bumping the player 1 Answer
Performance Physics.Checkbox/CheckSphere vs Collider/Trigger 1 Answer