修改水

This commit is contained in:
2026-01-01 22:00:33 +08:00
parent 040a222bd6
commit 9ceffccd39
1800 changed files with 103929 additions and 139495 deletions

View File

@@ -10,7 +10,7 @@ namespace Obi{
this.collider = collider;
}
public override void UpdateIfNeeded (){
public override bool UpdateIfNeeded (){
BoxCollider2D box = collider as BoxCollider2D;
@@ -19,14 +19,12 @@ namespace Obi{
// update collider:
var shape = world.colliderShapes[index];
shape.is2D = true;
shape.is2D = 1;
shape.type = ColliderShape.ShapeType.Box;
shape.filter = source.Filter;
shape.SetSign(source.Inverted);
shape.isTrigger = box.isTrigger;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.Handle.index : -1;
shape.flags = box.isTrigger ? 1 : 0;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.forceZoneIndex = source.ForceZone != null ? source.ForceZone.Handle.index : -1;
shape.contactOffset = source.Thickness + box.edgeRadius;
shape.center = box.offset;
shape.size = box.size;
@@ -39,8 +37,9 @@ namespace Obi{
// update transform:
var trfm = world.colliderTransforms[index];
trfm.FromTransform2D(box.transform, source.Rigidbody as ObiRigidbody2D);
trfm.FromTransform(box.transform, true);
world.colliderTransforms[index] = trfm;
return false;
}
}

View File

@@ -11,7 +11,7 @@ namespace Obi{
this.collider = collider;
}
public override void UpdateIfNeeded ()
public override bool UpdateIfNeeded ()
{
CapsuleCollider2D capsule = collider as CapsuleCollider2D;
@@ -21,14 +21,12 @@ namespace Obi{
// update collider:
var shape = world.colliderShapes[index];
shape.is2D = true;
shape.is2D = 1;
shape.type = ColliderShape.ShapeType.Capsule;
shape.filter = source.Filter;
shape.SetSign(source.Inverted);
shape.isTrigger = capsule.isTrigger;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.Handle.index : -1;
shape.flags = capsule.isTrigger ? 1 : 0;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.forceZoneIndex = source.ForceZone != null ? source.ForceZone.Handle.index : -1;
shape.contactOffset = source.Thickness;
shape.center = capsule.offset;
Vector2 size = capsule.size;
@@ -44,8 +42,10 @@ namespace Obi{
// update transform:
var trfm = world.colliderTransforms[index];
trfm.FromTransform2D(capsule.transform, source.Rigidbody as ObiRigidbody2D);
trfm.FromTransform(capsule.transform,true);
world.colliderTransforms[index] = trfm;
return false;
}
}

View File

@@ -12,7 +12,7 @@ namespace Obi{
this.collider = collider;
}
public override void UpdateIfNeeded ()
public override bool UpdateIfNeeded ()
{
CircleCollider2D sphere = collider as CircleCollider2D;
@@ -23,14 +23,12 @@ namespace Obi{
// update collider:
var shape = world.colliderShapes[index];
shape.is2D = true;
shape.is2D = 1;
shape.type = ColliderShape.ShapeType.Sphere;
shape.filter = source.Filter;
shape.SetSign(source.Inverted);
shape.isTrigger = sphere.isTrigger;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.Handle.index : -1;
shape.flags = sphere.isTrigger ? 1 : 0;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.forceZoneIndex = source.ForceZone != null ? source.ForceZone.Handle.index : -1;
shape.contactOffset = source.Thickness;
shape.center = sphere.offset;
shape.size = Vector3.one * sphere.radius;
@@ -43,8 +41,10 @@ namespace Obi{
// update transform:
var trfm = world.colliderTransforms[index];
trfm.FromTransform2D(sphere.transform, source.Rigidbody as ObiRigidbody2D);
trfm.FromTransform(sphere.transform,true);
world.colliderTransforms[index] = trfm;
return true;
}
}

View File

@@ -19,7 +19,7 @@ namespace Obi{
ObiColliderWorld.GetInstance().DestroyEdgeMesh(handle);
}
public override void UpdateIfNeeded (){
public override bool UpdateIfNeeded (){
EdgeCollider2D edgeCollider = collider as EdgeCollider2D;
@@ -36,14 +36,12 @@ namespace Obi{
// update collider:
var shape = world.colliderShapes[index];
shape.is2D = true;
shape.is2D = 1;
shape.type = ColliderShape.ShapeType.EdgeMesh;
shape.filter = source.Filter;
shape.SetSign(source.Inverted);
shape.isTrigger = edgeCollider.isTrigger;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.Handle.index : -1;
shape.flags = edgeCollider.isTrigger ? 1 : 0;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.forceZoneIndex = source.ForceZone != null ? source.ForceZone.Handle.index : -1;
shape.center = edgeCollider.offset;
shape.contactOffset = source.Thickness + edgeCollider.edgeRadius;
shape.dataIndex = handle.index;
@@ -56,8 +54,10 @@ namespace Obi{
// update transform:
var trfm = world.colliderTransforms[index];
trfm.FromTransform2D(edgeCollider.transform, source.Rigidbody as ObiRigidbody2D);
trfm.FromTransform(edgeCollider.transform, true);
world.colliderTransforms[index] = trfm;
return true;
}
public override void Destroy()

View File

@@ -12,10 +12,18 @@ namespace Obi{
this.collider = collider;
}
public override void UpdateIfNeeded (){
public override bool UpdateIfNeeded (){
BoxCollider box = collider as BoxCollider;
/*if (box != null && (box.size != size || box.center != center)){
size = box.size;
center = box.center;
adaptor.Set(center, size);
Oni.UpdateShape(oniShape,ref adaptor);
return true;
}*/
// retrieve collision world and index:
var world = ObiColliderWorld.GetInstance();
int index = source.Handle.index;
@@ -24,11 +32,9 @@ namespace Obi{
var shape = world.colliderShapes[index];
shape.type = ColliderShape.ShapeType.Box;
shape.filter = source.Filter;
shape.SetSign(source.Inverted);
shape.isTrigger = box.isTrigger;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.Handle.index : -1;
shape.flags = box.isTrigger ? 1 : 0;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.forceZoneIndex = source.ForceZone != null ? source.ForceZone.Handle.index : -1;
shape.contactOffset = source.Thickness;
shape.center = box.center;
shape.size = box.size;
@@ -41,9 +47,10 @@ namespace Obi{
// update transform:
var trfm = world.colliderTransforms[index];
trfm.FromTransform3D(box.transform, source.Rigidbody as ObiRigidbody);
trfm.FromTransform(box.transform);
world.colliderTransforms[index] = trfm;
return true;
}
}

View File

@@ -11,7 +11,7 @@ namespace Obi{
this.source = source;
}
public override void UpdateIfNeeded (){
public override bool UpdateIfNeeded (){
CapsuleCollider capsule = collider as CapsuleCollider;
@@ -23,11 +23,9 @@ namespace Obi{
var shape = world.colliderShapes[index];
shape.type = ColliderShape.ShapeType.Capsule;
shape.filter = source.Filter;
shape.SetSign(source.Inverted);
shape.isTrigger = capsule.isTrigger;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.Handle.index : -1;
shape.flags = capsule.isTrigger ? 1 : 0;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.forceZoneIndex = source.ForceZone != null ? source.ForceZone.Handle.index : -1;
shape.contactOffset = source.Thickness;
shape.center = capsule.center;
shape.size = new Vector4(capsule.radius, capsule.height, capsule.direction, 0);
@@ -40,8 +38,10 @@ namespace Obi{
// update transform:
var trfm = world.colliderTransforms[index];
trfm.FromTransform3D(capsule.transform, source.Rigidbody as ObiRigidbody);
trfm.FromTransform(capsule.transform);
world.colliderTransforms[index] = trfm;
return true;
}
}

View File

@@ -13,7 +13,7 @@ namespace Obi
this.source = source;
}
public override void UpdateIfNeeded()
public override bool UpdateIfNeeded()
{
CharacterController character = collider as CharacterController;
@@ -26,11 +26,9 @@ namespace Obi
var shape = world.colliderShapes[index];
shape.type = ColliderShape.ShapeType.Capsule;
shape.filter = source.Filter;
shape.SetSign(source.Inverted);
shape.isTrigger = character.isTrigger;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.Handle.index : -1;
shape.flags = character.isTrigger ? 1 : 0;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.forceZoneIndex = source.ForceZone != null ? source.ForceZone.Handle.index : -1;
shape.contactOffset = source.Thickness;
shape.center = character.center;
shape.size = new Vector4(character.radius, character.height, 1, 0);
@@ -43,8 +41,10 @@ namespace Obi
// update transform:
var trfm = world.colliderTransforms[index];
trfm.FromTransform3D(character.transform, source.Rigidbody as ObiRigidbody);
trfm.FromTransform(character.transform);
world.colliderTransforms[index] = trfm;
return true;
}
}

View File

@@ -26,7 +26,7 @@ namespace Obi{
ObiColliderWorld.GetInstance().DestroyDistanceField(handle);
}
public override void UpdateIfNeeded ()
public override bool UpdateIfNeeded ()
{
bool trigger = false;
@@ -55,11 +55,9 @@ namespace Obi{
var shape = world.colliderShapes[index];
shape.type = ColliderShape.ShapeType.SignedDistanceField;
shape.filter = source.Filter;
shape.SetSign(source.Inverted);
shape.isTrigger = trigger;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.Handle.index : -1;
shape.flags = trigger ? 1 : 0;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.forceZoneIndex = source.ForceZone != null ? source.ForceZone.Handle.index : -1;
shape.contactOffset = source.Thickness;
shape.dataIndex = handle.index;
world.colliderShapes[index] = shape;
@@ -71,8 +69,10 @@ namespace Obi{
// update transform:
var trfm = world.colliderTransforms[index];
trfm.FromTransform3D(source.transform, source.Rigidbody as ObiRigidbody);
trfm.FromTransform(source.transform);
world.colliderTransforms[index] = trfm;
return true;
}
public override void Destroy()

View File

@@ -9,14 +9,6 @@ namespace Obi{
{
ObiTriangleMeshHandle handle;
public Mesh targetMesh
{
get {
var mc = collider as MeshCollider;
return mc?.sharedMesh;
}
}
public ObiMeshShapeTracker(ObiCollider source, MeshCollider collider){
this.source = source;
@@ -31,7 +23,7 @@ namespace Obi{
ObiColliderWorld.GetInstance().DestroyTriangleMesh(handle);
}
public override void UpdateIfNeeded ()
public override bool UpdateIfNeeded ()
{
MeshCollider meshCollider = collider as MeshCollider;
@@ -59,11 +51,9 @@ namespace Obi{
var shape = world.colliderShapes[index];
shape.type = ColliderShape.ShapeType.TriangleMesh;
shape.filter = source.Filter;
shape.SetSign(source.Inverted);
shape.isTrigger = meshCollider.isTrigger;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.Handle.index : -1;
shape.flags = meshCollider.isTrigger ? 1 : 0;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.forceZoneIndex = source.ForceZone != null ? source.ForceZone.Handle.index : -1;
shape.contactOffset = source.Thickness;
shape.dataIndex = handle.index;
world.colliderShapes[index] = shape;
@@ -75,8 +65,10 @@ namespace Obi{
// update transform:
var trfm = world.colliderTransforms[index];
trfm.FromTransform3D(meshCollider.transform, source.Rigidbody as ObiRigidbody);
trfm.FromTransform(meshCollider.transform);
world.colliderTransforms[index] = trfm;
return true;
}
public override void Destroy()

View File

@@ -11,7 +11,7 @@ namespace Obi{
public virtual void Destroy(){
}
public abstract void UpdateIfNeeded ();
public abstract bool UpdateIfNeeded ();
}

View File

@@ -12,37 +12,59 @@ namespace Obi{
this.collider = collider;
}
public override void UpdateIfNeeded()
public override bool UpdateIfNeeded()
{
SphereCollider sphere = collider as SphereCollider;
// retrieve collision world and index:
var world = ObiColliderWorld.GetInstance();
int index = source.Handle.index;
// TODO: testing for changes here is not needed? all we do is set variables...
//if (sphere != null && (sphere.radius != radius || sphere.center != center))
{
//radius = sphere.radius;
//center = sphere.center;
// update collider:
var shape = world.colliderShapes[index];
shape.type = ColliderShape.ShapeType.Sphere;
shape.filter = source.Filter;
shape.SetSign(source.Inverted);
shape.isTrigger = sphere.isTrigger;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.Handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.forceZoneIndex = source.ForceZone != null ? source.ForceZone.Handle.index : -1;
shape.contactOffset = source.Thickness;
shape.center = sphere.center;
shape.size = Vector3.one * sphere.radius;
world.colliderShapes[index] = shape;
// retrieve collision world and index:
var world = ObiColliderWorld.GetInstance();
int index = source.Handle.index;
// update bounds:
var aabb = world.colliderAabbs[index];
aabb.FromBounds(sphere.bounds, shape.contactOffset);
world.colliderAabbs[index] = aabb;
// update collider:
var shape = world.colliderShapes[index];
shape.type = ColliderShape.ShapeType.Sphere;
shape.filter = source.Filter;
shape.flags = sphere.isTrigger ? 1 : 0;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.contactOffset = source.Thickness;
shape.center = sphere.center;
shape.size = Vector3.one * sphere.radius;
world.colliderShapes[index] = shape;
// update transform:
var trfm = world.colliderTransforms[index];
trfm.FromTransform3D(sphere.transform, source.Rigidbody as ObiRigidbody);
world.colliderTransforms[index] = trfm;
// update bounds:
var aabb = world.colliderAabbs[index];
aabb.FromBounds(sphere.bounds, shape.contactOffset);
world.colliderAabbs[index] = aabb;
// update transform:
var trfm = world.colliderTransforms[index];
trfm.FromTransform(sphere.transform);
world.colliderTransforms[index] = trfm;
/*var shape = source.colliderWorld.colliderShapes[source.shapeHandle.index];
// update the transform
shape.Set(collider as Collider, source.Phase, source.Thickness);
// update the shape:
shape.SetSphere(sphere.center, sphere.radius);
source.colliderWorld.colliderShapes[source.shapeHandle.index] = shape;*/
//adaptor.Set(center, radius);
//Oni.UpdateShape(oniShape,ref adaptor);
return true;
}
//return false;
}
}

View File

@@ -6,6 +6,7 @@ namespace Obi{
public class ObiTerrainShapeTracker : ObiShapeTracker
{
private bool heightmapDataHasChanged = false;
ObiHeightFieldHandle handle;
public ObiTerrainShapeTracker(ObiCollider source, TerrainCollider collider){
@@ -19,7 +20,7 @@ namespace Obi{
ObiColliderWorld.GetInstance().DestroyHeightField(handle);
}
public override void UpdateIfNeeded ()
public override bool UpdateIfNeeded ()
{
TerrainCollider terrain = collider as TerrainCollider;
@@ -41,11 +42,9 @@ namespace Obi{
var shape = world.colliderShapes[index];
shape.type = ColliderShape.ShapeType.Heightmap;
shape.filter = source.Filter;
shape.SetSign(source.Inverted);
shape.isTrigger = terrain.isTrigger;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.Handle.index : -1;
shape.flags = terrain.isTrigger ? 1 : 0;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.forceZoneIndex = source.ForceZone != null ? source.ForceZone.Handle.index : -1;
shape.contactOffset = source.Thickness;
shape.dataIndex = handle.index;
shape.size = terrain.terrainData.size;
@@ -59,8 +58,10 @@ namespace Obi{
// update transform:
var trfm = world.colliderTransforms[index];
trfm.FromTransform3D(terrain.transform, source.Rigidbody as ObiRigidbody);
trfm.FromTransform(terrain.transform);
world.colliderTransforms[index] = trfm;
return true;
}
public override void Destroy()

View File

@@ -30,8 +30,9 @@ namespace Obi
return;
}
RemoveCollider();
m_SourceCollider = value;
RemoveCollider();
AddCollider();
}

View File

@@ -13,9 +13,6 @@ namespace Obi
[SerializeProperty("Thickness")]
[SerializeField] private float thickness = 0;
[SerializeProperty("Inverted")]
[SerializeField] private bool inverted = false;
[SerializeProperty("CollisionMaterial")]
[SerializeField] private ObiCollisionMaterial material;
@@ -25,11 +22,7 @@ namespace Obi
{
set
{
if (material != value)
{
material = value;
ForceUpdate();
}
material = value;
}
get { return material; }
}
@@ -41,7 +34,7 @@ namespace Obi
if (filter != value)
{
filter = value;
ForceUpdate();
dirty = true;
}
}
get { return filter; }
@@ -54,25 +47,12 @@ namespace Obi
if (!Mathf.Approximately(thickness, value))
{
thickness = value;
ForceUpdate();
dirty = true;
}
}
get { return thickness; }
}
public bool Inverted
{
set
{
if (inverted != value)
{
inverted = value;
ForceUpdate();
}
}
get { return inverted; }
}
public ObiShapeTracker Tracker
{
get { return tracker; }
@@ -88,9 +68,15 @@ namespace Obi
}
}
public ObiForceZone ForceZone
public IntPtr OniCollider
{
get; set;
get
{
if (oniCollider == IntPtr.Zero)
FindSourceCollider();
return oniCollider;
}
}
public ObiRigidbodyBase Rigidbody
@@ -99,8 +85,11 @@ namespace Obi
}
protected ObiColliderHandle shapeHandle;
protected IntPtr oniCollider;
protected ObiRigidbodyBase obiRigidbody;
protected bool wasUnityColliderEnabled = true;
protected bool dirty = false;
protected ObiShapeTracker tracker; /**< tracker object used to determine when to update the collider's shape*/
@@ -151,6 +140,7 @@ namespace Obi
protected void AddCollider()
{
Component unityCollider = GetUnityCollider(ref wasUnityColliderEnabled);
if (unityCollider != null && (shapeHandle == null || !shapeHandle.isValid))
@@ -164,6 +154,7 @@ namespace Obi
// Create rigidbody if necessary, and link ourselves to it:
CreateRigidbody();
}
}
protected void RemoveCollider()
@@ -178,15 +169,6 @@ namespace Obi
}
}
/**
* Flags the collider as needing to be updated from now on. If the object it's on has any editor static flag enabled,
* the collider will be again flagged as *not* needing to be updated after its next update.
*/
public void ForceUpdate()
{
ObiColliderWorld.GetInstance().MarkColliderAsNeedingUpdate(shapeHandle);
}
/**
* Check if the collider transform or its shape have changed any relevant property, and update their Oni counterparts.
*/
@@ -194,18 +176,14 @@ namespace Obi
{
bool unityColliderEnabled = false;
Component unityCollider = GetUnityCollider(ref unityColliderEnabled);
var colliderWorld = ObiColliderWorld.GetInstance();
if (unityCollider != null)
{
// Only if this object is not static:
if (tracker != null) //&& needsUpdate)
{
// no need to test for changes, all we are doing is setting some variables here.
if (tracker != null)
tracker.UpdateIfNeeded();
}
// check isStatic, *after* updating the tracker at least once.
if (unityCollider.gameObject.isStatic)
ObiColliderWorld.GetInstance().MarkColliderAsNotNeedingUpdate(shapeHandle);
}
// If the unity collider is null but its handle is valid, the unity collider has been destroyed.
else if (shapeHandle != null && shapeHandle.isValid)

View File

@@ -44,10 +44,6 @@ namespace Obi
{
public ObiColliderHandle(int index = -1) : base(index) { }
}
public class ObiForceZoneHandle : ObiResourceHandle<ObiForceZone>
{
public ObiForceZoneHandle(int index = -1) : base(index) { }
}
public class ObiCollisionMaterialHandle : ObiResourceHandle<ObiCollisionMaterial>
{
public ObiCollisionMaterialHandle(int index = -1) : base(index) { }
@@ -57,6 +53,7 @@ namespace Obi
public ObiRigidbodyHandle(int index = -1) : base(index) { }
}
[ExecuteInEditMode]
public class ObiColliderWorld
{
[NonSerialized] public List<IColliderWorldImpl> implementations;
@@ -66,10 +63,7 @@ namespace Obi
[NonSerialized] public ObiNativeAabbList colliderAabbs; // list of collider bounds.
[NonSerialized] public ObiNativeAffineTransformList colliderTransforms; // list of collider transforms.
[NonSerialized] public List<ObiForceZoneHandle> forceZoneHandles; // list of collider handles, used by ObiForceZone components to retrieve them.
[NonSerialized] public ObiNativeForceZoneList forceZones; // list of collider force zones.
[NonSerialized] public List<ObiCollisionMaterialHandle> materialHandles; // list of material handles, used by ObiCollisionMaterial components to retrieve them.
[NonSerialized] public List<ObiCollisionMaterialHandle> materialHandles;
[NonSerialized] public ObiNativeCollisionMaterialList collisionMaterials; // list of collision materials.
[NonSerialized] public List<ObiRigidbodyHandle> rigidbodyHandles; // list of rigidbody handles, used by ObiRigidbody components to retrieve them.
@@ -80,19 +74,6 @@ namespace Obi
[NonSerialized] public ObiDistanceFieldContainer distanceFieldContainer;
[NonSerialized] public ObiHeightFieldContainer heightFieldContainer;
private List<ObiColliderHandle> collidersToCreate;
private List<ObiColliderHandle> collidersToDestroy;
private List<ObiForceZoneHandle> forceZonesToCreate;
private List<ObiForceZoneHandle> forceZonesToDestroy;
private List<ObiRigidbodyHandle> rigidbodiesToCreate;
private List<ObiRigidbodyHandle> rigidbodiesToDestroy;
public int collidersToUpdateCount { private set; get; } = 0; // amount of colliders that need to be updated. These are always grouped at the start of the collider arrays (handles, shapes, etc).
private bool dirty = false;
private static ObiColliderWorld instance;
public static ObiColliderWorld GetInstance()
@@ -120,11 +101,6 @@ namespace Obi
if (colliderTransforms == null)
colliderTransforms = new ObiNativeAffineTransformList();
if (forceZoneHandles == null)
forceZoneHandles = new List<ObiForceZoneHandle>();
if (forceZones == null)
forceZones = new ObiNativeForceZoneList();
if (materialHandles == null)
materialHandles = new List<ObiCollisionMaterialHandle>();
if (collisionMaterials == null)
@@ -143,29 +119,13 @@ namespace Obi
distanceFieldContainer = new ObiDistanceFieldContainer();
if (heightFieldContainer == null)
heightFieldContainer = new ObiHeightFieldContainer();
if (collidersToCreate == null)
collidersToCreate = new List<ObiColliderHandle>();
if (collidersToDestroy == null)
collidersToDestroy = new List<ObiColliderHandle>();
if (forceZonesToCreate == null)
forceZonesToCreate = new List<ObiForceZoneHandle>();
if (forceZonesToDestroy == null)
forceZonesToDestroy = new List<ObiForceZoneHandle>();
if (rigidbodiesToCreate == null)
rigidbodiesToCreate = new List<ObiRigidbodyHandle>();
if (rigidbodiesToDestroy == null)
rigidbodiesToDestroy = new List<ObiRigidbodyHandle>();
}
private void Destroy()
{
dirty = false;
for (int i = 0; i < implementations.Count; ++i)
{
implementations[i].SetColliders(colliderShapes, colliderAabbs, colliderTransforms);
implementations[i].SetColliders(colliderShapes, colliderAabbs, colliderTransforms, 0);
implementations[i].UpdateWorld(0);
}
@@ -182,48 +142,41 @@ namespace Obi
foreach (var handle in materialHandles)
handle.Invalidate();
if (forceZoneHandles != null)
foreach (var handle in forceZoneHandles)
handle.Invalidate();
// Dispose of all lists:
implementations = null;
colliderHandles = null;
rigidbodyHandles = null;
materialHandles = null;
forceZoneHandles = null;
collidersToCreate = null;
collidersToDestroy = null;
forceZonesToCreate = null;
forceZonesToDestroy = null;
rigidbodiesToCreate = null;
rigidbodiesToDestroy = null;
if (colliderShapes != null)
colliderShapes.Dispose();
if (colliderAabbs != null)
colliderAabbs.Dispose();
if (colliderTransforms != null)
colliderTransforms.Dispose();
colliderShapes?.Dispose();
colliderAabbs?.Dispose();
colliderTransforms?.Dispose();
forceZones?.Dispose();
collisionMaterials?.Dispose();
rigidbodies?.Dispose();
if (collisionMaterials != null)
collisionMaterials.Dispose();
triangleMeshContainer?.Dispose();
edgeMeshContainer?.Dispose();
distanceFieldContainer?.Dispose();
heightFieldContainer?.Dispose();
if (rigidbodies != null)
rigidbodies.Dispose();
if (triangleMeshContainer != null)
triangleMeshContainer.Dispose();
if (edgeMeshContainer != null)
edgeMeshContainer.Dispose();
if (distanceFieldContainer != null)
distanceFieldContainer.Dispose();
if (heightFieldContainer != null)
heightFieldContainer.Dispose();
instance = null;
}
private void DestroyIfUnused()
{
// when there is no data and no implementations, the world gets destroyed.
// don't check materialHandles.Count == 0, as these are scriptable objects and may outlive the world.
if (colliderHandles.Count == 0 &&
rigidbodyHandles.Count == 0 &&
forceZoneHandles.Count == 0 &&
implementations.Count == 0)
// when there are no implementations and no colliders, the world gets destroyed.
if (colliderHandles.Count == 0 && rigidbodyHandles.Count == 0 && materialHandles.Count == 0 && implementations.Count == 0)
Destroy();
}
@@ -241,39 +194,22 @@ namespace Obi
public ObiColliderHandle CreateCollider()
{
var handle = new ObiColliderHandle();
var handle = new ObiColliderHandle(colliderHandles.Count);
colliderHandles.Add(handle);
// in-editor, we create data right away since the simulation is not running.
if (!Application.isPlaying)
CreateColliderData(handle);
else
collidersToCreate.Add(handle);
return handle;
}
public ObiForceZoneHandle CreateForceZone()
{
var handle = new ObiForceZoneHandle();
// in-editor, we create data right away since the simulation is not running.
if (!Application.isPlaying)
CreateForceZoneData(handle);
else
forceZonesToCreate.Add(handle);
colliderShapes.Add(new ColliderShape() { materialIndex = -1, rigidbodyIndex = -1});
colliderAabbs.Add(new Aabb());
colliderTransforms.Add(new AffineTransform());
return handle;
}
public ObiRigidbodyHandle CreateRigidbody()
{
var handle = new ObiRigidbodyHandle();
var handle = new ObiRigidbodyHandle(rigidbodyHandles.Count);
rigidbodyHandles.Add(handle);
// in-editor, we create data right away since the simulation is not running.
if (!Application.isPlaying)
CreateRigidbodyData(handle);
else
rigidbodiesToCreate.Add(handle);
rigidbodies.Add(new ColliderRigidbody());
return handle;
}
@@ -330,41 +266,64 @@ namespace Obi
public void DestroyCollider(ObiColliderHandle handle)
{
// Destroy data right away if no simulation is running.
if (!Application.isPlaying || implementations.Count == 0)
DestroyColliderData(handle);
else
if (colliderShapes != null && handle != null && handle.isValid && handle.index < colliderHandles.Count)
{
// In case the handle is in the creation queue, just remove it.
if (!collidersToCreate.Remove(handle))
collidersToDestroy.Add(handle);
}
}
int index = handle.index;
int lastIndex = colliderHandles.Count - 1;
public void DestroyForceZone(ObiForceZoneHandle handle)
{
// Destroy data right away if no simulation is running.
if (!Application.isPlaying || implementations.Count == 0)
DestroyForceZoneData(handle);
else
{
// In case the handle is in the creation queue, just remove it.
if (!forceZonesToCreate.Remove(handle))
forceZonesToDestroy.Add(handle);
// swap all collider info:
colliderHandles.Swap(index, lastIndex);
colliderShapes.Swap(index, lastIndex);
colliderAabbs.Swap(index, lastIndex);
colliderTransforms.Swap(index, lastIndex);
// update the index of the handle we swapped with:
colliderHandles[index].index = index;
// invalidate our handle:
// (after updating the swapped one!
// in case there's just one handle in the array,
// we need to write -1 after 0)
handle.Invalidate();
// remove last index:
colliderHandles.RemoveAt(lastIndex);
colliderShapes.count--;
colliderAabbs.count--;
colliderTransforms.count--;
DestroyIfUnused();
}
}
public void DestroyRigidbody(ObiRigidbodyHandle handle)
{
// Destroy data right away if no simulation is running.
if (!Application.isPlaying || implementations.Count == 0)
DestroyRigidbodyData(handle);
else
if (rigidbodies != null && handle != null && handle.isValid && handle.index < rigidbodyHandles.Count)
{
// In case the handle is in the creation queue, just remove it.
if (!rigidbodiesToCreate.Remove(handle))
rigidbodiesToDestroy.Add(handle);
int index = handle.index;
int lastIndex = rigidbodyHandles.Count - 1;
// swap all collider info:
rigidbodyHandles.Swap(index, lastIndex);
rigidbodies.Swap(index, lastIndex);
// update the index of the handle we swapped with:
rigidbodyHandles[index].index = index;
// invalidate our handle:
// (after updating the swapped one!
// in case there's just one handle in the array,
// we need to write -1 after 0)
handle.Invalidate();
// remove last index:
rigidbodyHandles.RemoveAt(lastIndex);
rigidbodies.count--;
DestroyIfUnused();
}
}
public void DestroyCollisionMaterial(ObiCollisionMaterialHandle handle)
@@ -395,309 +354,80 @@ namespace Obi
}
}
private void DestroyColliderData (ObiColliderHandle handle)
public void UpdateColliders()
{
if (colliderShapes != null && handle != null && handle.isValid && handle.index < colliderHandles.Count)
{
int index = handle.index;
int lastIndex = colliderHandles.Count - 1;
// swap all collider info:
colliderHandles.Swap(index, lastIndex);
colliderShapes.Swap(index, lastIndex);
colliderAabbs.Swap(index, lastIndex);
colliderTransforms.Swap(index, lastIndex);
// update the index of the handle we swapped with:
colliderHandles[index].index = index;
// invalidate our handle:
// (after updating the swapped one!
// in case there's just one handle in the array,
// we need to write -1 after 0)
handle.Invalidate();
// remove last index:
colliderHandles.RemoveAt(lastIndex);
colliderShapes.count--;
colliderAabbs.count--;
colliderTransforms.count--;
// force all colliders to update next frame, as the index of the data they reference
// (eg the mesh in a MeshCollider) may have changed as a result of deleting this collider's data.
collidersToUpdateCount = colliderHandles.Count;
DestroyIfUnused();
}
}
private void DestroyForceZoneData(ObiForceZoneHandle handle)
{
if (forceZones != null && handle != null && handle.isValid && handle.index < forceZoneHandles.Count)
{
int index = handle.index;
int lastIndex = forceZoneHandles.Count - 1;
// swap all force zone info:
forceZoneHandles.Swap(index, lastIndex);
forceZones.Swap(index, lastIndex);
// update the index of the handle we swapped with:
forceZoneHandles[index].index = index;
// invalidate our handle:
// (after updating the swapped one!
// in case there's just one handle in the array,
// we need to write -1 after 0)
handle.Invalidate();
// remove last index:
forceZoneHandles.RemoveAt(lastIndex);
forceZones.count--;
DestroyIfUnused();
}
}
private void DestroyRigidbodyData(ObiRigidbodyHandle handle)
{
if (rigidbodies != null && handle != null && handle.isValid && handle.index < rigidbodyHandles.Count)
{
int index = handle.index;
int lastIndex = rigidbodyHandles.Count - 1;
// swap all collider info:
rigidbodyHandles.Swap(index, lastIndex);
rigidbodies.Swap(index, lastIndex);
// update the index of the handle we swapped with:
rigidbodyHandles[index].index = index;
// invalidate our handle:
// (after updating the swapped one!
// in case there's just one handle in the array,
// we need to write -1 after 0)
handle.Invalidate();
// remove last index:
rigidbodyHandles.RemoveAt(lastIndex);
rigidbodies.count--;
DestroyIfUnused();
}
}
private void CreateColliderData(ObiColliderHandle handle)
{
handle.index = colliderHandles.Count;
colliderHandles.Add(handle);
colliderShapes.Add(new ColliderShape { materialIndex = -1, rigidbodyIndex = -1, dataIndex = -1 });
colliderAabbs.Add(new Aabb());
colliderTransforms.Add(new AffineTransform());
MarkColliderAsNeedingUpdate(handle);
}
private void CreateForceZoneData(ObiForceZoneHandle handle)
{
handle.index = forceZoneHandles.Count;
forceZoneHandles.Add(handle);
forceZones.Add(new ForceZone());
}
private void CreateRigidbodyData(ObiRigidbodyHandle handle)
{
handle.index = rigidbodyHandles.Count;
rigidbodyHandles.Add(handle);
rigidbodies.Add(new ColliderRigidbody());
}
public bool DoesColliderRequireToBeUpdated(ObiColliderHandle handle)
{
if (handle != null && handle.isValid &&
handle.index < colliderHandles.Count && handle.index < collidersToUpdateCount)
return true;
return false;
}
public void MarkColliderAsNeedingUpdate(ObiColliderHandle handle)
{
if (colliderShapes != null && handle != null && handle.isValid &&
handle.index < colliderHandles.Count && handle.index >= collidersToUpdateCount &&
collidersToUpdateCount < colliderHandles.Count)
{
int index = handle.index;
int lastIndex = collidersToUpdateCount;
// swap all collider info:
colliderHandles.Swap(index, lastIndex);
colliderShapes.Swap(index, lastIndex);
colliderAabbs.Swap(index, lastIndex);
colliderTransforms.Swap(index, lastIndex);
// update handles:
colliderHandles[lastIndex].index = lastIndex;
colliderHandles[index].index = index;
collidersToUpdateCount++;
}
}
public void MarkColliderAsNotNeedingUpdate(ObiColliderHandle handle)
{
if (colliderShapes != null && handle != null && handle.isValid &&
handle.index < colliderHandles.Count && handle.index < collidersToUpdateCount)
{
int index = handle.index;
int lastIndex = collidersToUpdateCount-1;
// swap all collider info:
colliderHandles.Swap(index, lastIndex);
colliderShapes.Swap(index, lastIndex);
colliderAabbs.Swap(index, lastIndex);
colliderTransforms.Swap(index, lastIndex);
// update handles:
colliderHandles[lastIndex].index = lastIndex;
colliderHandles[index].index = index;
collidersToUpdateCount--;
}
}
public void FlushHandleBuffers()
{
// First process destruction, then process creation.
// In case we create a handle and then destroy it,
// we should enqueue it for destruction only if it's not in the creation queue.
// If it is, just remove if from the creation queue.
if (collidersToDestroy != null)
{
foreach (var handle in collidersToDestroy)
DestroyColliderData(handle);
collidersToDestroy?.Clear();
}
if (forceZonesToDestroy != null)
{
foreach (var handle in forceZonesToDestroy)
DestroyForceZoneData(handle);
forceZonesToDestroy?.Clear();
}
if (rigidbodiesToDestroy != null)
{
foreach (var handle in rigidbodiesToDestroy)
DestroyRigidbodyData(handle);
rigidbodiesToDestroy?.Clear();
}
if (collidersToCreate != null)
{
foreach (var handle in collidersToCreate)
CreateColliderData(handle);
collidersToCreate?.Clear();
}
if (forceZonesToCreate != null)
{
foreach (var handle in forceZonesToCreate)
CreateForceZoneData(handle);
forceZonesToCreate?.Clear();
}
if (rigidbodiesToCreate != null)
{
foreach (var handle in rigidbodiesToCreate)
CreateRigidbodyData(handle);
rigidbodiesToCreate?.Clear();
}
}
public void UpdateWorld(float deltaTime, bool updateDynamics = true)
{
if (!dirty)
return;
dirty = false;
// ensure all objects have valid handles.
// May destroy the world if it's empty,
// so we next check that handle/implementations are not null.
FlushHandleBuffers();
// update all colliders:
if (colliderHandles != null)
for (int i = 0; i < collidersToUpdateCount; ++i)
colliderHandles[i].owner.UpdateIfNeeded();
// update all force zones:
if (forceZoneHandles != null)
for (int i = 0; i < forceZoneHandles.Count; ++i)
forceZoneHandles[i].owner.UpdateIfNeeded();
// update rigidbodies:
if (rigidbodyHandles != null && updateDynamics)
for (int i = 0; i < rigidbodyHandles.Count; ++i)
rigidbodyHandles[i].owner.UpdateIfNeeded(deltaTime);
// update implementations:
if (implementations != null)
for (int i = 0; i < implementations.Count; ++i)
{
if (implementations[i].referenceCount > 0)
{
// set arrays:
implementations[i].SetColliders(colliderShapes, colliderAabbs, colliderTransforms);
implementations[i].SetForceZones(forceZones);
implementations[i].SetRigidbodies(rigidbodies);
implementations[i].SetCollisionMaterials(collisionMaterials);
implementations[i].SetTriangleMeshData(triangleMeshContainer.headers, triangleMeshContainer.bihNodes, triangleMeshContainer.triangles, triangleMeshContainer.vertices);
implementations[i].SetEdgeMeshData(edgeMeshContainer.headers, edgeMeshContainer.bihNodes, edgeMeshContainer.edges, edgeMeshContainer.vertices);
implementations[i].SetDistanceFieldData(distanceFieldContainer.headers, distanceFieldContainer.dfNodes);
implementations[i].SetHeightFieldData(heightFieldContainer.headers, heightFieldContainer.samples);
// update world implementation:
if (updateDynamics)
implementations[i].UpdateWorld(deltaTime);
}
}
for (int i = 0; i < colliderHandles.Count; ++i)
colliderHandles[i].owner.UpdateIfNeeded();
}
public void SetDirty()
public void UpdateRigidbodies(List<ObiSolver> solvers, float stepTime)
{
dirty = true;
}
public void UpdateCollisionMaterials()
{
if (implementations != null)
for (int i = 0; i < implementations.Count; ++i)
{
if (implementations[i].referenceCount > 0)
{
implementations[i].SetCollisionMaterials(collisionMaterials);
}
}
}
public void UpdateRigidbodyVelocities(ObiSolver solver)
{
if (solver != null && solver.initialized)
// reset all solver's delta buffers to zero:
foreach (ObiSolver solver in solvers)
{
int count = Mathf.Min(rigidbodyHandles.Count, solver.rigidbodyLinearDeltas.count);
for (int i = 0; i < count; ++i)
rigidbodyHandles[i].owner.UpdateVelocities(solver.rigidbodyLinearDeltas[i], solver.rigidbodyAngularDeltas[i]);
if (solver != null && solver.initialized)
{
solver.EnsureRigidbodyArraysCapacity(rigidbodyHandles.Count);
solver.rigidbodyLinearDeltas.WipeToZero();
solver.rigidbodyAngularDeltas.WipeToZero();
}
}
solver.rigidbodyLinearDeltas.WipeToZero();
solver.rigidbodyAngularDeltas.WipeToZero();
solver.rigidbodyLinearDeltas.Upload();
solver.rigidbodyAngularDeltas.Upload();
for (int i = 0; i < rigidbodyHandles.Count; ++i)
rigidbodyHandles[i].owner.UpdateIfNeeded(stepTime);
}
public void UpdateWorld(float deltaTime)
{
for (int i = 0; i < implementations.Count; ++i)
{
if (implementations[i].referenceCount > 0)
{
// set arrays:
implementations[i].SetColliders(colliderShapes, colliderAabbs, colliderTransforms, colliderShapes.count);
implementations[i].SetRigidbodies(rigidbodies);
implementations[i].SetCollisionMaterials(collisionMaterials);
implementations[i].SetTriangleMeshData(triangleMeshContainer.headers, triangleMeshContainer.bihNodes, triangleMeshContainer.triangles, triangleMeshContainer.vertices);
implementations[i].SetEdgeMeshData(edgeMeshContainer.headers, edgeMeshContainer.bihNodes, edgeMeshContainer.edges, edgeMeshContainer.vertices);
implementations[i].SetDistanceFieldData(distanceFieldContainer.headers, distanceFieldContainer.dfNodes);
implementations[i].SetHeightFieldData(heightFieldContainer.headers, heightFieldContainer.samples);
// update world implementation:
implementations[i].UpdateWorld(deltaTime);
}
}
}
public void UpdateRigidbodyVelocities(List<ObiSolver> solvers)
{
int count = 0;
foreach (ObiSolver solver in solvers)
if (solver != null && solver.initialized) count++;
if (count > 0)
{
// we want to average the deltas applied by all solvers, so calculate 1/solverCount.
float rcpCount = 1.0f / count;
for (int i = 0; i < rigidbodyHandles.Count; ++i)
{
Vector4 linearDelta = Vector4.zero;
Vector4 angularDelta = Vector4.zero;
foreach (ObiSolver solver in solvers)
{
if (solver != null && solver.initialized)
{
linearDelta += solver.rigidbodyLinearDeltas[i] * rcpCount;
angularDelta += solver.rigidbodyAngularDeltas[i] * rcpCount;
}
}
// update rigidbody velocities
rigidbodyHandles[i].owner.UpdateVelocities(linearDelta, angularDelta);
}
}
}
}

View File

@@ -36,17 +36,17 @@ namespace Obi{
}
}
private void OnEnable()
public void OnEnable()
{
UpdateMaterial();
}
private void OnDisable()
public void OnDisable()
{
ObiColliderWorld.GetInstance().DestroyCollisionMaterial(materialHandle);
}
private void OnValidate()
public void OnValidate()
{
// we can't create GameObjects in OnValidate(), so make sure the colliderworld already exists.
UpdateMaterial();

View File

@@ -2,16 +2,17 @@
using System;
using System.Collections;
namespace Obi{
namespace Obi
{
/**
/**
* Small helper class that lets you specify Obi-only properties for rigidbodies.
*/
[ExecuteInEditMode]
[RequireComponent(typeof(Rigidbody))]
public class ObiRigidbody : ObiRigidbodyBase
{
[ExecuteInEditMode]
[RequireComponent(typeof(Rigidbody))]
public class ObiRigidbody : ObiRigidbodyBase
{
public Rigidbody unityRigidbody { get; private set; }
public Vector3 position => unityRigidbody.position;
@@ -23,26 +24,22 @@ namespace Obi{
private Quaternion prevRotation;
private Vector3 prevPosition;
protected override void OnEnable()
{
unityRigidbody = GetComponent<Rigidbody>();
ResetPosition();
base.OnEnable();
}
public void ResetPosition()
public override void OnEnable()
{
unityRigidbody = GetComponent<Rigidbody>();
prevPosition = unityRigidbody.position;
prevRotation = unityRigidbody.rotation;
linearVelocity = unityRigidbody.linearVelocity;
angularVelocity = unityRigidbody.angularVelocity;
base.OnEnable();
}
private void CacheVelocities(float stepTime)
private void UpdateVelocities(float stepTime)
{
// differentiate positions/orientations to get our own velocites for kinematic objects.
// also useful for animations.
if (unityRigidbody.isKinematic && stepTime > 0)
// when calling Physics.Simulate, MovePosition/Rotation do not work correctly. Also useful for animations.
if (unityRigidbody.isKinematic)
{
// differentiate positions to obtain linear velocity:
linearVelocity = (unityRigidbody.position - prevPosition) / stepTime;
@@ -62,31 +59,21 @@ namespace Obi{
prevRotation = unityRigidbody.rotation;
}
public override void UpdateIfNeeded(float stepTime)
public override void UpdateIfNeeded(float stepTime)
{
// Rigidbody might not exist, as rigidbody deletion is buffered.
// This means the unity rigidbody might be deleted before the rigidbody handle is invalidated.
// Conversely, rigidbodyHandle might be non-null but invalid.
if (unityRigidbody == null || !Handle.isValid) return;
CacheVelocities(stepTime);
UpdateVelocities(stepTime);
var world = ObiColliderWorld.GetInstance();
var rb = world.rigidbodies[Handle.index];
var rb = world.rigidbodies[handle.index];
rb.FromRigidbody(this);
world.rigidbodies[Handle.index] = rb;
world.rigidbodies[handle.index] = rb;
}
/**
/**
* Reads velocities back from the solver.
*/
public override void UpdateVelocities(Vector3 linearDelta, Vector3 angularDelta)
public override void UpdateVelocities(Vector3 linearDelta, Vector3 angularDelta)
{
// Rigidbody might not exist, as rigidbody deletion is buffered.
// This means the unity rigidbody might be deleted before the rigidbody handle is invalidated.
if (unityRigidbody == null) return;
// kinematic rigidbodies are passed to Obi with zero velocity, so we must ignore the new velocities calculated by the solver:
if (Application.isPlaying && !(unityRigidbody.isKinematic || kinematicForParticles))
{
@@ -97,6 +84,6 @@ namespace Obi{
}
}
}
}
}
}

View File

@@ -2,16 +2,17 @@
using System;
using System.Collections;
namespace Obi{
namespace Obi
{
/**
/**
* Small helper class that lets you specify Obi-only properties for rigidbodies.
*/
[ExecuteInEditMode]
[RequireComponent(typeof(Rigidbody2D))]
public class ObiRigidbody2D : ObiRigidbodyBase
{
[ExecuteInEditMode]
[RequireComponent(typeof(Rigidbody2D))]
public class ObiRigidbody2D : ObiRigidbodyBase
{
public Rigidbody2D unityRigidbody { get; private set; }
public Vector2 position => unityRigidbody.position;
@@ -23,26 +24,22 @@ namespace Obi{
private Quaternion prevRotation;
private Vector3 prevPosition;
protected override void OnEnable()
public override void OnEnable()
{
unityRigidbody = GetComponent<Rigidbody2D>();
ResetPosition();
prevPosition = transform.position;
prevRotation = transform.rotation;
linearVelocity = unityRigidbody.linearVelocity;
angularVelocity = unityRigidbody.angularVelocity;
base.OnEnable();
}
public void ResetPosition()
{
prevPosition = unityRigidbody.position;
prevRotation = Quaternion.AngleAxis(unityRigidbody.rotation, Vector3.forward);
linearVelocity = unityRigidbody.linearVelocity;
angularVelocity = unityRigidbody.angularVelocity;
}
private void CacheVelocities(float stepTime)
private void UpdateKinematicVelocities(float stepTime)
{
// differentiate positions/orientations to get our own velocites for kinematic objects.
// when calling Physics.Simulate, MovePosition/Rotation do not work correctly. Also useful for animations.
if (unityRigidbody.isKinematic && stepTime > 0)
if (unityRigidbody.isKinematic)
{
// differentiate positions to obtain linear velocity:
linearVelocity = (transform.position - prevPosition) / stepTime;
@@ -64,35 +61,27 @@ namespace Obi{
public override void UpdateIfNeeded(float stepTime)
{
// Rigidbody might not exist, as rigidbody deletion is buffered.
// This means the unity rigidbody might be deleted before the rigidbody handle is invalidated.
if (unityRigidbody == null) return;
CacheVelocities(stepTime);
UpdateKinematicVelocities(stepTime);
var world = ObiColliderWorld.GetInstance();
var rb = world.rigidbodies[Handle.index];
var rb = world.rigidbodies[handle.index];
rb.FromRigidbody(this);
world.rigidbodies[Handle.index] = rb;
world.rigidbodies[handle.index] = rb;
}
/**
/**
* Reads velocities back from the solver.
*/
public override void UpdateVelocities(Vector3 linearDelta, Vector3 angularDelta)
public override void UpdateVelocities(Vector3 linearDelta, Vector3 angularDelta)
{
// Rigidbody might not exist, as rigidbody deletion is buffered.
// This means the unity rigidbody might be deleted before the rigidbody handle is invalidated.
if (unityRigidbody == null) return;
// kinematic rigidbodies are passed to Obi with zero velocity, so we must ignore the new velocities calculated by the solver:
if (Application.isPlaying && !(unityRigidbody.isKinematic || kinematicForParticles))
{
unityRigidbody.linearVelocity += new Vector2(linearDelta.x, linearDelta.y);
unityRigidbody.angularVelocity += angularDelta[2] * Mathf.Rad2Deg;
}
unityRigidbody.linearVelocity += new Vector2(linearDelta.x, linearDelta.y);
unityRigidbody.angularVelocity += angularDelta[2] * Mathf.Rad2Deg;
}
}
}
}
}
}

View File

@@ -5,45 +5,28 @@ using System.Collections;
namespace Obi{
/**
/**
* Small helper class that lets you specify Obi-only properties for rigidbodies.
*/
[ExecuteInEditMode]
public abstract class ObiRigidbodyBase : MonoBehaviour
{
[ExecuteInEditMode]
public abstract class ObiRigidbodyBase : MonoBehaviour
{
public bool kinematicForParticles = false;
protected ObiRigidbodyHandle rigidbodyHandle;
public ObiRigidbodyHandle Handle
{
get
{
// don't check rigidbodyHandle.isValid:
// CreateRigidbody may defer creation, so we get a non-null, but invalid handle.
// If calling handle again right away before it becomes valid, it will call CreateRigidbody() again and create a second handle to the same body.
if (rigidbodyHandle == null)
{
var world = ObiColliderWorld.GetInstance();
public ObiRigidbodyHandle handle;
// create the material:
rigidbodyHandle = world.CreateRigidbody();
rigidbodyHandle.owner = this;
}
return rigidbodyHandle;
}
}
protected virtual void OnEnable()
public virtual void OnEnable()
{
rigidbodyHandle = ObiColliderWorld.GetInstance().CreateRigidbody();
rigidbodyHandle.owner = this;
handle = ObiColliderWorld.GetInstance().CreateRigidbody();
handle.owner = this;
UpdateIfNeeded(1);
}
public void OnDisable()
{
ObiColliderWorld.GetInstance().DestroyRigidbody(rigidbodyHandle);
ObiColliderWorld.GetInstance().DestroyRigidbody(handle);
}
public abstract void UpdateIfNeeded(float stepTime);

View File

@@ -76,38 +76,30 @@ namespace Obi
ObiTriangleMeshHandle handle = new ObiTriangleMeshHandle(null);
if (source != null && !handles.TryGetValue(source, out handle))
{
if (source.isReadable)
{
var sourceTris = source.triangles;
var sourceVertices = source.vertices;
// Build a bounding interval hierarchy from the triangles:
IBounded[] t = new IBounded[sourceTris.Length/3];
for (int i = 0; i < t.Length; ++i)
{
var sourceTris = source.triangles;
var sourceVertices = source.vertices;
// Build a bounding interval hierarchy from the triangles:
IBounded[] t = new IBounded[sourceTris.Length / 3];
for (int i = 0; i < t.Length; ++i)
{
int t1 = sourceTris[i * 3];
int t2 = sourceTris[i * 3 + 1];
int t3 = sourceTris[i * 3 + 2];
t[i] = new Triangle(t1, t2, t3, sourceVertices[t1], sourceVertices[t2], sourceVertices[t3]);
}
var sourceBih = BIH.Build(ref t);
Triangle[] tris = Array.ConvertAll(t, x => (Triangle)x);
handle = new ObiTriangleMeshHandle(source, headers.count);
handles.Add(source, handle);
headers.Add(new TriangleMeshHeader(bihNodes.count, sourceBih.Length, triangles.count, tris.Length, vertices.count, sourceVertices.Length));
bihNodes.AddRange(sourceBih);
triangles.AddRange(tris);
vertices.AddRange(sourceVertices);
}
else
{
handle = new ObiTriangleMeshHandle(source);
handles.Add(source, handle);
int t1 = sourceTris[i * 3];
int t2 = sourceTris[i * 3 + 1];
int t3 = sourceTris[i * 3 + 2];
t[i] = new Triangle(t1,t2,t3, sourceVertices[t1], sourceVertices[t2], sourceVertices[t3]);
}
var sourceBih = BIH.Build(ref t);
Triangle[] tris = Array.ConvertAll(t, x => (Triangle)x);
handle = new ObiTriangleMeshHandle(source, headers.count);
handles.Add(source, handle);
headers.Add(new TriangleMeshHeader(bihNodes.count, sourceBih.Length, triangles.count, tris.Length, vertices.count, sourceVertices.Length));
bihNodes.AddRange(sourceBih);
triangles.AddRange(tris);
vertices.AddRange(sourceVertices);
}
return handle;