Skip to content

Commit

Permalink
SimpleRotors
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Miiller committed Oct 25, 2020
1 parent 802df3d commit 24dc65a
Show file tree
Hide file tree
Showing 47 changed files with 6,618 additions and 148 deletions.
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Assets/SimpleWings/Demo/Global.asset
Binary file not shown.
8 changes: 8 additions & 0 deletions Assets/SimpleWings/Demo/Global.asset.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified Assets/SimpleWings/Demo/Scenes/Heli.unity
Binary file not shown.
Binary file not shown.
8 changes: 8 additions & 0 deletions Assets/SimpleWings/Demo/Scenes/LIghtingSettings.lighting.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions Assets/SimpleWings/Demo/Scenes/Plane.meta

This file was deleted.

Binary file not shown.
9 changes: 0 additions & 9 deletions Assets/SimpleWings/Demo/Scenes/Plane/LightingData.asset.meta

This file was deleted.

Binary file not shown.
69 changes: 0 additions & 69 deletions Assets/SimpleWings/Demo/Scenes/Plane/ReflectionProbe-0.exr.meta

This file was deleted.

Binary file added Assets/SimpleWings/Demo/Scenes/Test.unity
Binary file not shown.
7 changes: 7 additions & 0 deletions Assets/SimpleWings/Demo/Scenes/Test.unity.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Assets/SimpleWings/Demo/Scripts/AircraftBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using UnityEngine;

public abstract class AircraftBase : MonoBehaviour
{
public abstract Rigidbody Rigidbody { get; internal set; }
}
11 changes: 11 additions & 0 deletions Assets/SimpleWings/Demo/Scripts/AircraftBase.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Assets/SimpleWings/Demo/Scripts/Airplane.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using UnityEngine;
using System;

