Skip to content

[Tips] development

Hyunseok edited this page Jul 15, 2020 · 1 revision

General

Coding Guide line

Just following this guide line. link

Bracing

  • Open braces should always be at the beginning of the line after the statement that begins the block.

Single line statements

  • Single line statements can have braces that begin and end on the same line.

Spacing

  • Spaces improve readability by decreasing code density

Naming

  • Do not use Hungarian notation Do not use a prefix for member variables (, m, s_, etc.). If you want to distinguish between local and member variables you should use “this.” in C# and “Me.” in VB.NET.
  • Do use camelCasing for member variables
  • Do use camelCasing for parameters
  • Do use camelCasing for local variables
  • Do use PascalCasing for function, property, event, and class names
  • Do prefix interfaces names with “I”
  • Do not prefix enums, classes, or delegates with any letter

ROS2 <-> Unity

Sensor data

If you trying to take communicating between ROS2 and Unity, you should consider the directions of rotation are different.

  • ROS2 => CCW
  • Unity => CW

Protobuf-net

Consider to use lock(Object) with MemoryStream variable since 'System.IO.MemoryStream' is not thread safe.

If you wan to reuse the MemoryStream which is utilized at Protobuf.Deserialize() function, must rewind the position to set 0 after Write() something on MemoryStream.

NetMQ

This library does not support UDP.

Unity3D related

General tips for Unity3D

thread vs coroutine

When threads are useful to use:

When you are computing some expensive and/or long-term operations, Threads can still be useful.

Examples of this are:

  • AI
  • Pathfinding
  • Network communication
  • Files operations

Refer to link

Unity API performance

Super Heavy

  • Instantiate

  • Resources.FindObjectsOfTypeAll

    because it loads all of those resources (and is clearly super slow at searching anyway)

Heavy

  • Destroy

  • Object(s).FindObjectOfType

  • GameObject.Find

  • GetComponentsInChildren (etc)

  • BroadcastMessage, SendMessageUpwards

    depending on the number of children/parents

Medium

  • GetComponents

  • GameObject.AddComponent

  • GameObject.FindWithTag

  • SendMessage

    it's fine for events don't use it every Update/OnGUI etc

  • OnGUI

    but it's more about the number of things you put in it

Low (but worth considering)

  • GetComponent

  • transform, animation, renderer, collider etc

  • transform.position, transform.rotation for things with parents

  • Debug.Log

    is slow if called every frame

Refer to link

Physics

PhysX Engine

Things just go crazy when you stack too many joints in one model with PhysX. The lack of high fidelity articulated physics support often manifest as the following problems:

  • Two rigidly joined rigid bodies can be pulled apart under huge force
  • Mass ratio between two joined rigid bodies is limited to less than 1:10 in order to maintain joint stability
  • Heavy weight at the end of a long rope makes the rope springy, jumpy and unstable
  • Motors are soft and cannot deliver enough power to drive multi-level articulated robotics
  • Doors, hands, wheels wobble around their joint axis under heavy load
  • Simulation step size (time interval) has to be reduced to too small to provide the needed accuracy which kills the performance

RigidBody

RigidBody in Unity calculates any moment of inertia from the colliders including all children colliders.

RigidBody Component must be added after all colliders in children tree are loaded. And, currently, inertia values from SDF can not use directly due to unexpected behavior with physics on Unity3D. So just let the RigidBody Component calculate the inertia momentum automatically.

Time

TBD;

Rendering

Color Space

for depth shading, we need to change colorspace 'Gamma' to 'Linear'.

  • Project Settings → Player → Other Settings → Color Space

refer to link