protected override void OnUpdate() { var commandBuffer = barrier.CreateCommandBuffer().AsParallelWriter(); var physicsWorld = buildPhysicsWorld.PhysicsWorld; var localToWorldFromEntity = GetComponentDataFromEntity <LocalToWorld>(true); Dependency = JobHandle.CombineDependencies(Dependency, buildPhysicsWorld.GetOutputDependency()); Entities .WithAll <LocalToWorld>() .WithReadOnly(localToWorldFromEntity) .WithReadOnly(physicsWorld) .ForEach((Entity entity, int entityInQueryIndex, ref Sticky sticky) => { if (sticky.StickAttempts-- <= 0) { commandBuffer.RemoveComponent <Sticky>(entityInQueryIndex, entity); commandBuffer.AddComponent <StickyFailed>(entityInQueryIndex, entity); return; } var worldPosition = localToWorldFromEntity[entity].Position; var collider = SphereCollider.Create( new SphereGeometry() { Center = worldPosition, Radius = sticky.Radius }, sticky.Filter ); unsafe { var castInput = new ColliderCastInput() { Collider = (Collider *)collider.GetUnsafePtr(), Orientation = quaternion.LookRotationSafe(sticky.WorldDirection, math.up()) }; if (!physicsWorld.CastCollider(castInput, out ColliderCastHit hit)) { return; } commandBuffer.AddComponent(entityInQueryIndex, entity, new Parent { Value = hit.Entity }); commandBuffer.RemoveComponent <Sticky>(entityInQueryIndex, entity); } }) .WithName("StickyJob") .ScheduleParallel(); barrier.AddJobHandleForProducer(Dependency); }
protected override JobHandle OnUpdate(JobHandle inputDeps) { var commandBuffer = barrier.CreateCommandBuffer().ToConcurrent(); var physicsWorld = buildPhysicsWorld.PhysicsWorld; var localToWorldFromEntity = GetComponentDataFromEntity <LocalToWorld>(true); var createJob = Entities .WithChangeFilter <NavNeedsDestination>() .WithReadOnly(localToWorldFromEntity) .WithReadOnly(physicsWorld) .ForEach((Entity entity, int entityInQueryIndex, ref NavAgent agent, in NavNeedsDestination destination) => { var collider = SphereCollider.Create( new SphereGeometry() { Center = destination.Value, Radius = 1 }, new CollisionFilter() { BelongsTo = NavUtil.ToBitMask(NavConstants.COLLIDER_LAYER), CollidesWith = NavUtil.ToBitMask(NavConstants.SURFACE_LAYER), } ); unsafe { var castInput = new ColliderCastInput() { Collider = (Collider *)collider.GetUnsafePtr(), Orientation = quaternion.identity }; if (!physicsWorld.CastCollider(castInput, out ColliderCastHit hit) || hit.RigidBodyIndex == -1) { commandBuffer.RemoveComponent <NavNeedsDestination>(entityInQueryIndex, entity); // Ignore invalid destinations. return; } agent.DestinationSurface = physicsWorld.Bodies[hit.RigidBodyIndex].Entity; agent.LocalDestination = NavUtil.MultiplyPoint3x4( math.inverse(localToWorldFromEntity[agent.DestinationSurface].Value), destination.Value ) + agent.Offset; commandBuffer.AddComponent <NavPlanning>(entityInQueryIndex, entity); } }) .WithName("CreateDestinationJob") .Schedule(JobHandle.CombineDependencies( inputDeps, buildPhysicsWorld.FinalJobHandle )); barrier.AddJobHandleForProducer(createJob); return(createJob); }
protected override void OnCreate() { _entityCommandBufferSystem = World.GetOrCreateSystem <BeginInitializationEntityCommandBufferSystem>(); _buildPhysicsWorldSystem = World.GetOrCreateSystem <BuildPhysicsWorld>(); _query = GetEntityQuery(new EntityQueryDesc() { All = new ComponentType[] { ComponentType.ReadOnly <Translation>(), ComponentType.ReadOnly <Rotation>(), typeof(PhysicsVelocity), ComponentType.ReadOnly <PhysicsMass>(), typeof(FighterComponent), }, }); _targetableQuery = GetEntityQuery(new EntityQueryDesc() { All = new ComponentType[] { ComponentType.ReadOnly <FighterTargetable>(), }, }); #if SEARCHING cast_collider_ = SphereCollider.Create(new float3(0, 0, 0) /* center */, 1f /* radius */, new CollisionFilter { BelongsTo = ~0u, CollidesWith = ~0u, }, null /* material */); // create approximate hexagonal directions. // 2 // 6 4 // 0 // 5 3 // 1 local_search_rotations_ = new NativeArray <quaternion>(FighterConfig.SEARCH_NUM, Allocator.Persistent); local_search_rotations_[0] = quaternion.Euler(0, 0, 0); local_search_rotations_[1] = quaternion.Euler(math.radians(10f), 0, 0); local_search_rotations_[2] = quaternion.Euler(math.radians(-10f), 0, 0); local_search_rotations_[3] = quaternion.Euler(math.radians(5f), math.radians(5f * 1.7320508f), 0); local_search_rotations_[4] = quaternion.Euler(math.radians(-5f), math.radians(5f * 1.7320508f), 0); local_search_rotations_[5] = quaternion.Euler(math.radians(5f), math.radians(-5f * 1.7320508f), 0); local_search_rotations_[6] = quaternion.Euler(math.radians(-5f), math.radians(-5f * 1.7320508f), 0); #endif _lastPrimaryFighterPos = new NativeArray <float3>(1, Allocator.Persistent); _lastPrimaryTargetPos = new NativeArray <float3>(1, Allocator.Persistent); #if RECORDING controller_buffer_ = new NativeList <ControllerUnit>(ControllerBuffer.MAX_FRAMES, Allocator.Persistent); controller_device_ = new ControllerDevice(controller_buffer_); controller_device_.start(Time.GetCurrent()); #else _controllerBuffer = ControllerBuffer.Load <ControllerUnit>("controller.bin"); #endif }
public unsafe static NativeArray <Entity> GetNearbyObjects(Entity e, float3 translation, quaternion rotation, PhysicsCollider physicsCollider, BuildPhysicsWorld physicsWorldSystem) { CollisionWorld world = physicsWorldSystem.PhysicsWorld.CollisionWorld;; NativeArray <Entity> context; var filter = new CollisionFilter() { BelongsTo = ~0u, CollidesWith = ~0u, // all 1s, so all layers, collide with everything GroupIndex = 0 }; SphereGeometry sphereGeometry = new SphereGeometry() { Center = float3.zero, Radius = 3f }; BlobAssetReference <Collider> sphereCollider = SphereCollider.Create(sphereGeometry, filter); NativeList <ColliderCastHit> colliderHit = new NativeList <ColliderCastHit>(); ColliderCastInput input = new ColliderCastInput() { Collider = (Collider *)sphereCollider.GetUnsafePtr(), Orientation = quaternion.identity, Start = translation, End = translation }; if (world.CastCollider(input, ref colliderHit)) { int compteur = 0; context = new NativeArray <Entity>(colliderHit.Length, Allocator.Temp); foreach (var collider in colliderHit) { context[compteur] = collider.Entity; compteur++; } Debug.Log(context.Length); } else { context = new NativeArray <Entity>(0, Allocator.Temp); Debug.Log("inhere"); } return(context); }
static public BlobAssetReference <Collider> ProduceColliderBlob (this UnityEngine.SphereCollider shape, int groupIndex) { var worldCenter = math.mul(shape.transform.localToWorldMatrix, new float4(shape.center, 1f)); var shapeFromWorld = math.inverse( new float4x4(new RigidTransform(shape.transform.rotation, shape.transform.position)) ); var center = math.mul(shapeFromWorld, worldCenter).xyz; var linearScale = (float3)shape.transform.lossyScale; var radius = shape.radius * math.cmax(math.abs(linearScale)); return(SphereCollider.Create( new SphereGeometry { Center = center, Radius = radius }, GetFilter(shape, groupIndex) //ProduceCollisionFilter( shape ), //ProduceMaterial( shape ) )); }
protected unsafe override void OnUpdate() { float time = Time.DeltaTime; float dirForward = Input.GetAxis("Vertical"); float dirAngle = Input.GetAxis("Horizontal"); CollisionWorld world = physicsWorldSystem.PhysicsWorld.CollisionWorld; Entities.ForEach((Entity e, ref PhysicsVelocity physicsVelocity, ref Rotation rotation, ref Translation translation, ref PhysicsCollider physicsCollider) => { var filter = new CollisionFilter() { BelongsTo = ~0u, CollidesWith = ~0u, // all 1s, so all layers, collide with everything GroupIndex = 0 }; SphereGeometry sphereGeometry = new SphereGeometry() { Center = translation.Value, Radius = 10f }; BlobAssetReference <Collider> sphereCollider = SphereCollider.Create(sphereGeometry, filter); NativeList <ColliderCastHit> colliderHit = new NativeList <ColliderCastHit>(Allocator.Temp); ColliderCastInput input = new ColliderCastInput() { Collider = (Collider *)sphereCollider.GetUnsafePtr(), Orientation = quaternion.identity, Start = translation.Value, End = translation.Value }; world.CastCollider(input, ref colliderHit); //for (int i = 0; i < colliderHit.Length; i++) //{ //Debug.Log(colliderHit[i].Entity + " / " + e.Index); //} //Debug.Log(colliderHit.Length); colliderHit.Dispose(); //NativeArray<Entity> context = GetNearbyObjects(e, translation.Value, rotation.Value, physicsCollider, test); //context.Dispose(); }).ScheduleParallel(); }
private void Explode(EntityCommandBuffer ecb) { // If sphere doesńt exist add it if (!explosion.hasExplosionEntity) { // This code will run on each entity with a SpawnerTag (i.e only the Spawner entity in the scene). Entities .WithoutBurst() .WithAll <SpawnerTag>() .ForEach((Entity entity, int entityInQueryIndex, in Explosion explosion, in Spawner spawner) => { // Instantiate the sphere entity var theInstance = ecb.Instantiate(explosion.spherePrefab); // Set the position of the sphere entity to be placed in the middle of the cube structure ecb.SetComponent(theInstance, new Translation { Value = new float3(((float)spawner.maxWidth - 1) / 2, 0, ((float)spawner.maxDeep - 1) / 2) }); // Add a Spawner on the sphere entity so its possible to get the maxWidth in the entities.foreach at the end of this explode method ecb.AddComponent(theInstance, new Spawner { maxWidth = spawner.maxWidth, maxDeep = spawner.maxDeep }); // Add an ExplodeTag on the sphere entity so its possible to find it with the entities.foreach at the end of this explode method ecb.AddComponent(theInstance, new ExplodeTag()); // Set the radius of the sphere ecb.AddComponent(theInstance, new Scale { Value = 1f }); // Adding SphereCollider component and set the radius of it to the same as the sphere radius ecb.AddComponent(theInstance, new PhysicsCollider { Value = SphereCollider.Create(new SphereGeometry { Center = float3.zero, Radius = 1f }, CollisionFilter.Default, Unity.Physics.Material.Default) }); }).Run();
protected override void OnCreate() { this.buildPhysicsWorldSystem = World.GetOrCreateSystem <BuildPhysicsWorld>(); this.stepPhysicsWorldSystem = World.GetOrCreateSystem <StepPhysicsWorld>(); //this.ImpulseGroup = GetEntityQuery( new EntityQueryDesc //{ // All = new ComponentType[] { typeof( MoveChData ), } //} ); var geom = new SphereGeometry() { Center = float3.zero, Radius = 0.15f, }; var filter = new CollisionFilter { BelongsTo = 1u << 23, CollidesWith = 1u << 20 | 1u << 22,// | 1u<<23, GroupIndex = 0, }; this.movebodySphereCollider = SphereCollider.Create(geom, filter); }
protected override void OnUpdate() { var commandBuffer = barrier.CreateCommandBuffer().AsParallelWriter(); var elapsedSeconds = (float)Time.ElapsedTime; var localToWorldFromEntity = GetComponentDataFromEntity <LocalToWorld>(true); var physicsWorld = buildPhysicsWorld.PhysicsWorld; var settings = navSystem.Settings; Entities .WithNone <NavProblem>() .WithChangeFilter <NavDestination>() .WithReadOnly(localToWorldFromEntity) .WithReadOnly(physicsWorld) .ForEach((Entity entity, int entityInQueryIndex, ref NavAgent agent, in NavDestination destination) => { if (elapsedSeconds - agent.DestinationSeconds < settings.DestinationRateLimitSeconds) { commandBuffer.AddComponent <NavDestination>(entityInQueryIndex, entity, destination); // So that the change filter applies next frame. return; } var collider = SphereCollider.Create( new SphereGeometry() { Center = destination.WorldPoint, Radius = settings.DestinationSurfaceColliderRadius }, new CollisionFilter() { BelongsTo = NavUtil.ToBitMask(settings.ColliderLayer), CollidesWith = NavUtil.ToBitMask(settings.SurfaceLayer), } ); unsafe { var castInput = new ColliderCastInput() { Collider = (Collider *)collider.GetUnsafePtr(), Orientation = quaternion.identity }; if (!physicsWorld.CastCollider(castInput, out ColliderCastHit hit)) { commandBuffer.RemoveComponent <NavDestination>(entityInQueryIndex, entity); // Ignore invalid destinations. return; } var localDestination = destination.WorldPoint.ToLocal(localToWorldFromEntity[hit.Entity]) + agent.Offset; if (NavUtil.ApproxEquals(localDestination, agent.LocalDestination, destination.Tolerance)) { return; } if (destination.Teleport) { commandBuffer.SetComponent <Parent>(entityInQueryIndex, entity, new Parent { Value = hit.Entity }); commandBuffer.SetComponent <Translation>(entityInQueryIndex, entity, new Translation { Value = localDestination }); commandBuffer.RemoveComponent <NavDestination>(entityInQueryIndex, entity); return; } agent.DestinationSurface = physicsWorld.Bodies[hit.RigidBodyIndex].Entity; agent.LocalDestination = localDestination; agent.DestinationSeconds = elapsedSeconds; commandBuffer.AddComponent <NavPlanning>(entityInQueryIndex, entity); } }) .WithName("CreateDestinationJob") .ScheduleParallel(); barrier.AddJobHandleForProducer(Dependency); buildPhysicsWorld.AddInputDependencyToComplete(Dependency); }
public static BlobAssetReference <Collider> CreateCollider(UnityEngine.Mesh mesh, ColliderType type) { switch (type) { case ColliderType.Sphere: { Bounds bounds = mesh.bounds; return(SphereCollider.Create(new SphereGeometry { Center = bounds.center, Radius = math.cmax(bounds.extents) })); } case ColliderType.Triangle: { return(PolygonCollider.CreateTriangle(mesh.vertices[0], mesh.vertices[1], mesh.vertices[2])); } case ColliderType.Quad: { // We assume the first 2 triangles of the mesh are a quad with a shared edge // Work out a correct ordering for the triangle int[] orderedIndices = new int[4]; // Find the vertex in first triangle that is not on the shared edge for (int i = 0; i < 3; i++) { if ((mesh.triangles[i] != mesh.triangles[3]) && (mesh.triangles[i] != mesh.triangles[4]) && (mesh.triangles[i] != mesh.triangles[5])) { // Push in order or prev, unique, next orderedIndices[0] = mesh.triangles[(i - 1 + 3) % 3]; orderedIndices[1] = mesh.triangles[i]; orderedIndices[2] = mesh.triangles[(i + 1) % 3]; break; } } // Find the vertex in second triangle that is not on a shared edge for (int i = 3; i < 6; i++) { if ((mesh.triangles[i] != orderedIndices[0]) && (mesh.triangles[i] != orderedIndices[1]) && (mesh.triangles[i] != orderedIndices[2])) { orderedIndices[3] = mesh.triangles[i]; break; } } return(PolygonCollider.CreateQuad( mesh.vertices[orderedIndices[0]], mesh.vertices[orderedIndices[1]], mesh.vertices[orderedIndices[2]], mesh.vertices[orderedIndices[3]])); } case ColliderType.Box: { Bounds bounds = mesh.bounds; return(BoxCollider.Create(new BoxGeometry { Center = bounds.center, Orientation = quaternion.identity, Size = 2.0f * bounds.extents, BevelRadius = 0.0f })); } case ColliderType.Capsule: { Bounds bounds = mesh.bounds; float min = math.cmin(bounds.extents); float max = math.cmax(bounds.extents); int x = math.select(math.select(2, 1, min == bounds.extents.y), 0, min == bounds.extents.x); int z = math.select(math.select(2, 1, max == bounds.extents.y), 0, max == bounds.extents.x); int y = math.select(math.select(2, 1, (1 != x) && (1 != z)), 0, (0 != x) && (0 != z)); float radius = bounds.extents[y]; float3 vertex0 = bounds.center; vertex0[z] = -(max - radius); float3 vertex1 = bounds.center; vertex1[z] = (max - radius); return(CapsuleCollider.Create(new CapsuleGeometry { Vertex0 = vertex0, Vertex1 = vertex1, Radius = radius })); } case ColliderType.Cylinder: // TODO: need someone to add throw new NotImplementedException(); case ColliderType.Convex: { NativeArray <float3> points = new NativeArray <float3>(mesh.vertices.Length, Allocator.TempJob); for (int i = 0; i < mesh.vertices.Length; i++) { points[i] = mesh.vertices[i]; } BlobAssetReference <Collider> collider = ConvexCollider.Create(points, default, CollisionFilter.Default); points.Dispose(); return(collider); }
public void Execute(Entity e, int index, ref BoubleComp b, ref Scale scale, ref PhysicsVelocity vel, ref PhysicsCollider pc, ref Translation trans) { b = bubMap[e]; var s = b.gravRadius * sizeToGrav * 2; if (s != scale.Value && s <= maxScale) { scale.Value = s; var mat = PMat.Default; mat.Flags = absorb ? PMat.MaterialFlags.IsTrigger : 0; pc.Value = PSphereCollider.Create( new SphereGeometry { Radius = b.gravRadius * sizeToGrav }, CollisionFilter.Default, mat ); b.colorChanged = true; } t = vel.Linear + (float3)b.velc; tzero = float3.zero; tzero.y = b.pos.y; dist = math.distance(b.pos, tzero); if (bounce) // simply filp the velocity of axis when it has surpassed the max { if (b.pos.y > maxHeight && t.y > 0) { t.y = -t.y; } if (b.pos.y < -maxHeight && t.y < 0) { t.y = -t.y; } if (dist > maxRad) { if (math.abs(b.pos.x + t.x) > math.abs(b.pos.x - t.x)) { t.x = -t.x; } if (math.abs(b.pos.z + t.z) > math.abs(b.pos.z - t.z)) { t.z = -t.z; } } } else // teleport to the other axis max and keep velocity { if (dist > maxRad) { tzero = b.pos; tzero.y = 0; tzero = math.normalize(-tzero) * maxRad; tzero.y = b.pos.y; trans.Value = tzero; } if (math.abs(b.pos.y) > maxHeight) { if (b.pos.y > maxHeight) { b.pos.y = -maxHeight; } if (b.pos.y < -maxHeight) { b.pos.y = maxHeight; } trans.Value = b.pos; } } vel.Linear = t; }
protected override void OnUpdate() { var commandBuffer = barrier.CreateCommandBuffer().AsParallelWriter(); var physicsWorld = buildPhysicsWorld.PhysicsWorld; var localToWorldFromEntity = GetComponentDataFromEntity <LocalToWorld>(true); Dependency = JobHandle.CombineDependencies(Dependency, buildPhysicsWorld.GetOutputDependency()); Entities .WithNone <NavHasProblem>() .WithChangeFilter <NavNeedsDestination>() .WithReadOnly(localToWorldFromEntity) .WithReadOnly(physicsWorld) .ForEach((Entity entity, int entityInQueryIndex, ref NavAgent agent, in NavNeedsDestination needsDestination) => { var collider = SphereCollider.Create( new SphereGeometry() { Center = needsDestination.Destination, Radius = NavConstants.DESTINATION_SURFACE_COLLIDER_RADIUS }, new CollisionFilter() { BelongsTo = NavUtil.ToBitMask(NavConstants.COLLIDER_LAYER), CollidesWith = NavUtil.ToBitMask(NavConstants.SURFACE_LAYER), } ); unsafe { var castInput = new ColliderCastInput() { Collider = (Collider *)collider.GetUnsafePtr(), Orientation = quaternion.identity }; if (!physicsWorld.CastCollider(castInput, out ColliderCastHit hit)) { commandBuffer.RemoveComponent <NavNeedsDestination>(entityInQueryIndex, entity); // Ignore invalid destinations. return; } var destination = NavUtil.MultiplyPoint3x4( math.inverse(localToWorldFromEntity[hit.Entity].Value), needsDestination.Destination ) + agent.Offset; if (needsDestination.Teleport) { commandBuffer.SetComponent <Parent>(entityInQueryIndex, entity, new Parent { Value = hit.Entity }); commandBuffer.SetComponent <Translation>(entityInQueryIndex, entity, new Translation { Value = destination }); commandBuffer.RemoveComponent <NavNeedsDestination>(entityInQueryIndex, entity); return; } agent.DestinationSurface = physicsWorld.Bodies[hit.RigidBodyIndex].Entity; agent.LocalDestination = destination; commandBuffer.AddComponent <NavPlanning>(entityInQueryIndex, entity); } }) .WithName("CreateDestinationJob") .ScheduleParallel(); barrier.AddJobHandleForProducer(Dependency); }