physics to addons

This commit is contained in:
2016-11-27 05:47:38 +07:00
parent dddd778c34
commit 49a78c0d3f
46 changed files with 274 additions and 68 deletions

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 8dd47e4d38ef9485586318e48755aff9
folderAsset: yes
timeCreated: 1480174284
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: e3a035a6c93424c7a98d81c8cb5aad1c
folderAsset: yes
timeCreated: 1480174340
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,6 +1,6 @@
using UnityEngine;
namespace IsoTools.Internal {
namespace IsoTools.Physics.Internal {
public class IsoFakeCollider : MonoBehaviour {
IsoCollider _isoCollider = null;

View File

@@ -1,6 +1,6 @@
using UnityEngine;
namespace IsoTools.Internal {
namespace IsoTools.Physics.Internal {
public class IsoFakeCollisionListener : MonoBehaviour {
GameObject _realGameObject = null;

View File

@@ -1,6 +1,6 @@
using UnityEngine;
namespace IsoTools.Internal {
namespace IsoTools.Physics.Internal {
public class IsoFakeObject : MonoBehaviour {
IsoObject _isoObject = null;
@@ -25,9 +25,9 @@ namespace IsoTools.Internal {
void FixedUpdate() {
CheckLayers();
if ( !IsoUtils.Vec3Approximately(_lastPosition, isoObject.position) ) {
if ( _lastPosition != isoObject.position ) {
_lastPosition = transform.position = isoObject.position;
} else if ( !IsoUtils.Vec3Approximately(_lastPosition, transform.position) ) {
} else if ( _lastPosition != transform.position ) {
_lastPosition = isoObject.position = transform.position;
}
}

View File

@@ -1,6 +1,6 @@
using UnityEngine;
namespace IsoTools.Internal {
namespace IsoTools.Physics.Internal {
public class IsoFakeRigidbody : MonoBehaviour {
IsoRigidbody _isoRigidbody = null;

View File

@@ -1,6 +1,6 @@
using UnityEngine;
namespace IsoTools.Internal {
namespace IsoTools.Physics.Internal {
public class IsoFakeTriggerListener : MonoBehaviour {
GameObject _realGameObject = null;
@@ -12,14 +12,14 @@ namespace IsoTools.Internal {
void OnTriggerEnter(Collider collider) {
_realGameObject.SendMessage(
"OnIsoTriggerEnter",
IsoUtils.IsoConvertCollider(collider),
IsoPhysicsUtils.IsoConvertCollider(collider),
SendMessageOptions.DontRequireReceiver);
}
void OnTriggerExit(Collider collider) {
_realGameObject.SendMessage(
"OnIsoTriggerExit",
IsoUtils.IsoConvertCollider(collider),
IsoPhysicsUtils.IsoConvertCollider(collider),
SendMessageOptions.DontRequireReceiver);
}
}

View File

@@ -1,9 +1,9 @@
using UnityEngine;
namespace IsoTools.Internal {
namespace IsoTools.Physics.Internal {
[DisallowMultipleComponent]
[RequireComponent(typeof(IsoObject))]
public class IsoPhysicHelper : MonoBehaviour {
public class IsoPhysicsHelper : MonoBehaviour {
GameObject _isoFakeObject = null;
public GameObject isoFakeObject {

View File

@@ -0,0 +1,26 @@
using UnityEngine;
using IsoTools.Internal;
using System.Collections.Generic;
namespace IsoTools.Physics.Internal {
public class IsoPhysicsHelperHolder : MonoBehaviour {
static List<IsoPhysicsHelperHolder> _tmpHolders = new List<IsoPhysicsHelperHolder>(7);
protected GameObject fakeObject {
get { return physicsHelper.isoFakeObject; }
}
protected IsoPhysicsHelper physicsHelper {
get { return IsoUtils.GetOrCreateComponent<IsoPhysicsHelper>(gameObject); }
}
protected void DestroyUnnecessaryCheck() {
GetComponents<IsoPhysicsHelperHolder>(_tmpHolders);
if ( _tmpHolders.Count == 1 && _tmpHolders[0] == this ) {
Destroy(physicsHelper);
}
_tmpHolders.Clear();
}
}
}

View File

@@ -0,0 +1,37 @@
using UnityEngine;
namespace IsoTools.Physics.Internal {
public static class IsoPhysicsUtils {
public static IsoCollider IsoConvertCollider(Collider collider) {
var fake_collider = collider ? collider.GetComponent<IsoFakeCollider>() : null;
return fake_collider ? fake_collider.isoCollider : null;
}
public static IsoRigidbody IsoConvertRigidbody(Rigidbody rigidbody) {
var fake_rigidbody = rigidbody ? rigidbody.GetComponent<IsoFakeRigidbody>() : null;
return fake_rigidbody ? fake_rigidbody.isoRigidbody : null;
}
public static GameObject IsoConvertGameObject(GameObject game_object) {
var fake_object = game_object ? game_object.GetComponent<IsoFakeObject>() : null;
var iso_object = fake_object ? fake_object.isoObject : null;
return iso_object ? iso_object.gameObject : null;
}
public static IsoContactPoint[] IsoConvertContactPoints(ContactPoint[] points) {
var iso_points = new IsoContactPoint[points.Length];
for ( int i = 0, e = points.Length; i < e; ++i ) {
iso_points[i] = new IsoContactPoint(points[i]);
}
return iso_points;
}
public static IsoRaycastHit[] IsoConvertRaycastHits(RaycastHit[] hits) {
var iso_hits = new IsoRaycastHit[hits.Length];
for ( int i = 0, e = hits.Length; i < e; ++i ) {
iso_hits[i] = new IsoRaycastHit(hits[i]);
}
return iso_hits;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 0d44532ca15804cdc997583d81cf4df1
timeCreated: 1480196616
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -5,7 +5,7 @@ using IsoTools.Internal;
using UnityEditor;
#endif
namespace IsoTools {
namespace IsoTools.Physics {
[RequireComponent(typeof(IsoObject))]
public class IsoBoxCollider : IsoCollider {

View File

@@ -5,7 +5,7 @@ using IsoTools.Internal;
using UnityEditor;
#endif
namespace IsoTools {
namespace IsoTools.Physics {
[RequireComponent(typeof(IsoObject))]
public class IsoCapsuleCollider : IsoCollider {

View File

@@ -1,13 +1,13 @@
using UnityEngine;
using IsoTools.Internal;
using IsoTools.Physics.Internal;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace IsoTools {
namespace IsoTools.Physics {
[RequireComponent(typeof(IsoObject))]
public abstract class IsoCollider : IsoPhysicHelperHolder {
public abstract class IsoCollider : IsoPhysicsHelperHolder {
protected abstract Collider CreateRealCollider(GameObject target);
IsoFakeCollider _fakeCollider;
@@ -44,7 +44,7 @@ namespace IsoTools {
public IsoRigidbody attachedRigidbody {
get {
return realCollider
? IsoUtils.IsoConvertRigidbody(realCollider.attachedRigidbody)
? IsoPhysicsUtils.IsoConvertRigidbody(realCollider.attachedRigidbody)
: null;
}
}

View File

@@ -1,7 +1,7 @@
using UnityEngine;
using IsoTools.Internal;
using IsoTools.Physics.Internal;
namespace IsoTools {
namespace IsoTools.Physics {
public class IsoCollision {
public IsoCollider collider { get; private set; }
@@ -12,12 +12,12 @@ namespace IsoTools {
public IsoRigidbody rigidbody { get; private set; }
public IsoCollision(Collision collision) {
collider = IsoUtils.IsoConvertCollider(collision.collider);
contacts = IsoUtils.IsoConvertContactPoints(collision.contacts);
gameObject = IsoUtils.IsoConvertGameObject(collision.gameObject);
collider = IsoPhysicsUtils.IsoConvertCollider(collision.collider);
contacts = IsoPhysicsUtils.IsoConvertContactPoints(collision.contacts);
gameObject = IsoPhysicsUtils.IsoConvertGameObject(collision.gameObject);
impulse = collision.impulse;
relativeVelocity = collision.relativeVelocity;
rigidbody = IsoUtils.IsoConvertRigidbody(collision.rigidbody);
rigidbody = IsoPhysicsUtils.IsoConvertRigidbody(collision.rigidbody);
}
}
}

View File

@@ -1,10 +1,10 @@
using UnityEngine;
using IsoTools.Internal;
using IsoTools.Physics.Internal;
namespace IsoTools {
namespace IsoTools.Physics {
[DisallowMultipleComponent]
[RequireComponent(typeof(IsoObject))]
public class IsoCollisionListener : IsoPhysicHelperHolder {
public class IsoCollisionListener : IsoPhysicsHelperHolder {
IsoFakeCollisionListener _fakeListener;

View File

@@ -1,7 +1,7 @@
using UnityEngine;
using IsoTools.Internal;
using IsoTools.Physics.Internal;
namespace IsoTools {
namespace IsoTools.Physics {
public struct IsoContactPoint {
public Vector3 normal { get; private set; }
@@ -12,10 +12,10 @@ namespace IsoTools {
public IsoContactPoint(ContactPoint contact_point) : this() {
normal = contact_point.normal;
otherCollider = IsoUtils.IsoConvertCollider(contact_point.otherCollider);
otherCollider = IsoPhysicsUtils.IsoConvertCollider(contact_point.otherCollider);
point = contact_point.point;
separation = contact_point.separation;
thisCollider = IsoUtils.IsoConvertCollider(contact_point.thisCollider);
thisCollider = IsoPhysicsUtils.IsoConvertCollider(contact_point.thisCollider);
}
}
}

View File

@@ -0,0 +1,120 @@
using UnityEngine;
using IsoTools.Physics.Internal;
namespace IsoTools.Physics {
public static class IsoPhysics {
//
// Raycast
//
public static bool Raycast(Ray ray, out IsoRaycastHit iso_hit_info) {
return Raycast(ray, out iso_hit_info,
Mathf.Infinity, UnityEngine.Physics.DefaultRaycastLayers,
QueryTriggerInteraction.UseGlobal);
}
public static bool Raycast(Ray ray, out IsoRaycastHit iso_hit_info,
float max_distance)
{
return Raycast(ray, out iso_hit_info,
max_distance, UnityEngine.Physics.DefaultRaycastLayers,
QueryTriggerInteraction.UseGlobal);
}
public static bool Raycast(Ray ray, out IsoRaycastHit iso_hit_info,
float max_distance, int layer_mask)
{
return Raycast(ray, out iso_hit_info,
max_distance, layer_mask,
QueryTriggerInteraction.UseGlobal);
}
public static bool Raycast(Ray ray, out IsoRaycastHit iso_hit_info,
float max_distance, int layer_mask,
QueryTriggerInteraction query_trigger_interaction)
{
var hit_info = new RaycastHit();
var result = UnityEngine.Physics.Raycast(ray, out hit_info,
max_distance, layer_mask, query_trigger_interaction);
iso_hit_info = result ? new IsoRaycastHit(hit_info) : new IsoRaycastHit();
return result;
}
//
// RaycastAll
//
public static IsoRaycastHit[] RaycastAll(Ray ray) {
return RaycastAll(ray,
Mathf.Infinity, UnityEngine.Physics.DefaultRaycastLayers,
QueryTriggerInteraction.UseGlobal);
}
public static IsoRaycastHit[] RaycastAll(Ray ray,
float max_distance)
{
return RaycastAll(ray,
max_distance, UnityEngine.Physics.DefaultRaycastLayers,
QueryTriggerInteraction.UseGlobal);
}
public static IsoRaycastHit[] RaycastAll(Ray ray,
float max_distance, int layer_mask)
{
return RaycastAll(ray,
max_distance, layer_mask,
QueryTriggerInteraction.UseGlobal);
}
public static IsoRaycastHit[] RaycastAll(Ray ray,
float max_distance, int layer_mask,
QueryTriggerInteraction query_trigger_interaction)
{
var hits_info = UnityEngine.Physics.RaycastAll(ray,
max_distance, layer_mask, query_trigger_interaction);
return IsoPhysicsUtils.IsoConvertRaycastHits(hits_info);
}
//
// RaycastNonAlloc
//
public static int RaycastNonAlloc(Ray ray, IsoRaycastHit[] results) {
return RaycastNonAlloc(ray, results,
Mathf.Infinity, UnityEngine.Physics.DefaultRaycastLayers,
QueryTriggerInteraction.UseGlobal);
}
public static int RaycastNonAlloc(Ray ray, IsoRaycastHit[] results,
float max_distance)
{
return RaycastNonAlloc(ray, results,
max_distance, UnityEngine.Physics.DefaultRaycastLayers,
QueryTriggerInteraction.UseGlobal);
}
public static int RaycastNonAlloc(Ray ray, IsoRaycastHit[] results,
float max_distance, int layer_mask)
{
return RaycastNonAlloc(ray, results,
max_distance, layer_mask,
QueryTriggerInteraction.UseGlobal);
}
static RaycastHit[] _raycastNonAllocBuffer = new RaycastHit[128];
public static int RaycastNonAlloc(Ray ray, IsoRaycastHit[] results,
float max_distance, int layer_mask,
QueryTriggerInteraction query_trigger_interaction)
{
var hit_count = UnityEngine.Physics.RaycastNonAlloc(ray, _raycastNonAllocBuffer,
max_distance, layer_mask, query_trigger_interaction);
var min_hit_count = Mathf.Min(hit_count, results.Length);
for ( var i = 0; i < min_hit_count; ++i ) {
results[i] = new IsoRaycastHit(_raycastNonAllocBuffer[i]);
}
System.Array.Clear(_raycastNonAllocBuffer, 0, _raycastNonAllocBuffer.Length);
return min_hit_count;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 40ab1c30204174ffa8d877e00af23a62
timeCreated: 1480174712
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,7 +1,7 @@
using UnityEngine;
using IsoTools.Internal;
using IsoTools.Physics.Internal;
namespace IsoTools {
namespace IsoTools.Physics {
public struct IsoRaycastHit {
public IsoCollider collider { get; private set; }
@@ -11,11 +11,11 @@ namespace IsoTools {
public IsoRigidbody rigidbody { get; private set; }
public IsoRaycastHit(RaycastHit hit_info) : this() {
collider = IsoUtils.IsoConvertCollider(hit_info.collider);
collider = IsoPhysicsUtils.IsoConvertCollider(hit_info.collider);
distance = hit_info.distance;
normal = hit_info.normal;
point = hit_info.point;
rigidbody = IsoUtils.IsoConvertRigidbody(hit_info.rigidbody);
rigidbody = IsoPhysicsUtils.IsoConvertRigidbody(hit_info.rigidbody);
}
}
}

View File

@@ -1,14 +1,14 @@
using UnityEngine;
using IsoTools.Internal;
using IsoTools.Physics.Internal;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace IsoTools {
namespace IsoTools.Physics {
[DisallowMultipleComponent]
[RequireComponent(typeof(IsoObject))]
public class IsoRigidbody : IsoPhysicHelperHolder {
public class IsoRigidbody : IsoPhysicsHelperHolder {
IsoFakeRigidbody _fakeRigidbody;
@@ -330,7 +330,8 @@ namespace IsoTools {
float max_distance, QueryTriggerInteraction query_trigger_interaction)
{
return realRigidbody
? IsoUtils.IsoConvertRaycastHits(realRigidbody.SweepTestAll(direction, max_distance, query_trigger_interaction))
? IsoPhysicsUtils.IsoConvertRaycastHits(
realRigidbody.SweepTestAll(direction, max_distance, query_trigger_interaction))
: new IsoRaycastHit[0];
}

View File

@@ -5,7 +5,7 @@ using IsoTools.Internal;
using UnityEditor;
#endif
namespace IsoTools {
namespace IsoTools.Physics {
[RequireComponent(typeof(IsoObject))]
public class IsoSphereCollider : IsoCollider {

View File

@@ -1,10 +1,10 @@
using UnityEngine;
using IsoTools.Internal;
using IsoTools.Physics.Internal;
namespace IsoTools {
namespace IsoTools.Physics {
[DisallowMultipleComponent]
[RequireComponent(typeof(IsoObject))]
public class IsoTriggerListener : IsoPhysicHelperHolder {
public class IsoTriggerListener : IsoPhysicsHelperHolder {
IsoFakeTriggerListener _fakeListener;

View File

@@ -1,4 +1,5 @@
using UnityEngine;
using IsoTools.Physics;
namespace IsoTools.Examples.Dragosha {
[RequireComponent(typeof(IsoRigidbody))]

View File

@@ -1,4 +1,5 @@
using UnityEngine;
using IsoTools.Physics;
using System.Collections;
namespace IsoTools.Examples.Kenney {

View File

@@ -1,4 +1,5 @@
using UnityEngine;
using IsoTools.Physics;
using System.Collections;
namespace IsoTools.Examples.Kenney {
@@ -8,7 +9,7 @@ namespace IsoTools.Examples.Kenney {
if ( iso_world && Input.GetMouseButtonDown(0) ) {
var iso_mouse_pos = iso_world.MouseIsoPosition();
var ray_from_iso_camera = iso_world.RayFromIsoCameraToIsoPoint(iso_mouse_pos);
var hits = iso_world.RaycastAll(ray_from_iso_camera);
var hits = IsoPhysics.RaycastAll(ray_from_iso_camera);
for ( var i = 0; i < hits.Length; ++i ) {
var alien_go = hits[i].collider.gameObject;
if ( alien_go.GetComponent<AlienBallController>() ) {

View File

@@ -1,4 +1,5 @@
using UnityEngine;
using IsoTools.Physics;
using System.Collections;
namespace IsoTools.Examples.Kenney {

View File

@@ -1,4 +1,5 @@
using UnityEngine;
using IsoTools.Physics;
namespace IsoTools.Examples.Kenney {
[RequireComponent(typeof(IsoRigidbody))]

View File

@@ -1,25 +0,0 @@
using UnityEngine;
using System.Collections.Generic;
namespace IsoTools.Internal {
public class IsoPhysicHelperHolder : MonoBehaviour {
static List<IsoPhysicHelperHolder> _tmpHolders = new List<IsoPhysicHelperHolder>(7);
protected GameObject fakeObject {
get { return physicHelper.isoFakeObject; }
}
protected IsoPhysicHelper physicHelper {
get { return IsoUtils.GetOrCreateComponent<IsoPhysicHelper>(gameObject); }
}
protected void DestroyUnnecessaryCheck() {
GetComponents<IsoPhysicHelperHolder>(_tmpHolders);
if ( _tmpHolders.Count == 1 && _tmpHolders[0] == this ) {
Destroy(physicHelper);
}
_tmpHolders.Clear();
}
}
}