Last active
July 19, 2016 01:40
-
-
Save birdinforest/e4990b76af7cba5131627d98dc68ac34 to your computer and use it in GitHub Desktop.
# Bounds extension methods: 1. Conversion between local bounds and world bounds; 2. Get corners of bounds. # Vector3 extension methods.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
public static class ExtensionMethods { | |
//--------------------- | |
// transform | |
//--------------------- | |
public static Bounds TransformBounds(this Transform self, Bounds bounds) | |
{ | |
var center = self.TransformPoint(bounds.center); | |
var points = bounds.GetCorners(); | |
var result = new Bounds(center, Vector3.zero); | |
foreach (var point in points) | |
result.Encapsulate(self.TransformPoint(point)); | |
return result; | |
} | |
public static Bounds InverseTransformBounds(this Transform self, Bounds bounds) | |
{ | |
var center = self.InverseTransformPoint(bounds.center); | |
var points = bounds.GetCorners(); | |
var result = new Bounds(center, Vector3.zero); | |
foreach (var point in points) | |
result.Encapsulate(self.InverseTransformPoint(point)); | |
return result; | |
} | |
public static Bounds TransformBounds( this Transform _transform, Bounds _localBounds ) | |
{ | |
var center = _transform.TransformPoint(_localBounds.center); | |
// transform the local extents' axes | |
var extents = _localBounds.extents; | |
var axisX = _transform.TransformVector(extents.x, 0, 0); | |
var axisY = _transform.TransformVector(0, extents.y, 0); | |
var axisZ = _transform.TransformVector(0, 0, extents.z); | |
// sum their absolute value to get the world extents | |
extents.x = Mathf.Abs(axisX.x) + Mathf.Abs(axisY.x) + Mathf.Abs(axisZ.x); | |
extents.y = Mathf.Abs(axisX.y) + Mathf.Abs(axisY.y) + Mathf.Abs(axisZ.y); | |
extents.z = Mathf.Abs(axisX.z) + Mathf.Abs(axisY.z) + Mathf.Abs(axisZ.z); | |
return new Bounds { center = center, extents = extents }; | |
} | |
public static Bounds InverseTransformBounds( this Transform _transform, Bounds _localBounds ) | |
{ | |
var center = _transform.InverseTransformPoint(_localBounds.center); | |
// transform the local extents' axes | |
var extents = _localBounds.extents; | |
var axisX = _transform.InverseTransformVector(extents.x, 0, 0); | |
var axisY = _transform.InverseTransformVector(0, extents.y, 0); | |
var axisZ = _transform.InverseTransformVector(0, 0, extents.z); | |
// sum their absolute value to get the world extents | |
extents.x = Mathf.Abs(axisX.x) + Mathf.Abs(axisY.x) + Mathf.Abs(axisZ.x); | |
extents.y = Mathf.Abs(axisX.y) + Mathf.Abs(axisY.y) + Mathf.Abs(axisZ.y); | |
extents.z = Mathf.Abs(axisX.z) + Mathf.Abs(axisY.z) + Mathf.Abs(axisZ.z); | |
return new Bounds { center = center, extents = extents }; | |
} | |
//--------------------- | |
// bounds | |
//--------------------- | |
public static List<Vector3> GetCorners(this Bounds obj, bool includePosition = true) | |
{ | |
var result = new List<Vector3>(); | |
for (int x = -1; x <= 1; x += 2) | |
for (int y = -1; y <= 1; y += 2) | |
for (int z = -1; z <= 1; z += 2) | |
result.Add((includePosition ? obj.center : Vector3.zero) + (obj.size * 0.5F).Times(new Vector3(x, y, z))); | |
return result; | |
} | |
//--------------------- | |
// Vector3 | |
//--------------------- | |
public static Vector3 Times(this Vector3 v3, Vector3 other) | |
{ | |
float x = v3.x * other.x; | |
float y = v3.y * other.y; | |
float z = v3.z * other.z; | |
return new Vector3(x, y, z); | |
} | |
public static Vector3 RotateAroundPivot(this Vector3 v3, Vector3 pivot, Vector3 eulerAngles) { | |
Vector3 dir = v3 - pivot; // get point direction relative to pivot | |
dir = Quaternion.Euler(eulerAngles) * dir; // rotate it | |
new Vector3 newV3 = dir + pivot; // calculate rotated point | |
return newV3; // return it | |
} | |
public static Vector3 RotateAroundOrigin(this Vector3 v3, Vector3 eulerAngles) { | |
return Quaternion.Euler(eulerAngles) * v3; // rotate it | |
} | |
//--------------------- | |
// Rigidbody2D | |
//--------------------- | |
// An approch similiar to Rigidbody::AddExplosionForce | |
public static void AddExplosionForce(this Rigidbody2D body, float explosionForce, Vector3 explosionPosition, float explosionRadius) | |
{ | |
var dir = (body.transform.position - explosionPosition); | |
float wearoff = 1 - (dir.magnitude / explosionRadius); | |
body.AddForce(dir.normalized * explosionForce * wearoff); | |
} | |
// An approch similiar to Rigidbody::AddExplosionForce | |
public static void AddExplosionForce(this Rigidbody2D body, float explosionForce, Vector3 explosionPosition, float explosionRadius, float upliftModifier) | |
{ | |
var dir = (body.transform.position - explosionPosition); | |
float wearoff = 1 - (dir.magnitude / explosionRadius); | |
Vector3 baseForce = dir.normalized * explosionForce * wearoff; | |
body.AddForce(baseForce); | |
float upliftWearoff = 1 - upliftModifier / explosionRadius; | |
Vector3 upliftForce = Vector2.up * explosionForce * upliftWearoff; | |
body.AddForce(upliftForce); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bounds extension methods:
Vector3 extension methods:
Rigidbody2D extension methods: