Search Unity

Adding vectors and using angles (Coulomb force)

Discussion in 'Physics' started by Bogomip, May 19, 2019.

  1. Bogomip

    Bogomip

    Joined:
    Mar 28, 2019
    Posts:
    2
    Hi all,

    So im trying to build a visual physics simulator that I intend to use at work to help some of my students understand how various forces interact within multi particle systems. I also want them to see how computational physics is done, and so want to code some physics into unity as opposed to use the pre-installed physics systems.

    So I have a bunch of Spheres on my 2D plane which are acting as my particles - they have charge and mass. Im calculating the Coulomb Force that each particle acts upon one another, which gives a Vector2, where x is the magnitude of the force, and y is the angle the force acts. This all works perfectly I think:

    force = (coulombConstant * particleCalculating.getCharge() * particleApplying.getCharge()) / (distance.magnitude * distance.magnitude);
    angle = Mathf.Atan(distance.x / distance.y);

    (except for some powers which I will sort out later, this works fine)

    Then, the idea is to sum all these forces together as such:

    foreach (Vector2 particle in forces)
    {
    netXForce += particle.x * Mathf.Sin(particle.y);
    netYForce += particle.x * Mathf.Cos(particle.y);
    }

    Then I apply this to get an acceleration, and generate a velocity and move the particle.

    Which should all work... the problem is the unity angle system (or my use of the unity angle system, whichever you prefer :)). I cant get it to differentiate from 0.5 rad to the bottom left and 0.5 rad to the top right (for example). This means two particles repelling one another actually add to each others acceleration a they are trying to go in opposite directions.

    Any help here would be great - even if its just a description of why the unity angle system is great :)
     
  2. Bogomip

    Bogomip

    Joined:
    Mar 28, 2019
    Posts:
    2
    Seems like instead of atan, I need atan2 :)

    Thanks anybody for listening and thinking!