Skip to content

Commit

Permalink
mmm physics
Browse files Browse the repository at this point in the history
  • Loading branch information
Xlinka committed Nov 9, 2023
1 parent 0b94d35 commit 5443623
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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<PotentialEnergyCalculationNode>
{
public readonly SyncRef<INodeValueOutput<float>> Mass;
public readonly SyncRef<INodeValueOutput<float>> 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<TN>()
{
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;
}
}
}
}
2 changes: 1 addition & 1 deletion ProtoFlux/Bindings/Math/Physics/RefractNodeBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class RefractionCalculation : FrooxEngine.ProtoFlux.Runtimes.Execution.Va
public readonly SyncRef<INodeValueOutput<float>> AngleOfIncidence;

public override Type NodeType => typeof(RefractionNode);

public RefractionNode TypedNodeInstance { get; private set; }

public override INode NodeInstance => TypedNodeInstance;
Expand Down
34 changes: 34 additions & 0 deletions ProtoFlux/Math/Physics/PotentialEnergyCalculationNode
Original file line number Diff line number Diff line change
@@ -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<ExecutionContext, float>
{
public ValueInput<float> Mass;
public ValueInput<float> 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
}
}
}
11 changes: 5 additions & 6 deletions ProtoFlux/Math/Physics/RefractCalculationNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,23 @@ namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Math.Physics
[NodeCategory("Obsidian/Math/Physics")]
public class RefractionNode : ValueFunctionNode<ExecutionContext, float>
{
public ValueInput<float> RefractiveIndex1; // Refractive index of medium 1
public ValueInput<float> RefractiveIndex2; // Refractive index of medium 2
public ValueInput<float> AngleOfIncidence; // Angle of incidence in degrees
public ValueInput<float> RefractiveIndex1;
public ValueInput<float> RefractiveIndex2;
public ValueInput<float> 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;

// Ensure value is within [-1, 1] due to numerical inaccuracies
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;
}
}
}

0 comments on commit 5443623

Please sign in to comment.