-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAirFoil.cs
71 lines (62 loc) · 2.95 KB
/
AirFoil.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AirFoil : MonoBehaviour
{
Rigidbody body;
public BoxCollider shapeReferenceCollider = null;
//http://www.aerospaceweb.org/question/airfoils/q0150b.shtml
public AnimationCurve liftCoefficient;
public AnimationCurve dragCoefficient;
public AnimationCurve aerodynamicCenter;
public float chordLength = 1;
public float sectionLength = 1;
public float damping = 0.005f;
public float maxSpeed = 15f;
[ReadOnly, SerializeField] private float airDensity = 0;
[ReadOnly,SerializeField] private float angleOfAttack = 0;
[ReadOnly, SerializeField] private float planarFlowSpeed = 0;
[ReadOnly, SerializeField] private float lift = 0;
[ReadOnly, SerializeField] private float drag = 0;
[ReadOnly] public Vector3 force;
private Vector3 oldForce = Vector3.zero;
private void Start()
{
OnValidate();
body = GetComponentInParent<Rigidbody>();
}
private void OnDrawGizmosSelected()
{
OnValidate();
}
private void OnValidate()
{
if (shapeReferenceCollider)
{
chordLength = shapeReferenceCollider.size.z * shapeReferenceCollider.transform.localScale.z;
sectionLength = shapeReferenceCollider.size.x * shapeReferenceCollider.transform.localScale.x;
}
}
private void FixedUpdate()
{
Vector3 foilVelocity = body.GetPointVelocity(transform.position);
Vector3 airVelocity = Vector3.zero;
Vector3 localFlow = transform.InverseTransformDirection(airVelocity - foilVelocity);
localFlow.x = 0;
planarFlowSpeed = localFlow.magnitude;
float planarFlowSpeed1 = Mathf.Clamp(planarFlowSpeed, 0, maxSpeed);
angleOfAttack = planarFlowSpeed > 0 ? -Mathf.Atan2(-localFlow.y, -localFlow.z) * Mathf.Rad2Deg : 0;
airDensity = AirDensitySetting.getDensity(transform.position.y);//1.225f
//float coeff1 = 0.5f * airDensity * planarFlowSpeed * planarFlowSpeed * chordLength*sectionLength;
float coeff2 = 0.5f * airDensity * planarFlowSpeed1 * planarFlowSpeed1 * chordLength * sectionLength;
drag = dragCoefficient.Evaluate(angleOfAttack) * coeff2;
lift = liftCoefficient.Evaluate(angleOfAttack) * coeff2;
Vector3 localForce = Quaternion.Euler(angleOfAttack,0,0)*new Vector3(0, lift, -drag);
force = transform.TransformDirection(localForce);
force = oldForce = Vector3.Lerp(oldForce, force,Time.fixedDeltaTime/(damping+Time.fixedDeltaTime));
body.AddForceAtPosition(force, transform.position);
Debug.DrawRay(transform.position, transform.TransformDirection(localFlow).normalized, Color.blue);
Debug.DrawRay(transform.position, transform.TransformDirection(localForce), Color.green);
Debug.DrawRay(body.position, body.velocity.normalized, Color.red);
}
}