public class Airplane : MonoBehaviour
public class Airplane : AircraftBase
{
public ControlSurface elevator;
public ControlSurface aileronLeft;
Expand All @@ -16,7 +16,7 @@ public class Airplane : MonoBehaviour

public WeaponDropper[] weapons;

public Rigidbody Rigidbody { get; internal set; }
public override Rigidbody Rigidbody { get; internal set; }

private float throttle = 1.0f;
private bool yawDefined = false;
Expand Down
10 changes: 10 additions & 0 deletions Assets/SimpleWings/Demo/Scripts/CenterOfGravity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using UnityEngine;

public class CenterOfGravity : MonoBehaviour
{
void Start()
{
var rb = GetComponentInParent<Rigidbody>();
rb.centerOfMass = rb.transform.InverseTransformPoint(transform.position);
}
}
11 changes: 11 additions & 0 deletions Assets/SimpleWings/Demo/Scripts/CenterOfGravity.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Assets/SimpleWings/Demo/Scripts/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ private void FixedUpdate()
{
if (rigid != null)
{
//rigid.AddRelativeForce(Vector3.forward * 20000f);
rigid.AddRelativeForce(Vector3.forward * thrust * throttle, ForceMode.Force);
}
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/SimpleWings/Demo/Scripts/HeadsUpDisplay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public class HeadsUpDisplay : MonoBehaviour
{
public Airplane plane;
public AircraftBase plane;

public Image fpm;
public Image cross;
Expand Down
109 changes: 109 additions & 0 deletions Assets/SimpleWings/Demo/Scripts/Helicopter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//
// Copyright (c) Brian Hernandez. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
//

using UnityEngine;
using System;

public class Helicopter : AircraftBase
{
public bool EngineOn = false;

public WeaponDropper[] weapons;

public override Rigidbody Rigidbody { get; internal set; }

private Rigidbody _hub;
private HingeJoint _hinge;

public int Rpm
{
get
{
var angVel = _hub.transform.InverseTransformVector(_hub.angularVelocity);
return Mathf.Abs(Mathf.FloorToInt(angVel.y * 9.5493f));
}
}

private float _collective = 0f;
public float Collective => _collective;

private void Awake()
{
Rigidbody = GetComponent<Rigidbody>();
_hinge = GetComponent<HingeJoint>();
_hub = _hinge.connectedBody;
}

private void Start()
{
try
{
Input.GetAxis("Yaw");
}
catch (ArgumentException e)
{
Debug.LogWarning(e);
Debug.LogWarning(name + ": \"Yaw\" axis not defined in Input Manager. Tail rotor will not work correctly!");
}
}

void Update()
{
if(Input.GetButtonDown("Jump"))
{
EngineOn = !EngineOn;
}

_collective = (Input.GetAxis("Collective") + 1f) / 2f;

if (weapons.Length > 0)
{
if (Input.GetButtonDown("Fire3"))
{
foreach (WeaponDropper dropper in weapons)
{
dropper.Fire(Rigidbody.GetPointVelocity(dropper.transform.position));
}
}
}

_hinge.useMotor = EngineOn;
}

private float CalculatePitchG()
{
// Angular velocity is in radians per second.
Vector3 localVelocity = transform.InverseTransformDirection(Rigidbody.velocity);
Vector3 localAngularVel = transform.InverseTransformDirection(Rigidbody.angularVelocity);

// Local pitch velocity (X) is positive when pitching down.

// Radius of turn = velocity / angular velocity
float radius = (Mathf.Approximately(localAngularVel.x, 0.0f)) ? float.MaxValue : localVelocity.z / localAngularVel.x;

// The radius of the turn will be negative when in a pitching down turn.

// Force is mass * radius * angular velocity^2
float verticalForce = (Mathf.Approximately(radius, 0.0f)) ? 0.0f : (localVelocity.z * localVelocity.z) / radius;

// Express in G (Always relative to Earth G)
float verticalG = verticalForce / -9.81f;

// Add the planet's gravity in. When the up is facing directly up, then the full
// force of gravity will be felt in the vertical.
verticalG += transform.up.y * (Physics.gravity.y / -9.81f);

return verticalG;
}

private void OnGUI()
{
const float msToKnots = 1.94384f;
GUI.Label(new Rect(10, 40, 300, 20), string.Format("Speed: {0:0.0} knots", Rigidbody.velocity.magnitude * msToKnots));
GUI.Label(new Rect(10, 60, 300, 20), string.Format("Collective: {0:0.0}%", _collective * 100.0f));
GUI.Label(new Rect(10, 80, 300, 20), string.Format("G Load: {0:0.0} G", CalculatePitchG()));
GUI.Label(new Rect(10, 100, 300, 20), string.Format("Rotor RPM: {0}", Rpm));
}
}
12 changes: 12 additions & 0 deletions Assets/SimpleWings/Demo/Scripts/Helicopter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 66 additions & 0 deletions Assets/SimpleWings/Demo/Scripts/RotorAssembly.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class RotorAssembly : MonoBehaviour
{
public Helicopter Heli;
public float MaxAngularVelocity = 400f;
public float BladeLength = 5;
public float MaxBladePitch = 10f;
public float MinBladePitch = -5f;
public float CyclicMaxPitch = 5f;

private float _collective;
private float _rollInput;
private float _pitchInput;

private Rigidbody _rb;
private Quaternion[] _initialRotations;
private FixedJoint[] _rotors;

private void Start()
{
_rb = this.GetComponent<Rigidbody>();
_rb.maxAngularVelocity = MaxAngularVelocity;

_rotors = GetComponents<FixedJoint>();
_initialRotations = new Quaternion[_rotors.Length];

for(int i=0; i<_rotors.Length; i++)
{
_rotors[i].connectedBody.maxAngularVelocity = MaxAngularVelocity;
_initialRotations[i] = _rotors[i].connectedBody.transform.localRotation;
}
}

private void Update()
{
_collective = Heli.Collective;
_rollInput = Input.GetAxis("Horizontal");
_pitchInput = Input.GetAxis("Vertical") * -1f;
}

private void FixedUpdate()
{
var relwind = -Heli.Rigidbody.velocity;

for (int i=0; i<_rotors.Length; i++)
{
var holder = _rotors[i].connectedBody.transform;
var rotor = holder.GetChild(0);
var rvel = _rb.GetPointVelocity(rotor.transform.position) + relwind;
var angle = Vector3.SignedAngle(holder.forward, Heli.transform.forward, Heli.transform.up);
//var dot = Vector3.Dot(relwind.normalized, rvel.normalized); //use the dot product to simulate the effects of blade flapping (+1 == retreating blade; -1 == advancing blade)

var rollCyclic = Mathf.Sin(angle * Mathf.Deg2Rad) * _rollInput;
var pitchCyclic = Mathf.Cos(angle * Mathf.Deg2Rad) * _pitchInput;

var cyclic = Mathf.Clamp(rollCyclic + pitchCyclic, -1f, 1f);

var bladePitch = Mathf.Lerp(MinBladePitch, MaxBladePitch, _collective);
bladePitch += cyclic * CyclicMaxPitch;

rotor.transform.localRotation = Quaternion.AngleAxis(bladePitch, Vector3.left);
}
}
}
Loading

0 comments on commit 24dc65a

Please sign in to comment.