Skip to content

Instantly share code, notes, and snippets.

@mpaccione
Created June 20, 2019 00:00
Show Gist options
  • Save mpaccione/371a42694270e71d1ed371a72cfa7aaa to your computer and use it in GitHub Desktop.
Save mpaccione/371a42694270e71d1ed371a72cfa7aaa to your computer and use it in GitHub Desktop.
Trigonometry Helicopter Physics
updateVelocities(){
// Convert Degrees to Radians
const rollRads = this.roll < 0 ? this.getRadians( 150-this.roll ) : this.getRadians( 150+this.roll ) , // Hypothetically 90-this.roll, changed for better playability
pitchRads = this.pitch < 0 ? this.getRadians( 150-this.pitch ) : this.getRadians( 150+this.pitch ), // Hypothetically 90-this.pitch, changed for better playability
gravSimY = this.aY/this.weight,
yawRatio = this.yaw/this.maxYaw;
// Rotational Velocity
this.vR = this.aX * yawRatio;
// Y Velocity from accel & gravity
this.vY = this.aY <= this.gravAOffset ? gravSimY - this.gravVOffset : gravSimY;
const vYOriginal = this.vY;
// X & Z Velocity
if ( this.roll != 0 && this.pitch != 0 ) {
// Get Higher Degree of the two, use resultant Y Velocity for second equation
if ( Math.abs(this.roll) > Math.abs(this.pitch) ) {
// Calc Roll Vector with Trigonometry,
this.vX = Math.abs(vYOriginal * Math.cos(rollRads));
this.vY = Math.abs(vYOriginal * Math.sin(rollRads));
this.vZ = Math.abs(vYOriginal * Math.cos(pitchRads));
} else if ( Math.abs(this.pitch) > Math.abs(this.roll) ) {
// Calc Pitch Vector with Trigonometry
this.vX = Math.abs(vYOriginal * Math.cos(rollRads));
this.vY = Math.abs(vYOriginal * Math.sin(pitchRads));
this.vZ = Math.abs(vYOriginal * Math.cos(pitchRads));
}
} else if ( this.roll != 0 ) {
// Calc Roll Vector with Trigonometry
this.vX = vYOriginal * Math.cos(rollRads);
this.vY = vYOriginal * Math.sin(rollRads);
} else if ( this.pitch != 0 ) {
// Calc Pitch Vector with Trigonometry
this.vY = vYOriginal * Math.sin(pitchRads);
this.vZ = vYOriginal * Math.cos(pitchRads);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment