diff --git a/ProtoFlux/Bindings/Math/Physics/PotentialEnergyCalculationBinding.CS b/ProtoFlux/Bindings/Math/Physics/PotentialEnergyCalculationBinding.CS new file mode 100644 index 0000000..f042147 --- /dev/null +++ b/ProtoFlux/Bindings/Math/Physics/PotentialEnergyCalculationBinding.CS @@ -0,0 +1,76 @@ +using FrooxEngine.ProtoFlux; +using ProtoFlux.Runtimes.Execution; +using System; + +namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Math.Physics +{ + [Category("Obsidian/Math/Physics")] + public class PotentialEnergyCalculationBinding : FrooxEngine.ProtoFlux.Runtimes.Execution.NodeBinding + { + public readonly SyncRef> Mass; + public readonly SyncRef> Height; + + public override Type NodeType => typeof(PotentialEnergyCalculationNode); + + public PotentialEnergyCalculationNode TypedNodeInstance { get; private set; } + + public override INode NodeInstance => TypedNodeInstance; + + public override int NodeInputCount => base.NodeInputCount + 2; + + public override TN Instantiate() + { + try + { + if (TypedNodeInstance != null) + throw new InvalidOperationException("Node has already been instantiated"); + + var instance = (TypedNodeInstance = new PotentialEnergyCalculationNode()); + return instance as TN; + } + catch (Exception ex) + { + UniLog.Log($"Error in PotentialEnergyCalculationBinding.Instantiate: {ex.Message}"); + throw; + } + } + + protected override void AssociateInstanceInternal(INode node) + { + try + { + if (node is not PotentialEnergyCalculationNode typedNodeInstance) + throw new ArgumentException("Node instance is not of type " + typeof(PotentialEnergyCalculationNode)); + + TypedNodeInstance = typedNodeInstance; + } + catch (Exception ex) + { + UniLog.Log($"Error in PotentialEnergyCalculationBinding.AssociateInstanceInternal: {ex.Message}"); + throw; + } + } + + public override void ClearInstance() => TypedNodeInstance = null; + + protected override ISyncRef GetInputInternal(ref int index) + { + var inputInternal = base.GetInputInternal(ref index); + if (inputInternal != null) + { + return inputInternal; + } + + switch (index) + { + case 0: + return Mass; + case 1: + return Height; + default: + index -= 2; + return null; + } + } + } +} diff --git a/ProtoFlux/Bindings/Math/Physics/RefractNodeBinding.cs b/ProtoFlux/Bindings/Math/Physics/RefractNodeBinding.cs index 39eb99a..add0e62 100644 --- a/ProtoFlux/Bindings/Math/Physics/RefractNodeBinding.cs +++ b/ProtoFlux/Bindings/Math/Physics/RefractNodeBinding.cs @@ -13,7 +13,7 @@ public class RefractionCalculation : FrooxEngine.ProtoFlux.Runtimes.Execution.Va public readonly SyncRef> AngleOfIncidence; public override Type NodeType => typeof(RefractionNode); - + public RefractionNode TypedNodeInstance { get; private set; } public override INode NodeInstance => TypedNodeInstance; diff --git a/ProtoFlux/Math/Physics/PotentialEnergyCalculationNode b/ProtoFlux/Math/Physics/PotentialEnergyCalculationNode new file mode 100644 index 0000000..1f5470d --- /dev/null +++ b/ProtoFlux/Math/Physics/PotentialEnergyCalculationNode @@ -0,0 +1,34 @@ +using Elements.Core; +using ProtoFlux.Core; +using ProtoFlux.Runtimes.Execution; +using System; + +namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Math.Physics +{ + [NodeCategory("Obsidian/Math/Physics")] + public class PotentialEnergyCalculationNode : ValueFunctionNode + { + public ValueInput Mass; + public ValueInput Height; + + protected override float Compute(ExecutionContext context) + { + try + { + float mass = Mass.Evaluate(context); + float height = Height.Evaluate(context); + + float potentialEnergy = mass * Constants.G * height; + + return potentialEnergy; + } + catch (Exception ex) + { + // Log any exceptions without throwing them + UniLog.Log($"Error in PotentialEnergyCalculationNode.Compute: {ex.Message}"); + } + + return 0.0f; // Return a default value in case of an error + } + } +} diff --git a/ProtoFlux/Math/Physics/RefractCalculationNode.cs b/ProtoFlux/Math/Physics/RefractCalculationNode.cs index 070bc5c..df862f7 100644 --- a/ProtoFlux/Math/Physics/RefractCalculationNode.cs +++ b/ProtoFlux/Math/Physics/RefractCalculationNode.cs @@ -8,16 +8,15 @@ namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Math.Physics [NodeCategory("Obsidian/Math/Physics")] public class RefractionNode : ValueFunctionNode { - public ValueInput RefractiveIndex1; // Refractive index of medium 1 - public ValueInput RefractiveIndex2; // Refractive index of medium 2 - public ValueInput AngleOfIncidence; // Angle of incidence in degrees + public ValueInput RefractiveIndex1; + public ValueInput RefractiveIndex2; + public ValueInput AngleOfIncidence; protected override float Compute(ExecutionContext context) { float n1 = RefractiveIndex1.Evaluate(context); float n2 = RefractiveIndex2.Evaluate(context); - float theta1Rad = AngleOfIncidence.Evaluate(context) * (float)Math.PI / 180.0f; // Convert angle to radians - + float theta1Rad = AngleOfIncidence.Evaluate(context) * (float)Math.PI / 180.0f; // Calculate using Snell's Law float sinTheta2 = n1 * (float)Math.Sin(theta1Rad) / n2; @@ -25,7 +24,7 @@ protected override float Compute(ExecutionContext context) sinTheta2 = Math.Min(Math.Max(sinTheta2, -1.0f), 1.0f); float theta2Rad = (float)Math.Asin(sinTheta2); - return theta2Rad * 180.0f / (float)Math.PI; // Convert angle back to degrees + return theta2Rad * 180.0f / (float)Math.PI; } } }