Search Unity

Bug Wrong rigidbody reaction at AddForceAtPosition to the pendulum above rotation axis

Discussion in 'Physics' started by Gulliver, Nov 29, 2023.

  1. Gulliver

    Gulliver

    Joined:
    Jan 8, 2013
    Posts:
    101
    Hi.
    Everyone can replicate it. Just create the pendulum with HingeJoint axis at 0.2-0.3 height and apply force above the rotation axis. The pendulum will swing to the opposite (!) direction of the force vector. Just as if you apply force below the rotation axis.
    Is it a bug or feature?
     
  2. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    602
    Whether it is a feature is up to you decide.

    If you are asking about bug in Unity. It is very unlikely that there is a such bug in commonly used physics engine functionality like adding force.

    It is much more likely that you have a bug in your code which calculates the force or it's application position. Don't expect people to tell exact location of bug in code they haven't even seen.

    So start debugging. Add some debug messages, verify that the vectors calculated from shooting have the values and direction you expect them to have. If the values are not what you expected, look further in the code producing them. If the values are what you expect, go carefully (very carefully) read the documentation for functions you are calling to apply the force. Maybe you misunderstood what the vectors you are passing are supposed to represent or in what coordinates system they need to be.

    Vectors are just like plain floats, they can represent all kind of different things, and nothing (except being you being careful) prevents from assigning wrong value in the wrong place. Even if they represent the same concept, it might be in a different forms which need conversion.


    Remember that you don't fix problems by just staring at code (or someone else starting at your code). Debugging is a systematic and interactive process.

    * You split the code into steps
    * have theory of what is supposed to happen and what the intermediate values should be at each step
    * verify that the code is doing the thing you expect it do at corresponding step (add some debug logs, use a debugger, use debug drawing, use some kind of other API to output the values)
    * if the intermediate values are wrong, go to the code producing them and repeat the process, if values are correct move down
    * if you have a code doing a bunch of stuff on single line, split it into multiple lines/steps
    * once you have narrowed it down now it's time to stare at code a little bit, if there is some external API involved double check the documentation for corresponding API, test your assumptions about how the API behaves or you think the API behaves

    One more suggestion while debugging keep the setup as simple as possible. Disable any unrelated (or what you think is unrelated) systems. Make it easier to repeat the problem. For example in your video it seems like you have some kind of stamina/taking a breath/aiming accuracy system. Not only it increases the time you need to repeat the problem while debugging, it also introduces uncertainty as you can't be sure whether you have bug in code dealing with bullet hit, whether you have bug in aiming accuracy system, or maybe you are just missing the shots.
    If it turns out that disabling one those "unrelated things" makes the problem disappear maybe the problem was in completely different place from what you thought it is.