- Home /
how to view all forces on rigidbody
I am working on making my own very simple wheel-collider. However, this requires being able to judge the forces on each individual wheel collider.
The natural solution for me seems to be to create spherecolliders, then read the forces from their collisions with their surfaces they are in contact with to calculate friction and slippage.
After reading off col.impactForceSum, col.impulse, and even col.frictionForceSum, i'm getting zeroes across the board, despite gravity being enabled.
Is there a way to find the rest of the force that counteracts gravity in unity?
Answer by ThaHypnotoad · Dec 27, 2015 at 11:04 AM
Alright guys i figured this one out myself, or at least a workaround for it.
i was getting zeroes (or near zeroes) across the board for the collisions on different sphere colliders, despite gravity being in effect. Unfortunately with, unity's gravity, as soon as the rigidbody stopped moving, the OnCollisionStay method stopped being called, and the last value i could get out of the Collision passed to it was zeroes across the board.
The workaround for this was to make a "fake" gravity script:
using UnityEngine;
using System.Collections;
public class fakeGravity : MonoBehaviour {
public float G;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
GetComponent<Rigidbody>().AddForce(-transform.up * G, ForceMode.Acceleration);
}
}
Note: it is important that this is in a FixedUpdate function otherwise gravity could be applied twice once in a while making the physics engine resolve it with an impulse twice as high.
I've yet to isolate each wheel, as the way children are handled, i cant put the script on each individual collider as children cant have rigidbodies, however, I was able to prove to myself that it was working with this script and offsetting the positions of the wheels and checking if the series of collisions had different impulses. Sure enough, as I moved one pair closer to the CG, i saw one value grow while the other fell:
using UnityEngine;
using System.Collections;
public class customWheelCollider : MonoBehaviour {
Collision currentCollision;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
}
void OnCollisionEnter (Collision col){
currentCollision = col;
}
void OnCollisionStay(Collision col){
currentCollision = col;
print(currentCollision.impulse);
}
void OnCollisionExit(Collision col)
{
currentCollision = null;
}
}
Answer by Bunny83 · Dec 26, 2015 at 12:00 PM
Forces aren't really stored in the rigidbody. They are simply accumulated into one resulting force / torque and directly applied to velocity / angularVelocity. What you want to do is to split the velocity into seperate axis and analyse and adjust each axis seperately. When done you just recombine them. You can use Vector3.Project to project the velocity vector onto your desired axis. The important slip axis would be the rotation axis of your wheel.
Another way besides using Vector3.Project is to use Transform.InverseTransformDirection of the transform object of your wheel. That way the velocity vector is already seperated into it's local axis.
Unfortunately you are describing has absolutely nothing to do with my original question.
The issue is that friction is a function of force and the fricion coefficient. $$anonymous$$nowing the velocity vector of the vehicle isn't particularly useful for calculating slip and friction force because other than simulating the flexing of rubber through a slip based friction curve, the force made by the tire remains the same no matter how much it is actually slipping.
The weight on each individual wheel however, does affect the force friction puts on each of the wheels, so if, for example, the majority of the weight is on one set of wheels, they should provide the most resistance while slipping, hence the need to measure forces.
Of course it is related to your question. As i already said Unity doesn't really work with forces. Rigidbody systems work with velocities. The physics system derives the impact force from the velocity. Those impact forces are directly applied to the velocity. The first derivative of the velocity represents the sum of all forces applied during the last frame.
Collision.impulse
is just the impulse force the physics system applied to the velocity to resolve the collision. $$anonymous$$eep in $$anonymous$$d that forces which aren't applied to the center of mass results in both: a change of the linear velocity as well as a change of the linear angularVelocity. That's how rigidbody systems work in general. Again, Unity doesn't really calculate some kind of net-force, just how a force affects the velocity.
The physics system also has a lot fake counter forces like "drag". drag has no relation to real world drag. It's just a coefficient which is applied each frame. For information about how this works, see my answer over here and here.
Your answer
Follow this Question
Related Questions
Exported Scene in .swf loses 2D colliders ? 1 Answer
Getting "Internal error: Too many pairs created" without any apparent reason 0 Answers
2D Collision not working! (child sprites colliders) 1 Answer
How can i create a character like dragon hill game, Digging underground, what collider should i use? 0 Answers