private Vector3 GetEngineInput(Vector3 localSpaceInput) { Vector3 engineInput = Vector3.zero; for (int i = 0; i < 3; i++) { ThrusterAxis axis = (ThrusterAxis)(0b1 << i); if (localSpaceInput[i] >= 0) { axis = axis | ThrusterAxis.plus; } else { axis = axis | ThrusterAxis.minus; } Vector3 axisVector = GetGlobalSpaceVectorFromAxis(axis); // calculate max thrust float maxThrust = 0f; foreach (var thruster in gameObject.GetComponentsInChildren <SingleThruster>()) { if (Vector3.Dot(thruster.globalThrustDirection, axisVector) > 0.95f) { maxThrust += thruster.maxThrust; } } engineInput[i] = maxThrust * localSpaceInput[i]; } return(engineInput); }
void Update() { if (attachedInteractionPoint == null) { return; } translationPoint.position = attachedInteractionPoint.transform.position; var thrusterAxis = new ThrusterAxis(translationPoint.localPosition, thrusterMaxDistance); rotationPoint.rotation = attachedInteractionPoint.transform.rotation; var angles = rotationPoint.localEulerAngles; StickAxis rotationAxis; if (upright) { rotationAxis = new StickAxis(angles); } else { rotationAxis = new StickAxis(angles.x, -angles.z + 360f, angles.y); } var endpoint = thrusterAxis.Value * thrusterMaxDistance; if (line) { line.SetPosition(1, new Vector3(endpoint.x, 0, endpoint.z)); } if (marker) { marker.gameObject.SetActive(true); marker.transform.localPosition = new Vector3(endpoint.x, 0, endpoint.z); var euler = marker.transform.localEulerAngles; euler.y = rotationAxis.Yaw; marker.transform.localEulerAngles = euler; } if (verticalDisplay) { verticalDisplay.transform.localPosition = new Vector3(0, 0, endpoint.z); } if (verticalLine) { verticalLine.transform.localPosition = new Vector3(endpoint.x, 0, 0); verticalLine.SetPosition(1, new Vector3(0, endpoint.y, 0)); } if (rollMarker) { rollMarker.gameObject.SetActive(true); rollMarker.transform.localPosition = new Vector3(endpoint.x, endpoint.y, 0); rollMarker.transform.localEulerAngles = new Vector3(0, 0, -rotationAxis.Roll); } if (output) { output.SetThrusters(thrusterAxis); output.SetStickAxis(rotationAxis); } }
public Vector3 GetLocalSpaceVectorFromAxis(ThrusterAxis axis) { float invert = axis.HasFlag(ThrusterAxis.minus) ? 1f : -1f; if (axis.HasFlag(ThrusterAxis.x)) { return(invert * Vector3.right); } else if (axis.HasFlag(ThrusterAxis.y)) { return(invert * Vector3.up); } else if (axis.HasFlag(ThrusterAxis.z)) { return(invert * Vector3.forward); } else { throw new Exception($"Unknown axis type {axis}"); } }
public Vector3 GetGlobalSpaceVectorFromAxis(ThrusterAxis axis) { return(transform.localToWorldMatrix.MultiplyVector(GetLocalSpaceVectorFromAxis(axis))); }