- Home /
World to local matrix without Transform
Hi! I'm trying to convert world coordinates to local coordinates without using a Transform. (For performance reasons i can't use gameobjects and transforms this time.) I think i need to use a matrix but i don't understand matrices. Can anyone give me some hints?
Answer by JedBeryll · Jan 10, 2016 at 08:13 PM
Ok so if anyone else is searching for an oriented bounding box here's my implementation: The last method is what i was looking for, found it somewhere else but don't remember where. If anyone finds a better or more efficient solution please post it here.
public class Cuboid {
public Vector3 Center { get; set; }
public Quaternion Rotation { get; set; }
public Vector3 Size { get; set; }
public Cuboid (Vector3 center, Vector3 size, Quaternion rot) {
Center = center;
Size = size;
Rotation = rot;
}
public bool Contains (Vector3 worldPoint) {
Vector3 extents = AbsoluteVector(Size) / 2;
Vector3 projected = RotatePointAroundPivot(Center, worldPoint, Rotation);
Vector3 max = Center + extents;
Vector3 min = Center - extents;
if (projected.x <= max.x && projected.y <= max.y && projected.z <= max.z && projected.x >= min.x && projected.y >= min.y && projected.z >= min.z) return true;
else return false;
}
public static Vector3 RotatePointAroundPivot(Vector3 pivot, Vector3 point, Quaternion angle) {
angle = Quaternion.Inverse(angle);
Vector3 direction = pivot - point;
direction = angle * direction;
return direction + pivot;
}
public static Vector3 AbsoluteVector (Vector3 initial){
initial.x = Mathf.Abs(initial.x);
initial.y = Mathf.Abs(initial.y);
initial.z = Mathf.Abs(initial.z);
return initial;
}
}
Your answer
Follow this Question
Related Questions
Why on earth can I not just get and set the Matrix4x4 of a Transform?!? 2 Answers
GUI.Label font size scale - using GUISkin 1 Answer
WorldToViewportPoint problem 0 Answers
Custom Camera Projection Matrix that "Horizontally Compresses" worldview 1 Answer
Matrix to transform bone orientation to world orientation 0 Answers