/// <summary> /// Pefrorms a sweep test using a collider shape and stops at the first hit /// </summary> /// <param name="shape">The shape.</param> /// <param name="from">From.</param> /// <param name="to">To.</param> /// <returns></returns> /// <exception cref="System.Exception">This kind of shape cannot be used for a ShapeSweep.</exception> public HitResult ShapeSweep(ColliderShape shape, Matrix from, Matrix to) { var sh = shape.InternalShape as BulletSharp.ConvexShape; if (sh == null) { throw new Exception("This kind of shape cannot be used for a ShapeSweep."); } var result = new HitResult(); //result.Succeded is false by default using (var rcb = new BulletSharp.ClosestConvexResultCallback(from.TranslationVector, to.TranslationVector)) { collisionWorld.ConvexSweepTest(sh, from, to, rcb); if (rcb.HitCollisionObject == null) { return(result); } result.Succeeded = true; result.Collider = (Collider)rcb.HitCollisionObject.UserObject; result.Normal = rcb.HitNormalWorld; result.Point = rcb.HitPointWorld; } return(result); }
private static ColliderShape Compose(IReadOnlyList <IColliderShapeDesc> descs) { ColliderShape res = null; try { if (descs.Count == 1) //single shape case { res = CreateShape(descs[0]); } else if (descs.Count > 1) //need a compound shape in this case { var compound = new CompoundColliderShape(); foreach (var desc in descs) { compound.AddChildShape(CreateShape(desc)); } res = compound; } } catch (DllNotFoundException) { //during pre process and build process we often don't have the physics native engine running. } return(res); }
internal void DeriveBonePhysicsTransformation(out Matrix derivedTransformation) { Quaternion rotation; Vector3 translation; //derive rotation and translation, scale is ignored for now Vector3 scale; BoneWorldMatrix.Decompose(out scale, out rotation, out translation); derivedTransformation = Matrix.RotationQuaternion(rotation) * Matrix.Translation(translation); //handle dynamic scaling if allowed (aka not using assets) if (CanScaleShape) { if (scale != ColliderShape.Scaling) { ColliderShape.Scaling = scale; ColliderShape.UpdateLocalTransformations(); } } //Handle collider shape offset if (ColliderShape.LocalOffset != Vector3.Zero || ColliderShape.LocalRotation != Quaternion.Identity) { derivedTransformation = Matrix.Multiply(ColliderShape.PositiveCenterMatrix, derivedTransformation); } }
public void ComposeShape() { ColliderShapeChanged = false; if (ColliderShape != null) { if (!ColliderShape.IsPartOfAsset) { ColliderShape.Dispose(); ColliderShape = null; } else { ColliderShape = null; } } CanScaleShape = true; if (ColliderShapes.Count == 1) //single shape case { if (ColliderShapes[0] == null) { return; } if (ColliderShapes[0].GetType() == typeof(ColliderShapeAssetDesc)) { CanScaleShape = false; } ColliderShape = PhysicsColliderShape.CreateShape(ColliderShapes[0]); ColliderShape?.UpdateLocalTransformations(); } else if (ColliderShapes.Count > 1) //need a compound shape in this case { var compound = new CompoundColliderShape(); foreach (var desc in ColliderShapes) { if (desc == null) { continue; } if (desc.GetType() == typeof(ColliderShapeAssetDesc)) { CanScaleShape = false; } var subShape = PhysicsColliderShape.CreateShape(desc); if (subShape != null) { compound.AddChildShape(subShape); } } ColliderShape = compound; ColliderShape.UpdateLocalTransformations(); } }
/// <summary> /// Creates the rigid body. /// </summary> /// <param name="collider">The collider.</param> /// <returns></returns> public static RigidBody CreateRigidBody(ColliderShape collider) { var rb = new RigidBody(collider); rb.InternalRigidBody = new BulletSharp.RigidBody(0.0f, rb.MotionState, collider.InternalShape, Vector3.Zero); rb.InternalRigidBody.CollisionFlags |= BulletSharp.CollisionFlags.StaticObject; //already set if mass is 0 actually! rb.InternalCollider = rb.InternalRigidBody; rb.InternalCollider.ContactProcessingThreshold = 1e18f; if (collider.NeedsCustomCollisionCallback) { rb.InternalCollider.CollisionFlags |= BulletSharp.CollisionFlags.CustomMaterialCallback; } if (collider.Is2D) //set different defaults for 2D shapes { rb.InternalRigidBody.LinearFactor = new Vector3(1.0f, 1.0f, 0.0f); rb.InternalRigidBody.AngularFactor = new Vector3(0.0f, 0.0f, 1.0f); } rb.InternalRigidBody.UserObject = rb; return(rb); }
/// <summary> /// Pefrorms a sweep test using a collider shape and never stops until "to" /// </summary> /// <param name="shape">The shape.</param> /// <param name="from">From.</param> /// <param name="to">To.</param> /// <returns></returns> /// <exception cref="System.Exception">This kind of shape cannot be used for a ShapeSweep.</exception> public List <HitResult> ShapeSweepPenetrating(ColliderShape shape, Matrix from, Matrix to) { var sh = shape.InternalShape as BulletSharp.ConvexShape; if (sh == null) { throw new Exception("This kind of shape cannot be used for a ShapeSweep."); } var result = new List <HitResult>(); using (var rcb = new BulletSharp.AllHitsConvexResultCallback()) { collisionWorld.ConvexSweepTest(sh, from, to, rcb); var count = rcb.CollisionObjects.Count; for (var i = 0; i < count; i++) { var singleResult = new HitResult { Succeeded = true, Collider = (Collider)rcb.CollisionObjects[i].UserObject, Normal = rcb.HitNormalWorld[i], Point = rcb.HitPointWorld[i] }; result.Add(singleResult); } } return(result); }
/// <summary> /// Creates the character. /// </summary> /// <param name="collider">The collider.</param> /// <param name="stepHeight">Height of the step.</param> /// <returns></returns> public static Character CreateCharacter(ColliderShape collider, float stepHeight) { var ch = new Character(collider) { InternalCollider = new BulletSharp.PairCachingGhostObject { CollisionShape = collider.InternalShape } }; ch.InternalCollider.CollisionFlags |= BulletSharp.CollisionFlags.CharacterObject; if (collider.NeedsCustomCollisionCallback) { ch.InternalCollider.CollisionFlags |= BulletSharp.CollisionFlags.CustomMaterialCallback; } ch.InternalCollider.ContactProcessingThreshold = 1e18f; ch.KinematicCharacter = new BulletSharp.KinematicCharacterController((BulletSharp.PairCachingGhostObject)ch.InternalCollider, (BulletSharp.ConvexShape)collider.InternalShape, stepHeight); ch.InternalCollider.UserObject = ch; return(ch); }
/// <summary> /// Removes a child shape. /// </summary> /// <param name="shape">The shape.</param> public void RemoveChildShape(ColliderShape shape) { colliderShapes.Remove(shape); InternalCompoundShape.RemoveChildShape(shape.InternalShape); shape.Parent = null; }
/// <summary> /// Adds a child shape. /// </summary> /// <param name="shape">The shape.</param> public void AddChildShape(ColliderShape shape) { colliderShapes.Add(shape); InternalCompoundShape.AddChildShape(shape.PositiveCenterMatrix, shape.InternalShape); shape.Parent = this; }
public void Dispose() { if (Shape == null) { return; } Shape.Dispose(); Shape = null; }
/// <summary> /// Initializes a new instance of the <see cref="Collider"/> class. /// </summary> /// <param name="collider">The collider.</param> public Collider(ColliderShape collider) { ProtectedColliderShape = collider; FirstCollisionChannel = new Channel<Collision> { Preference = ChannelPreference.PreferSender }; NewPairChannel = new Channel<Collision> { Preference = ChannelPreference.PreferSender }; PairEndedChannel = new Channel<Collision> { Preference = ChannelPreference.PreferSender }; AllPairsEndedChannel = new Channel<Collision> { Preference = ChannelPreference.PreferSender }; }
public void Dispose() { if (Shape == null) { return; } var compound = Shape.Parent; compound?.RemoveChildShape(Shape); Shape.Dispose(); Shape = null; }
/// <summary> /// Initializes a new instance of the <see cref="Collider"/> class. /// </summary> /// <param name="collider">The collider.</param> public Collider(ColliderShape collider) { ProtectedColliderShape = collider; FirstCollisionChannel = new Channel <Collision> { Preference = ChannelPreference.PreferSender }; NewPairChannel = new Channel <Collision> { Preference = ChannelPreference.PreferSender }; PairEndedChannel = new Channel <Collision> { Preference = ChannelPreference.PreferSender }; AllPairsEndedChannel = new Channel <Collision> { Preference = ChannelPreference.PreferSender }; }
/// <summary> /// Creates the collider. /// </summary> /// <param name="shape">The shape.</param> /// <returns></returns> public Collider CreateCollider(ColliderShape shape) { var collider = new Collider(shape) { InternalCollider = new BulletSharp.CollisionObject { CollisionShape = shape.InternalShape, ContactProcessingThreshold = !canCcd ? 1e18f : 1e30f } }; collider.InternalCollider.CollisionFlags |= BulletSharp.CollisionFlags.NoContactResponse; if (shape.NeedsCustomCollisionCallback) { collider.InternalCollider.CollisionFlags |= BulletSharp.CollisionFlags.CustomMaterialCallback; } return(collider); }
internal static ColliderShape Compose(IReadOnlyList <IAssetColliderShapeDesc> descs) { ColliderShape res = null; if (descs.Count == 1) //single shape case { res = CreateShape(descs[0]); res.IsPartOfAsset = true; } else if (descs.Count > 1) //need a compound shape in this case { var compound = new CompoundColliderShape(); foreach (var desc in descs) { compound.AddChildShape(CreateShape(desc)); } res = compound; res.IsPartOfAsset = true; } return(res); }
public ColliderShape CreateShape(IInlineColliderShapeDesc desc) { ColliderShape shape = null; var shapeType = desc.GetType(); if (shapeType == typeof(BoxColliderShapeDesc)) { var boxDesc = (BoxColliderShapeDesc)desc; if (boxDesc.Is2D) { shape = new Box2DColliderShape(new Vector2(boxDesc.Size.X, boxDesc.Size.Y)) { LocalOffset = boxDesc.LocalOffset, LocalRotation = boxDesc.LocalRotation }; } else { shape = new BoxColliderShape(boxDesc.Size) { LocalOffset = boxDesc.LocalOffset, LocalRotation = boxDesc.LocalRotation }; } } else if (shapeType == typeof(CapsuleColliderShapeDesc)) { var capsuleDesc = (CapsuleColliderShapeDesc)desc; shape = new CapsuleColliderShape(capsuleDesc.Is2D, capsuleDesc.Radius, capsuleDesc.Length, capsuleDesc.Orientation) { LocalOffset = capsuleDesc.LocalOffset, LocalRotation = capsuleDesc.LocalRotation }; } else if (shapeType == typeof(CylinderColliderShapeDesc)) { var cylinderDesc = (CylinderColliderShapeDesc)desc; shape = new CylinderColliderShape(cylinderDesc.Height, cylinderDesc.Radius, cylinderDesc.Orientation) { LocalOffset = cylinderDesc.LocalOffset, LocalRotation = cylinderDesc.LocalRotation }; } else if (shapeType == typeof(SphereColliderShapeDesc)) { var sphereDesc = (SphereColliderShapeDesc)desc; shape = new SphereColliderShape(sphereDesc.Is2D, sphereDesc.Radius) { LocalOffset = sphereDesc.LocalOffset }; } else if (shapeType == typeof(StaticPlaneColliderShapeDesc)) { var planeDesc = (StaticPlaneColliderShapeDesc)desc; shape = new StaticPlaneColliderShape(planeDesc.Normal, planeDesc.Offset); } else if (shapeType == typeof(ColliderShapeAssetDesc)) { var assetDesc = (ColliderShapeAssetDesc)desc; if (assetDesc.Shape == null) { return(null); } if (assetDesc.Shape.Shape == null) { assetDesc.Shape.Shape = PhysicsColliderShape.Compose(assetDesc.Shape.Descriptions); } shape = assetDesc.Shape.Shape; } if (shape != null) { shape.Parent = null; //from now parent might change shape.UpdateLocalTransformations(); } return(shape); }
/// <summary> /// Initializes a new instance of the <see cref="Collider"/> class. /// </summary> /// <param name="collider">The collider.</param> public Collider(ColliderShape collider) { ColliderShape = collider; }
/// <summary> /// Initializes a new instance of the <see cref="Character"/> class. /// </summary> /// <param name="collider">The collider.</param> internal Character(ColliderShape collider) : base(collider) { }
private static ColliderShape CreateShape(IColliderShapeDesc desc) { ColliderShape shape = null; var type = desc.GetType(); if (type == typeof(Box2DColliderShapeDesc)) { var boxDesc = (Box2DColliderShapeDesc)desc; shape = new Box2DColliderShape(boxDesc.HalfExtent) { LocalOffset = boxDesc.LocalOffset, LocalRotation = boxDesc.LocalRotation }; } else if (type == typeof(BoxColliderShapeDesc)) { var boxDesc = (BoxColliderShapeDesc)desc; shape = new BoxColliderShape(boxDesc.HalfExtents) { LocalOffset = boxDesc.LocalOffset, LocalRotation = boxDesc.LocalRotation }; } else if (type == typeof(CapsuleColliderShapeDesc)) { var capsuleDesc = (CapsuleColliderShapeDesc)desc; shape = new CapsuleColliderShape(capsuleDesc.Is2D, capsuleDesc.Radius, capsuleDesc.Height, capsuleDesc.UpAxis) { LocalOffset = capsuleDesc.LocalOffset, LocalRotation = capsuleDesc.LocalRotation }; } else if (type == typeof(CylinderColliderShapeDesc)) { var cylinderDesc = (CylinderColliderShapeDesc)desc; shape = new CylinderColliderShape(cylinderDesc.HalfExtents, cylinderDesc.UpAxis) { LocalOffset = cylinderDesc.LocalOffset, LocalRotation = cylinderDesc.LocalRotation }; } else if (type == typeof(SphereColliderShapeDesc)) { var sphereDesc = (SphereColliderShapeDesc)desc; shape = new SphereColliderShape(sphereDesc.Is2D, sphereDesc.Radius) { LocalOffset = sphereDesc.LocalOffset }; } else if (type == typeof(StaticPlaneColliderShapeDesc)) { var planeDesc = (StaticPlaneColliderShapeDesc)desc; shape = new StaticPlaneColliderShape(planeDesc.Normal, planeDesc.Offset); } else if (type == typeof(ConvexHullColliderShapeDesc)) { var convexDesc = (ConvexHullColliderShapeDesc)desc; //Optimize performance and focus on less shapes creation since this shape could be nested if (convexDesc.ConvexHulls.Count == 1) { if (convexDesc.ConvexHulls[0].Count == 1) { shape = new ConvexHullColliderShape(convexDesc.ConvexHulls[0][0], convexDesc.ConvexHullsIndices[0][0], convexDesc.Scaling) { NeedsCustomCollisionCallback = true }; shape.UpdateLocalTransformations(); shape.Description = desc; return(shape); } if (convexDesc.ConvexHulls[0].Count <= 1) { return(null); } var subCompound = new CompoundColliderShape { NeedsCustomCollisionCallback = true }; for (var i = 0; i < convexDesc.ConvexHulls[0].Count; i++) { var verts = convexDesc.ConvexHulls[0][i]; var indices = convexDesc.ConvexHullsIndices[0][i]; var subHull = new ConvexHullColliderShape(verts, indices, convexDesc.Scaling); subHull.UpdateLocalTransformations(); subCompound.AddChildShape(subHull); } subCompound.UpdateLocalTransformations(); subCompound.Description = desc; return(subCompound); } if (convexDesc.ConvexHulls.Count <= 1) { return(null); } var compound = new CompoundColliderShape { NeedsCustomCollisionCallback = true }; for (var i = 0; i < convexDesc.ConvexHulls.Count; i++) { var verts = convexDesc.ConvexHulls[i]; var indices = convexDesc.ConvexHullsIndices[i]; if (verts.Count == 1) { var subHull = new ConvexHullColliderShape(verts[0], indices[0], convexDesc.Scaling); subHull.UpdateLocalTransformations(); compound.AddChildShape(subHull); } else if (verts.Count > 1) { var subCompound = new CompoundColliderShape(); for (var b = 0; b < verts.Count; b++) { var subVerts = verts[b]; var subIndex = indices[b]; var subHull = new ConvexHullColliderShape(subVerts, subIndex, convexDesc.Scaling); subHull.UpdateLocalTransformations(); subCompound.AddChildShape(subHull); } subCompound.UpdateLocalTransformations(); compound.AddChildShape(subCompound); } } compound.UpdateLocalTransformations(); compound.Description = desc; return(compound); } if (shape != null) { shape.UpdateLocalTransformations(); shape.Description = desc; } return(shape); }
public void Dispose() { if (Shape == null) return; Shape.Dispose(); Shape = null; }
internal RigidBody(ColliderShape collider) : base(collider) { LinkedConstraints = new List <Constraint>(); MotionState = new ParadoxMotionState(this); }
/// <summary> /// Creates the character. /// </summary> /// <param name="collider">The collider.</param> /// <param name="stepHeight">Height of the step.</param> /// <returns></returns> public Character CreateCharacter(ColliderShape collider, float stepHeight) { var ch = new Character(collider) { InternalCollider = new BulletSharp.PairCachingGhostObject { CollisionShape = collider.InternalShape } }; ch.InternalCollider.UserObject = ch; ch.InternalCollider.CollisionFlags |= BulletSharp.CollisionFlags.CharacterObject; if (collider.NeedsCustomCollisionCallback) { ch.InternalCollider.CollisionFlags |= BulletSharp.CollisionFlags.CustomMaterialCallback; } ch.InternalCollider.ContactProcessingThreshold = !canCcd ? 1e18f : 1e30f; ch.KinematicCharacter = new BulletSharp.KinematicCharacterController((BulletSharp.PairCachingGhostObject)ch.InternalCollider, (BulletSharp.ConvexShape)collider.InternalShape, stepHeight); return ch; }
/// <summary> /// Creates the rigid body. /// </summary> /// <param name="collider">The collider.</param> /// <returns></returns> public RigidBody CreateRigidBody(ColliderShape collider) { var rb = new RigidBody(collider); rb.InternalRigidBody = new BulletSharp.RigidBody(0.0f, rb.MotionState, collider.InternalShape, Vector3.Zero) { UserObject = rb }; rb.InternalCollider = rb.InternalRigidBody; rb.InternalCollider.ContactProcessingThreshold = !canCcd ? 1e18f : 1e30f; if (collider.NeedsCustomCollisionCallback) { rb.InternalCollider.CollisionFlags |= BulletSharp.CollisionFlags.CustomMaterialCallback; } if (collider.Is2D) //set different defaults for 2D shapes { rb.InternalRigidBody.LinearFactor = new Vector3(1.0f, 1.0f, 0.0f); rb.InternalRigidBody.AngularFactor = new Vector3(0.0f, 0.0f, 1.0f); } return rb; }
/// <summary> /// Creates the collider. /// </summary> /// <param name="shape">The shape.</param> /// <returns></returns> public Collider CreateCollider(ColliderShape shape) { var collider = new Collider(shape) { InternalCollider = new BulletSharp.CollisionObject { CollisionShape = shape.InternalShape, ContactProcessingThreshold = !canCcd ? 1e18f : 1e30f } }; collider.InternalCollider.UserObject = collider; collider.InternalCollider.CollisionFlags |= BulletSharp.CollisionFlags.NoContactResponse; if (shape.NeedsCustomCollisionCallback) { collider.InternalCollider.CollisionFlags |= BulletSharp.CollisionFlags.CustomMaterialCallback; } return collider; }
/// <summary> /// Pefrorms a sweep test using a collider shape and never stops until "to" /// </summary> /// <param name="shape">The shape.</param> /// <param name="from">From.</param> /// <param name="to">To.</param> /// <returns></returns> /// <exception cref="System.Exception">This kind of shape cannot be used for a ShapeSweep.</exception> public List<HitResult> ShapeSweepPenetrating(ColliderShape shape, Matrix from, Matrix to) { var sh = shape.InternalShape as BulletSharp.ConvexShape; if (sh == null) throw new Exception("This kind of shape cannot be used for a ShapeSweep."); var result = new List<HitResult>(); using (var rcb = new BulletSharp.AllHitsConvexResultCallback()) { collisionWorld.ConvexSweepTest(sh, from, to, rcb); var count = rcb.CollisionObjects.Count; for (var i = 0; i < count; i++) { var singleResult = new HitResult { Succeeded = true, Collider = (Collider)rcb.CollisionObjects[i].UserObject, Normal = rcb.HitNormalWorld[i], Point = rcb.HitPointWorld[i] }; result.Add(singleResult); } } return result; }
public void ComposeShape() { ColliderShapeChanged = false; if (ColliderShape != null) { if (!ColliderShape.IsPartOfAsset) { ColliderShape.Dispose(); ColliderShape = null; } else { ColliderShape = null; } } try { CanScaleShape = true; if (ColliderShapes.Count == 1) //single shape case { if (ColliderShapes[0] != null) { if (ColliderShapes[0].GetType() == typeof(ColliderShapeAssetDesc)) { CanScaleShape = false; } ColliderShape = CreateShape(ColliderShapes[0]); if (ColliderShape == null) { return; } ColliderShape.UpdateLocalTransformations(); } } else if (ColliderShapes.Count > 1) //need a compound shape in this case { var compound = new CompoundColliderShape(); foreach (var desc in ColliderShapes) { if (desc != null) { if (desc.GetType() == typeof(ColliderShapeAssetDesc)) { CanScaleShape = false; } var subShape = CreateShape(desc); if (subShape != null) { compound.AddChildShape(subShape); } } } ColliderShape = compound; ColliderShape.UpdateLocalTransformations(); } } catch (DllNotFoundException) { //during pre process and build process we often don't have the physics native engine running. } }
internal RigidBody(ColliderShape collider) : base(collider) { LinkedConstraints = new List<Constraint>(); MotionState = new ParadoxMotionState(this); }
public PhysicsColliderShape(ColliderShape shape) { Shape = shape; }
/// <summary> /// Pefrorms a sweep test using a collider shape and stops at the first hit /// </summary> /// <param name="shape">The shape.</param> /// <param name="from">From.</param> /// <param name="to">To.</param> /// <returns></returns> /// <exception cref="System.Exception">This kind of shape cannot be used for a ShapeSweep.</exception> public HitResult ShapeSweep(ColliderShape shape, Matrix from, Matrix to) { var sh = shape.InternalShape as BulletSharp.ConvexShape; if (sh == null) throw new Exception("This kind of shape cannot be used for a ShapeSweep."); var result = new HitResult(); //result.Succeded is false by default using (var rcb = new BulletSharp.ClosestConvexResultCallback(from.TranslationVector, to.TranslationVector)) { collisionWorld.ConvexSweepTest(sh, from, to, rcb); if (rcb.HitCollisionObject == null) return result; result.Succeeded = true; result.Collider = (Collider)rcb.HitCollisionObject.UserObject; result.Normal = rcb.HitNormalWorld; result.Point = rcb.HitPointWorld; } return result; }
public void Dispose() { if (Shape == null) return; var compound = Shape.Parent; compound?.RemoveChildShape(Shape); Shape.Dispose(); Shape = null; }