import time
import pychrono as chrono
import pychrono.irrlicht as chronoirr
import math
myfunction = lambda x,y,z: 1 / math.sqrt(x**2 + y**2 + z**2)
# Create a Chrono system
system = chrono.ChSystemSMC()
## With this next line, gravity stops despite me adding a Gravity-like loader later below
system.SetGravitationalAcceleration(chrono.ChVector3d(0, 0, 0)) # Disable default gravity
# Create a material
material = chrono.ChContactMaterialSMC()
material.SetFriction(0.5)
# Create a sphere
sphere = chrono.ChBodyEasySphere(0.05, 1000, True, True, material)
sphere.SetPos(chrono.ChVector3d(0.5, 0, 0))
system.Add(sphere)
# Create a floor
floor = chrono.ChBodyEasyBox(2, 0.1, 2, 1000, True, True, material)
floor.SetPos(chrono.ChVector3d(0, -1, 0))
floor.SetFixed(True)
system.Add(floor)
# Define a custom loader
class ForceFieldLoader(chrono.ChLoaderUVWdistributed):
def __init__(self, element):
super().__init__(element)
def ComputeF(self, U, V, W, F, state_x, state_w):
# Custom load calculation
# F.FillElem(0.0)
# F[0] = 10.0 # Example: constant load in X direction
F = myfunction(U, V, W)
## Attach the custom loader to all bodies in the system
load_container = chrono.ChLoadContainer()
for obj in system.GetBodies():
print(obj)
## If I uncomment the next line, I get "RuntimeError: SWIG director method error. Error detected when calling 'ChLoaderUVWdistributed.GetLoadable'"
# custom_loader = ForceFieldLoader(obj)
custom_loader = chrono.ChLoaderGravity(obj)
custom_load = chrono.ChLoad(custom_loader)
# print(custom_loader.GetLoadable()) ## <-- No problems here, even with ForceFieldLoader
load_container.Add(custom_load)
system.Add(load_container)
# Irrlicht basics
application = chronoirr.ChVisualSystemIrrlicht(system)
application.SetWindowSize(800, 600)
application.SetWindowTitle("Torus Simulation")
application.AddSkyBox()
application.AddTypicalLights()
## This doesn't work - Add an FPS camera through Irrlicht
## Results in a black screen and a locked mouse at the center
# camera = application.GetSceneManager().addCameraSceneNodeFPS()
# camera.setTarget(chronoirr.vector3df(0, 0, 0))
# application.cameras.push_back(camera)
## This works - standard Maya camera
application.AddCamera(chrono.ChVector3d(0, 1, -2))
application.BindAll()
# Simulation loop
while application.GetDevice().run():
application.BeginScene()
application.Render()
application.EndScene()
system.DoStepDynamics(0.01)
# time.sleep(0.1) # Slow down for better visibility