-
Notifications
You must be signed in to change notification settings - Fork 2
/
CalculateForcesScript.cs
81 lines (63 loc) · 2.68 KB
/
CalculateForcesScript.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//Calculates the forces involved in the ramp scene
//The Physics engine does not guarantee exact physics.
public class CalculateForcesScript : MonoBehaviour {
public Transform mass; //the sliding mass for accessing the friction coefficient and mass
public Transform ramp; // the ramp accessing the angle and the friction coefficient
private float weight; //The three forces that are calculated in the script
private float reaction;
private float friction;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
weight = CalculateWeight(mass.GetComponent<Rigidbody>().mass);
reaction = CalculateReaction(weight);
friction = CalculateFriction(reaction);
}
private float CalculateWeight(float mass)
{
return mass * Physics.gravity.magnitude; //changing the strength of gravity will automatically change weight
}
private float CalculateReaction(float weight)
{
Quaternion boxRot = mass.rotation;
float angle = Quaternion.Angle(boxRot, Quaternion.identity); //the box rotation in radians - the box can only rotate around the x axis (fixed in others) so no other rotations possible
float reaction = weight * Mathf.Cos(angle*Mathf.Deg2Rad);
return reaction;
}
//Currently, only a single friction value is used, the static friction one.
//IMPLEMENT a changing friction value later.
private float CalculateFriction(float reaction)
{
//Instead of only getting the ramp friction, have adapted this in order to get the friction of whatever the mass
// is colliding with.
//float staticFriction = ramp.GetComponent<BoxCollider>().material.staticFriction;
//float dynamicFriction = ramp.GetComponent<BoxCollider>().material.dynamicFriction;
float friction_coeff = mass.GetComponent<CollisionFrictionScript>().GetFrictionCoefficient();
return friction_coeff * reaction;
}
//Getters for the three force variables.
public float GetWeight()
{
return weight;
}
public float GetReaction()
{
return reaction;
}
public float GetFriction()
{
return friction;
}
public float GetParallelForce()
{
Quaternion boxRot = mass.rotation;
float angle = Quaternion.Angle(boxRot, Quaternion.identity); //the box rotation in radians - the box can only rotate around the x axis (fixed in others) so no other rotations possible
float parallel = weight * Mathf.Sin(angle * Mathf.Deg2Rad);
return parallel;
}
}