/// <summary> /// Removes the native geometry from the simulation. /// </summary> protected override void OnDestroy() { if (m_geometry != null && GetSimulation() != null) { GetSimulation().remove(m_geometry); } if (Simulation.Instance != null) { Simulation.Instance.StepCallbacks.PostSynchronizeTransforms -= OnPostSynchronizeTransformsCallback; } if (m_shape != null) { m_shape.Dispose(); } m_shape = null; if (m_geometry != null) { m_geometry.Dispose(); } m_geometry = null; m_transform = null; base.OnDestroy(); }
/// <summary> /// Creates native shape and geometry. Assigns material to the /// native geometry if material is present. /// </summary> /// <returns></returns> protected override bool Initialize() { m_transform = transform; m_shape = CreateNative(); if (m_shape == null) { return(false); } m_geometry = new agxCollide.Geometry(m_shape, GetNativeGeometryOffset()); m_geometry.setName(name); m_geometry.setEnable(IsEnabled); if (Material != null) { m_geometry.setMaterial(m_material.GetInitialized <ShapeMaterial>().Native); } SyncNativeTransform(); GetSimulation().add(m_geometry); // TODO: Add pre-synch to be able to move geometries during play? // Adding transform synchronization. This will be removed if this // shape is part of a rigid body (SetRigidBody) since our transform // will be updated with our parent body. Simulation.Instance.StepCallbacks.PostSynchronizeTransforms += OnPostSynchronizeTransformsCallback; return(base.Initialize()); }
private Node GetOrCreateShape(agxCollide.Shape shape) { return(GetOrCreateNode(NodeType.Shape, shape.getUuid(), false, () => m_shapes.Add(shape.getUuid(), shape))); }
/// <summary> /// Creates native shape and geometry. Assigns material to the /// native geometry if material is present. /// </summary> /// <returns></returns> protected override bool Initialize() { m_shape = CreateNative(); if (m_shape == null) { return(false); } m_geometry = new agxCollide.Geometry(m_shape, GetNativeGeometryOffset()); m_geometry.setName(name); m_geometry.setEnable(IsEnabled); if (Material != null) { m_geometry.setMaterial(m_material.GetInitialized <ShapeMaterial>().Native); } SyncNativeTransform(); GetSimulation().add(m_geometry); // Temp hack to get "pulley property" of a RigidBody which name // contains the name "sheave". //RigidBody rbTmp = Find.FirstParentWithComponent<RigidBody>( gameObject ); //if ( rbTmp != null && rbTmp.gameObject.name.ToLower().Contains( "sheave" ) ) { // Debug.Log( "Adding pulley property to: " + gameObject.name + " from rb.name = " + rbTmp.gameObject.name ); // m_geometry.getPropertyContainer().addPropertyBool( "Pulley", true ); //} // TODO: Add pre-synch to be able to move geometries during play? Simulation.Instance.StepCallbacks.PostSynchronizeTransforms += OnPostSynchronizeTransformsCallback; return(base.Initialize()); }
/// <summary> /// Peek at a temporary native instance or the current (if initialized). /// </summary> /// <param name="callback">Callback with temporary or already initialized native instance. Callback signature ( nativeRb, isTemporary ).</param> /// <remarks> /// Always assume the native instance to be temporary. It's never safe to cache an instance to the native object. /// </remarks> public void PeekTemporaryNativeOrGetNative(Action <agx.RigidBody, bool> callback) { if (callback == null) { return; } if (m_rb != null) { callback(m_rb, false); } else { Shape[] shapes = GetComponentsInChildren <Shape>(); using (agx.RigidBody rb = new agx.RigidBody()) { foreach (Shape shape in shapes) { agxCollide.Shape nativeShape = shape.CreateTemporaryNative(); if (nativeShape != null) { agxCollide.Geometry geometry = new agxCollide.Geometry(nativeShape); geometry.setEnable(shape.IsEnabled); if (shape.Material != null) { geometry.setMaterial(shape.Material.CreateTemporaryNative()); } rb.add(geometry, shape.GetNativeRigidBodyOffset(this)); } } // For center of mass position/rotation to be correct we have to // synchronize the native transform given current game object transform. SyncNativeTransform(rb); callback(rb, true); // Hitting "Update" (mass or inertia in the Inspector) several times // will crash agx if we don't remove the geometries and shapes. while (rb.getGeometries().Count > 0) { agxCollide.Geometry geometry = rb.getGeometries()[0].get(); if (geometry.getShapes().Count > 0) { geometry.remove(geometry.getShapes()[0].get()); } rb.remove(geometry); } } } }