Skip to content

Coordinates extraction #223

Closed
Closed

Description

@DomenicoRiggio

Hello,

I am working with a SOFA scene.scn where a mesh undergoes a rigid translation in 3D space (no deformation), driven by a set of springs.
The scene is loaded in Unity using SofaContext, and the simulation itself runs correctly.

However, I need to retrieve and record the coordinates of this mesh while it is moving, ideally in SOFA coordinates, but Unity coordinates would also be acceptable.

From my observations:
• The Transform values shown in the Unity Inspector are never updated, even though the objects visibly move.
• I cannot find any SofaUnity component or API that exposes the runtime position of an element of the scene.
• This behavior is consistent not only in my own scene, but also in the official SofaUnity demos and tutorials.

Is there a recommended or supported way to access the runtime position (SOFA or Unity coordinates) of an object in a SofaUnity scene.scn?

Thank you in advance for your help.

Activity

  1. epernod commented on Jan 15, 2026

    @epernod
    Contributor

    Hi,

    thank you for your explanation. Yes, depending on the template (Rigid3d or Vec3) of your mechanicalObject in SOFA, there are several ways to get the position in the SOFA or Unity world space.
    you can check for example this code for a Rigid3d object:

    protected void UpdateFromSofa()
    {
    float[] val = new float[7];
    m_sofaMesh.GetRawPositions(val);
    // Get raw values from SOFA, need to inverse left-right hand coordinate system
    Vector3 worldPos = new Vector3(-val[0], val[1], val[2]);
    // Project world position into SofaContext frame
    Vector3 localPos = m_sofaMesh.m_sofaContext.transform.TransformPoint(worldPos);
    this.transform.localPosition = localPos;
    // Get SOFA quaternion and inverse rotation
    var rotation = new Quaternion(val[3], val[4], val[5], val[6]);
    Vector3 angles = rotation.eulerAngles;
    this.transform.localEulerAngles = new Vector3(angles[0], -angles[1], -angles[2]);
    // Combine current rotation with SofaContext one
    this.transform.rotation = m_sofaMesh.m_sofaContext.transform.rotation * this.transform.rotation;
    }

    More generally in the SofaMesh, the GetRawPositions will return position in the SOFA coordinate system

    public void GetRawPositions(float[] vertices)
    {
    if (m_sofaMeshAPI != null)
    m_sofaMeshAPI.GetRawPositions(vertices);
    }

    Otherwise the SofaMesh creates a unity MeshFilter and you can access its vertices which will be in the Unity coordinate system:. For example:

    Vector3 vert = m_unityMesh.mesh.vertices[i];

    Let me know if this works
    Best regards

  2. locked and limited conversation to collaborators on Jan 15, 2026
  3. converted this issue into a discussion #230 on Jan 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions