0

I'm creating a small game in Unity with C#, I'm not allowed to use any of the built in physics or colliders. What I have is a platform with a weight object on it. With the arrow keys you can tilt the platforms angle. The weight on top should then react accordingly to the given tilt angle. I have calculated everything with the friction, max friction, normal force a.s.o. The thing that doesn't work is when you press a key to change the tilt, the force on the object should change accordingly, and if the force on the weight is greater that the maximum friction force, then the weight should move. Here is a simplified example of my code.

public float m = 35;

public float µ = 0.15f;

public float g = 9.82f; //(N/kg)

public float N;

public double deg = 0;

Fmax = m * g * µ;

if (Input.GetKeyDown(KeyCode.UpArrow)) {
   deg++;
   N = m * g * µ * (Math.Cos(deg) * (180.0 / Math.PI));
   platform.transform.Rotate(0, 0, deg, Space.Self);

} else if (Input.GetKeyDown(KeyCode.DownArrow)) {
   deg--;
   N = m * g * µ * (Math.Cos(deg) * (180.0 / Math.PI));
   platform.transform.Rotate(0, 0, deg, Space.Self);
}

if(N > Fmax) {
 // Do the rest of the code
}

I think that the problem has to do with that Math.Cos(deg) turns the deg into a radian, but I tried to then covert back the reg to a deg with: (180.0 / Math.PI). I can get everything to work on my calculator but not in the code.

Krullmizter
  • 529
  • 8
  • 28
  • 5
    Do you mean rad(ians) to deg(rees)? Unsure what `reg` is. If you do mean radians then Unity has a conversion for that build in in the `Mathf` libary. `Mathf.Rad2Deg` and `Mathf.Deg2Rad` https://docs.unity3d.com/ScriptReference/Mathf.Rad2Deg.html https://docs.unity3d.com/ScriptReference/Mathf.Deg2Rad.html – Remy Dec 17 '19 at 11:39
  • Yes sorry I meant radians to degrees – Krullmizter Dec 17 '19 at 11:41
  • 2
    You need `Math.Cos(deg * Math.PI / 180)`. Math.Cos takes its argument in radians. – Andrew Morton Dec 17 '19 at 11:47
  • I got it working with @AndrewMorton solution but now I get a convert error when I want to do this: ```N = m * g * µ * ((Math.Cos(deg * Math.PI / 180)));``` should I cast it? – Krullmizter Dec 17 '19 at 12:24
  • 1
    I suggest using only `double` for floating-point numbers. The only thing you will get from `float` is problems in this case. Ref: [Float vs Double Performance](https://stackoverflow.com/a/417591/1115360). – Andrew Morton Dec 17 '19 at 12:36
  • 3
    @Krullmizter it is exactly the same as already suggested by Remy_rm : `Mathf.Cos(deg * Mathf.Deg2Rad)` since it returns exactly the same as `Math.PI / 180` does. Note that there is a difference between the general c# library `Math` and Unity built-in `Mathf`! – derHugo Dec 17 '19 at 12:39
  • Cosine value ranges from -1 to +1. The conversion has to happen so the argument you pass to the function is in radians. It would go inside the function argument, not multiplying the cosine value. – duffymo Dec 17 '19 at 15:06
  • Unsolicited advice: I'd avoid namimg a variable `µ`. There's potential for that to cause issues. It's also just more difficult to type. I'd go with `mu` or `u`. – 3Dave Dec 17 '19 at 15:13

3 Answers3

3

This is incorrect:

   N = m * g * µ * (Math.Cos(deg) * (180.0 / Math.PI));

If the angle is in degrees you have to convert it to radians and pass that the cosine function:

double rad = deg*Math.PI/180.0;
N = m*g*mu*Math.Cos(rad);
duffymo
  • 305,152
  • 44
  • 369
  • 561
1

You can easily convert from radians to degrees and from degrees to radians using the Mathf.Deg2Rad and Mathf.Rad2Deg constants

using UnityEngine;

public class Example : MonoBehaviour
{
    // convert 1 radian to degrees

    float rad = 10.0f;

    void Start()
    {
        float deg = rad * Mathf.Rad2Deg;
        Debug.Log(rad + " radians are equal to " + deg + " degrees.");
    }
}
Windgate
  • 365
  • 1
  • 4
  • 14
-1

There is a super handy function called Mathf.Rad2Deg and Mathf.Deg2Rad. They convert radians to degrees and degrees to radians.

ninety-ninehundred
  • 126
  • 1
  • 3
  • 8