public unsafe void CylinderCollider_Create_ResultHasExpectedValues() { var geometry = new CylinderGeometry { Center = new float3(-10.10f, 10.12f, 0.01f), Orientation = quaternion.AxisAngle(math.normalize(new float3(1.4f, 0.2f, 1.1f)), 38.50f), Height = 2f, Radius = 0.25f, BevelRadius = 0.05f, SideCount = 10 }; var collider = CylinderCollider.Create(geometry); var cylinderCollider = UnsafeUtility.AsRef <CylinderCollider>(collider.GetUnsafePtr()); Assert.AreEqual(geometry.Center, cylinderCollider.Center); Assert.AreEqual(geometry.Center, cylinderCollider.Geometry.Center); Assert.AreEqual(geometry.Orientation, cylinderCollider.Orientation); Assert.AreEqual(geometry.Orientation, cylinderCollider.Geometry.Orientation); Assert.AreEqual(geometry.Height, cylinderCollider.Height); Assert.AreEqual(geometry.Height, cylinderCollider.Geometry.Height); Assert.AreEqual(geometry.Radius, cylinderCollider.Radius); Assert.AreEqual(geometry.Radius, cylinderCollider.Geometry.Radius); Assert.AreEqual(geometry.BevelRadius, cylinderCollider.BevelRadius); Assert.AreEqual(geometry.BevelRadius, cylinderCollider.Geometry.BevelRadius); Assert.AreEqual(CollisionType.Convex, cylinderCollider.CollisionType); Assert.AreEqual(ColliderType.Cylinder, cylinderCollider.Type); }
public override void InstanceSpawn() { vCollisionOrigin = new Vector2(0, 0); rCollisionRectangle = new Rectangle(0, 0, 16, 16); hitCylinder = new CylinderCollider(new Vector3(x, z, y), 10, 2.5f); }
private void SetupNewCollider(CylinderCollider cylc, MeshCollider mc) { switch (setupBoundsType.Value) { case SetupBoundsType.None: break; case SetupBoundsType.SetupFromPreciseBounds: cylc.SetFromPreciseBounds(); break; } if (PreserveColliderSettings) { cylc.Type.Value = mc.Type.Value; cylc.CharacterCollider.Value = mc.CharacterCollider.Value; cylc.IgnoreRaycasts.Value = mc.IgnoreRaycasts.Value; cylc.Mass.Value = mc.Mass.Value; } else { cylc.Type.Value = setColliderType; cylc.CharacterCollider.Value = SetCharacterCollider; cylc.IgnoreRaycasts.Value = SetIgnoreRaycasts; cylc.Mass.Value = Mass; } }
public void Execute() => CylinderCollider.Create(new CylinderGeometry { Orientation = quaternion.identity, Height = 1f, Radius = 1f, SideCount = CylinderGeometry.MaxSideCount }).Dispose();
public static PhysicsCollider GetEnviromentPhysicsCollider(string name, float scale = 1f) { PhysicsCollider pc = new PhysicsCollider { }; switch (name) { case "Kanto|Tree": NativeArray <CompoundCollider.ColliderBlobInstance> cs = new NativeArray <CompoundCollider.ColliderBlobInstance>(2, Allocator.TempJob); cs[0] = new CompoundCollider.ColliderBlobInstance { Collider = CylinderCollider.Create(new CylinderGeometry { Orientation = Quaternion.Euler(new float3(270f, 270f, 0)), Height = 1f * scale, Radius = .25f * scale, SideCount = 20, Center = new float3(0, .5f, 0) * scale }, new CollisionFilter { BelongsTo = TriggerEventClass.Collidable, CollidesWith = TriggerEventClass.Collidable, GroupIndex = 1 }), CompoundFromChild = new RigidTransform { rot = quaternion.identity } }; cs[1] = new CompoundCollider.ColliderBlobInstance { Collider = CylinderCollider.Create(new CylinderGeometry { Center = new float3(0, 1.7f, 0) * scale, Orientation = Quaternion.Euler(new float3(90f, 0, 0)), Height = 1.5f * scale, Radius = 1f * scale, SideCount = 20 }, new CollisionFilter { BelongsTo = TriggerEventClass.Collidable, CollidesWith = TriggerEventClass.Collidable, GroupIndex = 1 }), CompoundFromChild = new RigidTransform { rot = quaternion.identity } }; pc = new PhysicsCollider { Value = CompoundCollider.Create(cs) }; cs.Dispose(); break; default: Debug.LogWarning("Failed to get collider for \"" + name + "\""); break; } return(pc); }
public void Execute(int i) { var shapeData = ComputeData[ToComputeTable[i]]; switch (shapeData.ShapeType) { case ShapeType.Box: { BlobAssets[i] = BoxCollider.Create( shapeData.BoxProperties, shapeData.CollisionFilter, shapeData.Material ); return; } case ShapeType.Capsule: { BlobAssets[i] = CapsuleCollider.Create( shapeData.CapsuleProperties, shapeData.CollisionFilter, shapeData.Material ); return; } case ShapeType.Cylinder: { BlobAssets[i] = CylinderCollider.Create( shapeData.CylinderProperties, shapeData.CollisionFilter, shapeData.Material ); return; } case ShapeType.Plane: { var v = shapeData.PlaneVertices; BlobAssets[i] = PolygonCollider.CreateQuad( v.c0, v.c1, v.c2, v.c3, shapeData.CollisionFilter, shapeData.Material ); return; } case ShapeType.Sphere: { BlobAssets[i] = SphereCollider.Create( shapeData.SphereProperties, shapeData.CollisionFilter, shapeData.Material ); return; } // Note : Mesh and Hull are not computed here as they are in a separated set of jobs default: return; } }
public void CylinderCollider_Create_WhenHeightInvalid_Throws( [Values(float.PositiveInfinity, float.NegativeInfinity, float.NaN, -1f)] float errantValue ) { var geometry = new CylinderGeometry { Height = errantValue, Orientation = quaternion.identity, SideCount = CylinderGeometry.MaxSideCount }; var ex = Assert.Throws <ArgumentException>(() => CylinderCollider.Create(geometry)); Assert.That(ex.Message, Does.Match(nameof(CylinderGeometry.Height))); }
public bool isColliding(CylinderCollider otherCollider) { bool collided = false; //vertical collision if ((otherCollider.Position.Y + otherCollider.Height >= this.Position.Y && otherCollider.Position.Y + otherCollider.Height <= this.Position.Y + this.Height) || (otherCollider.Position.Y >= this.Position.Y && otherCollider.Position.Y <= this.Position.Y + this.Height)) { //horizontal collision if (PointDistance(this.Position.X, this.Position.Z, otherCollider.Position.X, otherCollider.Position.Z) < this.Radius + otherCollider.Radius) { collided = true; } } return(collided); }
protected override BlobAssetReference <Collider> ProduceColliderBlob(PhysicsShape shape) { var material = ProduceMaterial(shape); var collisionFilter = ProduceCollisionFilter(shape); var blob = new BlobAssetReference <Collider>(); shape.GetBakeTransformation(out var linearScalar, out var radiusScalar); switch (shape.ShapeType) { case ShapeType.Box: shape.GetBoxProperties(out var center, out var size, out quaternion orientation); blob = BoxCollider.Create( center * linearScalar, orientation, math.abs(size * linearScalar), shape.ConvexRadius * radiusScalar, collisionFilter, material); break; case ShapeType.Capsule: shape.GetCapsuleProperties(out var v0, out var v1, out var radius); blob = CapsuleCollider.Create( v0 * linearScalar, v1 * linearScalar, radius * radiusScalar, collisionFilter, material); break; case ShapeType.Sphere: shape.GetSphereProperties(out center, out radius, out orientation); blob = SphereCollider.Create( center * linearScalar, radius * radiusScalar, collisionFilter, material); break; case ShapeType.Cylinder: shape.GetCylinderProperties(out center, out var height, out radius, out orientation); var s = math.abs(math.mul(math.inverse(orientation), linearScalar)); blob = CylinderCollider.Create( center * linearScalar, height * s.z, radius * math.cmax(s.xy), orientation, shape.ConvexRadius * radiusScalar, collisionFilter, material); break; case ShapeType.Plane: shape.GetPlaneProperties(out v0, out v1, out var v2, out var v3); blob = PolygonCollider.CreateQuad( v0 * linearScalar, v1 * linearScalar, v2 * linearScalar, v3 * linearScalar, collisionFilter, material); break; case ShapeType.ConvexHull: var pointCloud = new NativeList <float3>(65535, Allocator.Temp); shape.GetConvexHullProperties(pointCloud); if (pointCloud.Length == 0) { pointCloud.Dispose(); throw new InvalidOperationException( $"No vertices associated with {shape.name}. Add a {typeof(MeshFilter)} component or assign {nameof(PhysicsShape.CustomMesh)}." ); } blob = ConvexCollider.Create( pointCloud, shape.ConvexRadius * radiusScalar, linearScalar, collisionFilter, material); pointCloud.Dispose(); break; case ShapeType.Mesh: // TODO: no convex radius? var mesh = shape.GetMesh(); if (mesh == null) { throw new InvalidOperationException( $"No mesh associated with {shape.name}. Add a {typeof(MeshFilter)} component or assign {nameof(PhysicsShape.CustomMesh)}." ); } else { blob = MeshCollider.Create(mesh.GetScaledVertices(linearScalar), mesh.triangles, collisionFilter, material); } break; default: throw new UnimplementedShapeException(shape.ShapeType); } return(blob); }
public static unsafe BlobAssetReference <Collider> GenerateRandomConvex(ref Random rnd) { ColliderType colliderType = (ColliderType)rnd.NextInt((int)ColliderType.Cylinder + 1); float convexRadius = (rnd.NextInt(4) > 0) ? rnd.NextFloat(0.5f) : 0.0f; switch (colliderType) { case ColliderType.Convex: { int numPoints = rnd.NextInt(1, 16); if (numPoints == 3) // TODO - hull builder doesn't build faces for flat shapes, work around it for now to run the test { numPoints++; } var points = new NativeArray <float3>(numPoints, Allocator.TempJob); for (int i = 0; i < numPoints; i++) { points[i] = rnd.NextFloat3(-1.0f, 1.0f); } var generationParameters = ConvexHullGenerationParameters.Default; generationParameters.BevelRadius = convexRadius; var collider = ConvexCollider.Create(points, generationParameters, CollisionFilter.Default); points.Dispose(); return(collider); } case ColliderType.Sphere: { return(SphereCollider.Create(new SphereGeometry { Center = (rnd.NextInt(4) > 0) ? float3.zero : rnd.NextFloat3(-0.5f, 0.5f), Radius = rnd.NextFloat(0.01f, 0.5f) })); } case ColliderType.Capsule: { float3 point0 = rnd.NextFloat3(0.0f, 1.0f); float3 point1 = (rnd.NextInt(4) > 0) ? -point0 : rnd.NextFloat3(-1.0f, 1.0f); return(CapsuleCollider.Create(new CapsuleGeometry { Vertex0 = point0, Vertex1 = point1, Radius = rnd.NextFloat(0.01f, 0.5f) })); } case ColliderType.Triangle: { return(PolygonCollider.CreateTriangle(rnd.NextFloat3(-1.0f, 1.0f), rnd.NextFloat3(-1.0f, 1.0f), rnd.NextFloat3(-1.0f, 1.0f))); } case ColliderType.Quad: { // Pick 3 totally random points, then choose a fourth that makes a flat and convex quad float3 point0 = rnd.NextFloat3(-1.0f, 1.0f); float3 point1 = rnd.NextFloat3(-1.0f, 1.0f); float3 point3 = rnd.NextFloat3(-1.0f, 1.0f); float t0 = rnd.NextFloat(0.0f, 1.0f); float t1 = rnd.NextFloat(0.0f, 1.0f); float3 e = point1 + point1 - point0; float3 a = math.lerp(point1, e, t0); float3 b = math.lerp(point3, point3 + point3 - point0, t0); float3 point2 = math.lerp(a, b, t1); return(PolygonCollider.CreateQuad(point0, point1, point2, point3)); } case ColliderType.Box: { float minSize = 0.05f; // TODO - work around hull builder problems with small faces, sometimes doesn't extend 1D->2D based on face area var boxGeometry = new BoxGeometry { Center = (rnd.NextInt(4) > 0) ? float3.zero : rnd.NextFloat3(-0.5f, 0.5f), Orientation = (rnd.NextInt(4) > 0) ? quaternion.identity : rnd.NextQuaternionRotation(), Size = rnd.NextFloat3(minSize, 1.0f) }; float maxBevelRadius = math.max(math.cmin((boxGeometry.Size - minSize) / (2.0f * (1.0f + float.Epsilon))), 0.0f); boxGeometry.BevelRadius = math.min(maxBevelRadius, convexRadius); return(BoxCollider.Create(boxGeometry)); } case ColliderType.Cylinder: { float minSize = 0.01f; // TODO - cylinder gets degenerate faces if radius-convexRadius=0 or height/2-convexRadius=0, decide how to handle this in CylinderCollider var cylinderGeometry = new CylinderGeometry { Center = (rnd.NextInt(4) > 0) ? float3.zero : rnd.NextFloat3(-0.5f, 0.5f), Orientation = (rnd.NextInt(4) > 0) ? quaternion.identity : rnd.NextQuaternionRotation(), Height = rnd.NextFloat(2.0f * minSize, 1f), Radius = rnd.NextFloat(minSize, 1.0f), SideCount = 20 }; var maxBevelRadius = math.max(math.min(cylinderGeometry.Height / 2, cylinderGeometry.Radius) - minSize, 0.0f); cylinderGeometry.BevelRadius = math.min(maxBevelRadius, convexRadius); return(CylinderCollider.Create(cylinderGeometry)); } default: throw new NotImplementedException(); } }
public override void InstanceSpawn() { hitCylinder = new CylinderCollider(new Vector3(x, z - 2, y), 4, 2); }
/// <summary> /// Generates a collider using a PhysicsShapeAuthoring and PhysicsMaterialsExtensionComponent /// </summary> /// <param name="shape"></param> /// <param name="shapeExt"></param> /// <param name="offsetPosition"></param> /// <param name="offsetRotation"></param> /// <returns></returns> BlobAssetReference <Unity.Physics.Collider> GenerateCollider(PhysicsShapeAuthoring shape, PhysicsMaterialsExtensionComponent shapeExt, out float3 offsetPosition, out quaternion offsetRotation) { CollisionFilter filter = new CollisionFilter { CollidesWith = shape.CollidesWith.Value, BelongsTo = shape.BelongsTo.Value, GroupIndex = 0 }; Unity.Physics.Material material = new Unity.Physics.Material { CollisionResponse = shape.CollisionResponse, CustomTags = shape.CustomTags.Value, Friction = shape.Friction.Value, FrictionCombinePolicy = shape.Friction.CombineMode, Restitution = shape.Restitution.Value, RestitutionCombinePolicy = shape.Restitution.CombineMode, EnableMassFactors = shapeExt != null ? shapeExt.EnableMassFactors : false, EnableSurfaceVelocity = shapeExt != null ? shapeExt.EnableSurfaceVelocity : false }; switch (shape.ShapeType) { case ShapeType.Box: var boxProperties = shape.GetBoxProperties(); offsetPosition = boxProperties.Center; offsetRotation = boxProperties.Orientation; return(Unity.Physics.BoxCollider.Create(boxProperties, filter, material)); case ShapeType.Capsule: var capsuleProperties = shape.GetCapsuleProperties(); var capsuleGeometry = new CapsuleGeometry { Radius = capsuleProperties.Radius, Vertex0 = capsuleProperties.Center - capsuleProperties.Height / 2 - capsuleProperties.Radius, Vertex1 = capsuleProperties.Center + capsuleProperties.Height / 2 - capsuleProperties.Radius }; offsetPosition = capsuleProperties.Center; offsetRotation = capsuleProperties.Orientation; return(Unity.Physics.CapsuleCollider.Create(capsuleGeometry, filter, material)); case ShapeType.Cylinder: var cylinderProperties = shape.GetCylinderProperties(); offsetPosition = cylinderProperties.Center; offsetRotation = cylinderProperties.Orientation; return(CylinderCollider.Create(cylinderProperties, filter, material)); case ShapeType.Sphere: var sphereProperties = shape.GetSphereProperties(out var orientation); var SphereGeometry = new SphereGeometry { Center = sphereProperties.Center, Radius = sphereProperties.Radius }; offsetPosition = sphereProperties.Center; offsetRotation = quaternion.identity; return(Unity.Physics.SphereCollider.Create(SphereGeometry, filter, material)); case ShapeType.ConvexHull: NativeList <float3> points = new NativeList <float3>(Allocator.Temp); shape.GetConvexHullProperties(points); var ConvexCollider = Unity.Physics.ConvexCollider.Create(points, shape.ConvexHullGenerationParameters, filter, material); // points.Dispose(); offsetPosition = float3.zero; offsetRotation = quaternion.identity; return(ConvexCollider); case ShapeType.Mesh: NativeList <float3> verts = new NativeList <float3>(Allocator.Temp); NativeList <int3> tris = new NativeList <int3>(Allocator.Temp); shape.GetMeshProperties(verts, tris); offsetPosition = float3.zero; offsetRotation = quaternion.identity; return(Unity.Physics.MeshCollider.Create(verts, tris, filter, material)); default: UnityEngine.Debug.LogWarning("GenerateCollider:: cannot generate collider for shapetype \"" + shape.ShapeType + "\""); offsetPosition = float3.zero; offsetRotation = quaternion.identity; return(new BlobAssetReference <Unity.Physics.Collider>()); } }
BlobAssetReference <Unity.Physics.Collider> GenerateCollider(PhysicsShapeAuthoring shape, out float3 offsetPosition, out quaternion offsetRotation) { switch (shape.ShapeType) { case ShapeType.Box: var boxProperties = shape.GetBoxProperties(); offsetPosition = boxProperties.Center; offsetRotation = boxProperties.Orientation; return(Unity.Physics.BoxCollider.Create(boxProperties)); case ShapeType.Capsule: var capsuleProperties = shape.GetCapsuleProperties(); var capsuleGeometry = new CapsuleGeometry { Radius = capsuleProperties.Radius, Vertex0 = capsuleProperties.Center - capsuleProperties.Height / 2 - capsuleProperties.Radius, Vertex1 = capsuleProperties.Center + capsuleProperties.Height / 2 - capsuleProperties.Radius }; offsetPosition = capsuleProperties.Center; offsetRotation = capsuleProperties.Orientation; return(Unity.Physics.CapsuleCollider.Create(capsuleGeometry)); case ShapeType.Cylinder: var cylinderProperties = shape.GetCylinderProperties(); offsetPosition = cylinderProperties.Center; offsetRotation = cylinderProperties.Orientation; return(CylinderCollider.Create(cylinderProperties)); case ShapeType.Sphere: var sphereProperties = shape.GetSphereProperties(out var orientation); var SphereGeometry = new SphereGeometry { Center = sphereProperties.Center, Radius = sphereProperties.Radius }; offsetPosition = sphereProperties.Center; offsetRotation = quaternion.identity; return(Unity.Physics.SphereCollider.Create(SphereGeometry)); case ShapeType.ConvexHull: NativeList <float3> points = new NativeList <float3>(Allocator.Temp); shape.GetConvexHullProperties(points); var ConvexCollider = Unity.Physics.ConvexCollider.Create(points, shape.ConvexHullGenerationParameters); // points.Dispose(); offsetPosition = float3.zero; offsetRotation = quaternion.identity; return(ConvexCollider); case ShapeType.Mesh: NativeList <float3> verts = new NativeList <float3>(Allocator.Temp); NativeList <int3> tris = new NativeList <int3>(Allocator.Temp); shape.GetMeshProperties(verts, tris); offsetPosition = float3.zero; offsetRotation = quaternion.identity; return(Unity.Physics.MeshCollider.Create(verts, tris)); default: UnityEngine.Debug.LogWarning("GenerateCollider:: cannot generate collider for shapetype \"" + shape.ShapeType + "\""); offsetPosition = float3.zero; offsetRotation = quaternion.identity; return(new BlobAssetReference <Unity.Physics.Collider>()); } }
protected override BlobAssetReference <Collider> ProduceColliderBlob(PhysicsShapeAuthoring shape) { var material = ProduceMaterial(shape); var collisionFilter = ProduceCollisionFilter(shape); BlobAssetReference <Collider> blob = default; switch (shape.ShapeType) { case ShapeType.Box: blob = BoxCollider.Create( shape.GetBakedBoxProperties(), collisionFilter, material); break; case ShapeType.Capsule: blob = CapsuleCollider.Create( shape.GetBakedCapsuleProperties(out var center, out var height, out var orientation), collisionFilter, material); break; case ShapeType.Sphere: blob = SphereCollider.Create( shape.GetBakedSphereProperties(out orientation), collisionFilter, material); break; case ShapeType.Cylinder: blob = CylinderCollider.Create( shape.GetBakedCylinderProperties(), collisionFilter, material); break; case ShapeType.Plane: shape.GetBakedPlaneProperties(out var v0, out var v1, out var v2, out var v3); blob = PolygonCollider.CreateQuad( v0, v1, v2, v3, collisionFilter, material); break; case ShapeType.ConvexHull: var pointCloud = new NativeList <float3>(65535, Allocator.Temp); shape.GetConvexHullProperties(pointCloud); if (pointCloud.Length == 0) { throw new InvalidOperationException( $"No vertices associated with {shape.name}. Add a {typeof(MeshFilter)} component or assign a readable {nameof(PhysicsShapeAuthoring.CustomMesh)}." ); } shape.GetBakedConvexProperties(pointCloud, out var hullGenerationParameters); RegisterConvexColliderDeferred( shape, pointCloud, hullGenerationParameters.ToRunTime(), collisionFilter, material ); pointCloud.Dispose(); break; case ShapeType.Mesh: const int defaultVertexCount = 2048; pointCloud = new NativeList <float3>(defaultVertexCount, Allocator.Temp); var triangles = new NativeList <int>((defaultVertexCount - 2) * 3, Allocator.Temp); shape.GetBakedMeshProperties(pointCloud, triangles); if (pointCloud.Length == 0 || triangles.Length == 0) { triangles.Dispose(); pointCloud.Dispose(); throw new InvalidOperationException( $"Invalid mesh data associated with {shape.name}. " + $"Add a {typeof(MeshFilter)} component or assign a {nameof(PhysicsShapeAuthoring.CustomMesh)}. " + "Ensure that you have enabled Read/Write on the mesh's import settings." ); } RegisterMeshColliderDeferred(shape, pointCloud, triangles, collisionFilter, material); triangles.Dispose(); pointCloud.Dispose(); break; default: throw new UnimplementedShapeException(shape.ShapeType); } return(blob); }
public override void InstanceSpawn() { hitCylinder = new CylinderCollider(new Vector3(x, 0, y), 200, 5); }