- Home /
Cancel the torque resulting from a collision (found whats wrong, don't know how to fix it)
Hello
------I'm currently designing a RigidBody controller from scratch and need to handle collisions differently based on where they hit my object. Specficially, i need to be able to cancel the torque that results from collisions on specific parts of my character. I've figured i could try and do this by calculating torque manually and adding it back into the character for every onCollisionEnter and onCollisionStay, with the impulse and point members of the Collision class, and while this does make a massive difference in the ammount of torque my character experiences from the collisions in question, its not perfect, and sometimes it will even create a nonzero cancel torque on a collision that doesn't contribute any torque (examples later in text)
private void antiTorque(Collision collision)
{
Vector3 counterTorque; // will be used to cancel the torque resulting from the collision
Vector3 r; // will represent the vector that represents the colision point relative to the players center of mass
for (int i = 0; i < collision.contacts.Length; i++)
{
r = collision.GetContact(i).point - player.transform.position;
if (Vector3.Dot(r, player.transform.Up) < .7f)
{
groundedBufferCurrent = .1f;
counterTorque = Vector3.Cross(collision.impulse, (collision.GetContact(i).point - player.transform.position)); // calculates the negative of the torque resultant from the collision
counterTorque -= Vector3.Dot(player.transform.up, counterTorque) * player.transform.up; // negates any torque that would cause the player to turn left/right
player.AddTorque(counterTorque, ForceMode.Impulse);
}
}
}
---------This is roughly the code i originally started working with, i have since added a decent amount to it , but the problem started here, specifically, the r vector calculation appears to be off, because even when i'm on completely flat ground and standing straight up (which would mean r should be (0,-1,0)) r will debug.log output something like (0,-1,0.1459) if i'm walking in the z direction, or (-0.0925,-1,0) if i'm walking in the negative x direction, or some combination of them if i'm walking in a diagonal direction. And when i jump, it will return things like (0,-1.165,0) which shouldn't be possible because the lowest point on my collider should be at most 1 unit beneath my players center of mass.
---------Like i said earlier, this code mostly works great, i originally wrote it so that my character wont lean when trying to walk up slanted surfaces, and it solves that almost perfectly. Likewise, (before i wrote my stepUp function) if my character hit a step without my antiTorque, my character would lean jerk sharply in the direction the step impedes, and with this antiTorque function, he still lean jerks in the same direction, but ALOT less.
---------And after investigation and finding the r vector to be off, this all makes sense. For example, with the standing straight up and moving without any friction example, the collision itself should contribute a zero torque, r X F = (0,-1,0) X (0,F,0) = (0,0,0) for all possible force magnitudes F, but if r is off by any ammount in the x or z direction, then my code will calculate a nonzero antiTorque and cause my player to lean.
--------- So my question is this, does anyone here know a decent way to get the r vector from the collision class without calculating it manually? I've also tried using the transform.InverseTransformPoint() function to calculate the r vector but it had the exact same error, if not, does anyone know of a way to work around it?