Skip to content

Instantly share code, notes, and snippets.

@omikun
omikun / Project3dVectorOnToPlane.cs
Created July 10, 2017 20:41
Project a 3d vector onto a plane, convert 3D to 2D vector, useful for rendering a 2D indicator over 3D element or get direction of 3D element if out of frame
//taken from http://answers.unity3d.com/questions/1097270/project-a-vector3-onto-a-plane-orthographically.html
//You simply project your vector onto the normal and subtract the result from your original vector. That will give you the projected vector on the plane. If you want to project a point to a plane you first need to calculate the relative vector from a point on that plane.
Vector3 planeOrigin;
Vector3 planeNormal;
Vector3 point;
Vector3 v = point - planeOrigin;
Vector3 d = Vector3.Project(v, planeNormal.normalized);
Vector3 projectedPoint = point - d;
@omikun
omikun / limit_max_memory_exec.sh
Created December 7, 2016 19:46
limits the maximum memory used by process (ls in example) to 10MB
# Create a memory cgroup named 'mygroup', allowing $USER to change the parameters and run tasks in the cgroup.
sudo cgcreate -a $USER -t $USER -g memory:mygroup
# Set the memory limit to 10MB
echo 10000000 > /sys/fs/cgroup/memory/mygroup/memory.limit_in_bytes
# Run a program in the cgroup
cgexec -g memory:mygroup ls
@omikun
omikun / vectormath.cs
Last active November 16, 2016 00:47
Vector math
float AngleBetweenVector2(Vector2 vec1, Vector2 vec2)
{
Vector2 diference = vec2 - vec1;
float sign = (vec2.y < vec1.y)? -1.0f : 1.0f;
return Vector2.Angle(Vector2.right, diference) * sign;
}
float cosAngleBetweenVector3(Vector3 vec1, Vector3, vec2)
{
float dot = Vector3.Dot(vec1,vec2);