Some help with Csharp & Unity

118 views
Skip to first unread message

J D

unread,
Nov 3, 2023, 9:25:15 PM11/3/23
to ProjectChrono
Hi all,

I'm attempting to get my head around some basic use of Chrono in Unity, I'd like to replicate some of the stuff I've seen Alan do in Unity, but simulating some heavy truck dynamics.

However, I'm having trouble getting off the ground with the namespace being unable to be found to get access to the Chrono functions.

I've tried a number of variations of "using Chrono" "using ChronoEngine" etc.

Compiling the library with SWIG 4.1.1 was successful, and I've placed the full library of dll's into the asset directory for the Unity Project, they're listed as 'native' and set for an 'x64' environment.

Is there a basic tutorial for this documented anywhere that I've missed? I've only found the python ones.

Any help is greatly appreciated!

Kind regards,

Josh


Josh Diyn

unread,
Nov 4, 2023, 12:13:11 AM11/4/23
to ProjectChrono
Update:

I didn't realise that I also need to copy the entire csharp directory to the unity project's assets folder, which appears to have helped. However, there's quite a number of C# compile errors to work through. I'll keep at it!

-jd

Josh Diyn

unread,
Nov 5, 2023, 1:37:22 AM11/5/23
to ProjectChrono
Update:
SWIG wrapping produced a number of implicit call errors within the .cs repository. Once those were debugged and sorted, I've got what appears to be an operational chrono c# module and .cs library.

I've managed to create a basic scene and attach a 'Chrono physics object' script to parse Unity cubes/boxes (haven't worked on convex hulls yet) into the chrono engine.
The "Chrono World" also required a world-manager type script attached to an empty object in my scene (and the script set to run prior to the physics objects). This then enables all the objects to 'enter' the same Chrono controlled physics world (and not create simultaneous, non-interacting worlds, as I found out the hard way).

Here's what I did:
1/ Create a basic Unity scene (I built in Unity 2021.3.19f1)
2/ Copy Csharp chrono dll's and the Csharp SWIG generated folder of built .cs files into the project's Assets directory (can be under a directory you create called 'chrono' for simplicity)
3/ create an empty game object and attach the world manager script
4/ create a bunch of cubes and attach the physics object script to them and set 'fixed' 'collision on' etc. as required.
5/ hit play and cross your fingers ;)

I hope this is permitted, I've attached the c# scripts, feel free to take and adapt for yourselves. Please excuse my poorly commented framework and non-optimised everything. It's all very rough and basic at this stage as I learn Chrono's particulars, but it might help someone else looking to make use of Unity.

unity chrono.PNG
ChronoPhysicsObject.cs
WorldManager.cs
ChronoPhysicsObjectEditor.cs

Josh Diyn

unread,
Nov 6, 2023, 6:36:52 AM11/6/23
to ProjectChrono
Update: ConvexHulls

I've now scripted a convexhull creator that reads the unity object's mesh, and parses it to Chrono to create an easyconvexhull and insert it into the chrono world.
Gizmos didn't work in displaying the collider mesh correctly, so I've written an editor drawing script that needs to go into a subfolder "Assets/Editor/" in order to work properly.

Hopefully others find this useful. I haven't tested with any objects other than the built in primitives. Nor have I tested broken mesh objects.

Scripts attached.
Enjoy :)


unity convex hull.PNG
ChronoPhysicsObject_ConvexMesh.cs
ChronoPhysicsObjectEditor.cs

Josh Diyn

unread,
Nov 12, 2023, 3:47:12 AM11/12/23
to ProjectChrono
Hi everyone,

An update for those playing along at home - Unity/Chrono interactions!

The unity objects are set as 'fixed' bodies so they don't react to any chrono collisions but they can cause them. It's a one way determiner, however notce in the gif, that the chrono objects (the boxes and the sphere and tyre) are sliding while the platform rotates underneath them (despite setting cohesion and friction high - i'm guessing because of the way chrono processes a kinematic body - i need to look into the documentation on that a bit more.

unity controlled object.png

Setting the 'unity controlled' overrides the 'fixed state' checkbox and sends a fixed signal to the chrono engine, updating the object's location with transform.position and transform.rotation.

The collisions between the two systems seems to be functioning well enough after I adjusted some solver settings.
I've attached a new world manager script that includes solver settings and the like, along with the updated chrono physics object script with working gizmos meshing (no need for the editor script)

I hope it helps others learnings
WorldManager.cs
ChronoPhysicsObject_v2.cs

Josh Diyn

unread,
Nov 12, 2023, 3:51:18 AM11/12/23
to ProjectChrono
in motion... I couldn't post it above, so its attached
The Red mixer is unity controlled, a parent with a basic controller script attached to the child cube and cylinder.
The platform is likewise unity controlled to rotate the other direction
(I'm aware we can make motors and the like in Chrono, but i wanted to get the two systems interacting)
interaction with unit objects_smaller.gif
Reply all
Reply to author
Forward
0 new messages