public static MeshBuilder Extrude(this MeshBuilder mb, ray r, float radius, vec3 force) { List <int> inside = mb.Select(r, radius, out inside); return(mb); }
void Update() { //Move transform.Translate(0, 0, speed * time.DeltaTime); //Detect whether there is an obstacle or a Player Ray ray = new ray(transform.position, transform.forward); RaycastHit hit; if Physics.SphereCast(ray, 0.75f, out hit) //Using SphereCast to detect collision easier { GameObject hitObject = hit.transform.gameObject; if (hitObject.GetComponent <PlayerCharacter>()) //Facing a Player -> shooting { if (fireball == null) { //Instantiate a fireball, and set its position & rotation fireball = Instantiate(FireBallPrefab) as GameObject; //FireBall Script will be attached to FireBallPrefab, setting its speed and damage. fireball.transform.position = transform.TransformPoint(Vector3.forward * 1.5f); //from local to global! fireball.transform.rotation = transform.rotation; } } else if (hit.distance < obstacleRange) //Facing an obstacle -> turning around { float angle = Random.Range(-100, 100); //randomly pick another direction to move forward transform.Rotate(0, angle, 0); } } }
void Update(){ RaycastHit hit; Ray ray = new ray(transform.position, transform.forward); if(Physics.Raycast(ray, out hit, ZombieRange)) { ApplyDamage(); } }
public static vec3 intersect(this plane pl, ray r) { float d = pl.normal ^ -r.direction; if (d == 0) { // line is parallel to plane return(vec3.empty); } float n = pl.d + (pl.normal ^ r.origin); return(r.origin + r.direction * (n / d)); }
public override zwischenSpeicher scatter(ray r_in, hit_record rec, Vektor attenuation, ray scattered) { zwischenSpeicher zw = new zwischenSpeicher(); Vektor reflected = Vektor.reflect(Vektor.unit_Vektor(r_in.Direction), rec.normal); scattered = new ray(rec.p, reflected + fuzz * Vektor.random_in_unit_sphere()); attenuation = albedo; zw.scattered = scattered; zw.attenuation = attenuation; zw.IsTrue = (Vektor.dot(scattered.Direction, rec.normal) > 0.0); return(zw); }
public static line3_segment intersect(this ray a, ray b) { vec3 d = a.direction % b.direction; if (d.isZero) { return(line3_segment.empty); } plane pa = new plane(a.origin, (a.direction % d).normalized); plane pb = new plane(b.origin, (b.direction % d).normalized); return(new line3_segment(pa.intersect(b), pb.intersect(a))); }
public List <int> Select(ray r, float radius, out List <int> sidea) { sidea = new List <int>(vertices.Count); for (int i = 0; i < vertices.Count; i++) { if (r.distance(vertices[i]) < radius) { sidea.Add(i); } } return(sidea); }
public BroadPhaseSphereCastJob(NativeArray <Entity> colliderEntities, NativeArray <HitCollisionData> colliderData, NativeArray <uint> flags, NativeArray <sphere> bounds, Entity exclude, Entity include, uint flagMask, ray ray, float distance, float radius) { this.ColliderEntities = colliderEntities; this.ColliderData = colliderData; this.flags = flags; this.ray = ray; this.include = include; this.exclude = exclude; this.flagMask = flagMask; this.bounds = bounds; rayDist = distance; rayRadius = radius; result = new NativeList <Entity>(colliderEntities.Length, Allocator.TempJob); }
public vec3 cast(ray r, out float d) { d = normal ^ r.direction; if (d == 0) { return(vec3.empty); } d = -((r.origin ^ normal) + pd) / d; if (d < 0) { return(vec3.empty); } return(r.origin + d * r.direction); }
public SphereCastSingleJob(EntityManager entityManager, Entity entity, ray ray, float distance, float radius, int tick) { this.ray = ray; rayDist = distance; rayRadius = radius; hitCollObject = entity; var collData = entityManager.GetComponentData <HitCollisionData>(entity); var histIndex = collData.GetHistoryIndex(tick); transformBuffer = new NativeSlice <HitCollisionData.TransformHistory>( entityManager.GetBuffer <HitCollisionData.TransformHistory>(entity).ToNativeArray(), histIndex * HitCollisionData.k_maxColliderCount); sphereArray = entityManager.GetBuffer <HitCollisionData.Sphere>(entity); capsuleArray = entityManager.GetBuffer <HitCollisionData.Capsule>(entity); boxArray = entityManager.GetBuffer <HitCollisionData.Box>(entity); result = new NativeArray <HitCollisionData.CollisionResult>(1, Allocator.TempJob); }
public override zwischenSpeicher scatter(ray r_in, hit_record rec, Vektor attenuation, ray scattered) { zwischenSpeicher zw = new zwischenSpeicher(); var scatter_direction = rec.normal + Vektor.random_unit_vektor(); //catch degenerate scatter direction if (scatter_direction.near_zero()) { scatter_direction = rec.normal; } scattered = new ray(rec.p, scatter_direction); attenuation = albedo; zw.scattered = scattered; zw.attenuation = attenuation; zw.IsTrue = true; return(zw); }
List <ray> RAYS(List <ray> r) { List <ray> rays = r; List <ray> temp2 = new List <ray>(); do { ray temp = new ray(); foreach (ray kek in rays) { if (kek.distance > temp.distance) { temp = kek; } } rays.Remove(temp); temp2.Add(temp); foreach (ray kek in rays) { if (kek.x < temp.x && kek.y > temp.x) { kek.y = temp.x; } else if (kek.x < temp.y && kek.y > temp.y) { kek.x = temp.y; } } rays.RemoveAll(kek => kek.x >= temp.x && kek.y <= temp.y); } while (rays.Count > 0); temp2.Sort(delegate(ray a, ray b) { return(a.x.CompareTo(b.x)); }); return(temp2); }
public static void Prim(ray ray, Color color, float duration = 0) { Debug.DrawLine(ray.origin, ray.origin + ray.direction * 1000, color, duration); Sphere(ray.origin, 0.03f, color, duration); }
public List <Vector2> Path(Point start, Point finish) { if (start.x < mapWeight && start.y < mapHeight && finish.x < mapWeight && finish.y < mapHeight && start.x >= 0 && start.y >= 0 && finish.x >= 0 && finish.y >= 0 && Map[start.x, start.y] && Map[finish.x, finish.y]) { start.PathLenghtToTheEnd = Mathf.Abs(start.x - finish.x) + Mathf.Abs(start.y - finish.y); start.UpdateFullLength(); List <Point> Open = new List <Point> { start }; List <Point> Closed = new List <Point> { }; Point currentpoint = start; while (Open.Count > 0) { Closed.Add(currentpoint); Open.RemoveAll(match => match == currentpoint); if (currentpoint == finish) { break; } foreach (Point kek in neibr(currentpoint, finish)) { if (Closed.FindIndex(match => match == kek) < 0) { if (Open.FindIndex(match => match == kek) < 0) { Open.Add(kek); } else { if (Open.Find(match => match == kek).PathLenghtFromStart > kek.PathLenghtFromStart) { Open[Open.FindIndex(match => match == kek)] = kek; } } } } Point h = Open[0]; float exp = Dist(finish, h); foreach (Point kek in Open) { if (kek.FullLenght < h.FullLenght || (kek.FullLenght == h.FullLenght && Dist(kek, finish) < exp)) { h = kek; exp = Dist(h, finish); } } currentpoint = h; } List <Point> path = new List <Point> { }; if (currentpoint == finish) { while (currentpoint != start) { path.Insert(0, currentpoint); currentpoint = currentpoint.cameFrom; } } List <Vector2> v = new List <Vector2> { }; for (int i = 0; i < path.Count; ++i) { v.Add(new Vector2(path[i].x, path[i].y)); //Debug.Log(v[i] + "||"); } v.Insert(0, new Vector2(start.x, start.y)); List <ray> rays = new List <ray>(); for (int i = 0; i < v.Count - 1; ++i) { ray w = new ray(); int LayerMask = 0; LayerMask = ~LayerMask; for (int j = i + 1; j < v.Count; ++j) { if (!(Physics2D.Raycast(v[i] + Vector2.up * 0.9f + Vector2.right * 0.1f, v[j] - v[i], (v[j] - v[i]).magnitude - 0.1f, LayerMask) || Physics2D.Raycast(v[i] + Vector2.right * 0.9f + Vector2.up * 0.1f, v[j] - v[i], (v[j] - v[i]).magnitude - 0.1f, LayerMask) || Physics2D.Raycast(v[i] + Vector2.up * 0.1f + Vector2.right * 0.1f, v[j] - v[i], (v[j] - v[i]).magnitude - 0.1f, LayerMask) || Physics2D.Raycast(v[i] + Vector2.right * 0.9f + Vector2.up * 0.9f, v[j] - v[i], (v[j] - v[i]).magnitude - 0.1f, LayerMask))) { w = new ray(i, j, Dist(v[j], v[i])); } else { break; } } rays.Add(w); //Debug.Log(w.x + " " + w.y); } rays = RAYS(rays); List <Vector2> L = new List <Vector2>(); L.Add(new Vector2(v[rays[0].x].x + 0.5f, v[rays[0].x].y + 0.5f)); // Debug.Log(rays[0].x); for (int i = 0; i < rays.Count; ++i) { L.Add(new Vector2(v[rays[i].y].x + 0.5f, v[rays[i].y].y + 0.5f)); // L.Add(v[rays[i].y]); // Debug.Log(rays[i].y); } return(L); } else { return(new List <Vector2> { }); } }
depth <= 0 ? Vector3.Zero : ShadeCore(ray, scene, p, depth);
public void set_face_normal(ray r, Vektor outward_normal) { front_face = Vektor.dot(r.Direction, outward_normal) < 0; normal = front_face ? outward_normal : (outward_normal * -1); }
public vec3 cast(ray r) { float d; return(cast(r, out d)); }
public static void PrepareColliders(ref ComponentArray <HitCollisionHistory> collections, int tick, int mask, Entity forceExcluded, Entity forceIncluded, ray ray, float rayDist, float radius) { Profiler.BeginSample("HitCollisionHistory.PrepareColliders [SphereCast]"); for (var i = 0; i < collections.Length; i++) { var collection = collections[i]; if (!IsRelevant(collection, mask, forceExcluded, forceIncluded)) { collection.DisableHitCollision(); continue; } var stateIndex = collection.GetStateIndex(tick); Profiler.BeginSample("-capsule test"); var boundCenter = collection.boundsCenterBuffer[stateIndex]; var rayEnd = ray.origin + ray.direction * rayDist; var closestPointOnRay = coll.ClosestPointOnLineSegment(ray.origin, rayEnd, boundCenter); var dist = math.distance(closestPointOnRay, boundCenter); var boundsHit = dist < collection.settings.boundsRadius + radius; Profiler.EndSample(); if (boundsHit) { collection.EnableCollisionForIndex(stateIndex); } else { collection.DisableHitCollision(); } if (HitCollisionModule.ShowDebug.IntValue > 0) { DebugPrimitiveModule.ClearChannel(HitCollisionModule.PrimDebugChannel); DebugPrimitiveModule.CreateCapsulePrimitive(HitCollisionModule.PrimDebugChannel, ray.origin + ray.direction * radius, ray.origin + ray.direction * (rayDist - radius), radius, Color.yellow, 5); DebugPrimitiveModule.CreateSpherePrimitive(HitCollisionModule.PrimDebugChannel, boundCenter, collection.settings.boundsRadius, boundsHit ? Color.yellow : Color.gray, 5); } } Profiler.EndSample(); }
public static void PrepareColliders(ref ComponentArray <HitCollisionHistory> collections, int tick, int mask, Entity forceExcluded, Entity forceIncluded, ray ray, float rayDist) { Profiler.BeginSample("HitCollisionHistory.PrepareColliders [Ray]"); // Rollback for (var i = 0; i < collections.Length; i++) { var collection = collections[i]; if (!IsRelevant(collection, mask, forceExcluded, forceIncluded)) { collection.DisableHitCollision(); continue; } var stateIndex = collection.GetStateIndex(tick); Profiler.BeginSample("-raycast"); var sphere = primlib.sphere(collection.boundsCenterBuffer[stateIndex], collection.settings.boundsRadius); var boundsHit = coll.RayCast(sphere, ray, rayDist); Profiler.EndSample(); if (boundsHit) { collection.EnableCollisionForIndex(stateIndex); } else { collection.DisableHitCollision(); } if (HitCollisionModule.ShowDebug.IntValue > 0) { DebugPrimitiveModule.ClearChannel(HitCollisionModule.PrimDebugChannel); DebugPrimitiveModule.CreateLinePrimitive(HitCollisionModule.PrimDebugChannel, ray.origin, ray.origin + ray.direction * rayDist, Color.yellow, 5); DebugPrimitiveModule.CreateSpherePrimitive(HitCollisionModule.PrimDebugChannel, sphere.center, sphere.radius, boundsHit ? Color.yellow : Color.gray, 5); } } Profiler.EndSample(); }
public virtual zwischenSpeicher Hit(ray r, double t_min, double t_max, hit_record rec) { zwischenSpeicher zw = new zwischenSpeicher(); return(zw); }
public virtual zwischenSpeicher scatter(ray r_in, hit_record rec, Vektor attenuation, ray scattered) { zwischenSpeicher zw = new zwischenSpeicher(); return(zw); }
public void find_region(bool account_for_overlap = false) { int similar_counter; update_subject(); top_corner = new Vector3(subject.joints[0].X, subject.joints[0].Y, subject.joints[0].Z); bottom_corner = new Vector3(subject.joints[0].X, subject.joints[0].Y, subject.joints[0].Z); max_radius = subject.joints[0].Radius; for (int i = 1; i < subject.joints.Length; i++) { if (subject.joints[i].X > top_corner.x) { top_corner.x = subject.joints[i].X; } if (subject.joints[i].Y > top_corner.y) { top_corner.y = subject.joints[i].Y; } if (subject.joints[i].Z > top_corner.z) { top_corner.z = subject.joints[i].Z; } if (subject.joints[i].Radius > max_radius) { max_radius = subject.joints[i].Radius; } if (subject.joints[i].X < bottom_corner.x) { bottom_corner.x = subject.joints[i].X; } if (subject.joints[i].Y < bottom_corner.y) { bottom_corner.y = subject.joints[i].Y; } if (subject.joints[i].Z < bottom_corner.z) { bottom_corner.z = subject.joints[i].Z; } if (account_for_overlap) { similar_counter = 0; for (int j = 0; j < subject.joints.Length; j++) { if ((new Vector3(subject.joints[j].X, subject.joints[j].Y, subject.joints[j].Z) - new Vector3(subject.joints[i].X, subject.joints[i].Y, subject.joints[i].Z)).magnitude <= subject.joints[i].Radius + subject.joints[j].Radius) { similar_counter++; } if ((subject.joints[i].Radius + subject.joints[j].Radius) * similar_counter > max_radius) { max_radius = (subject.joints[i].Radius + subject.joints[j].Radius) * similar_counter; } } } } bool upper_collision = true; bool lower_collision = true; // bool bottom_inside = true, top_inside = true; while (lower_collision && upper_collision) { max_radius = max_radius * 2; if (upper_collision) { top_corner = top_corner + new Vector3(max_radius, max_radius, max_radius); } if (lower_collision) { bottom_corner = bottom_corner - new Vector3(max_radius, max_radius, max_radius); } int number_of_steps = 10; var casting_rays = new ray[12]; casting_rays[0] = new ray(bottom_corner, new Vector3(top_corner.x, bottom_corner.y, bottom_corner.z), number_of_steps); casting_rays[1] = new ray(bottom_corner, new Vector3(bottom_corner.x, bottom_corner.y, top_corner.z), number_of_steps); casting_rays[2] = new ray(bottom_corner, new Vector3(bottom_corner.x, top_corner.y, bottom_corner.z), number_of_steps); casting_rays[3] = new ray(top_corner, new Vector3(bottom_corner.x, top_corner.y, top_corner.z), number_of_steps); casting_rays[4] = new ray(top_corner, new Vector3(top_corner.x, top_corner.y, bottom_corner.z), number_of_steps); casting_rays[5] = new ray(top_corner, new Vector3(top_corner.x, bottom_corner.y, top_corner.z), number_of_steps); casting_rays[6] = new ray(top_corner, new Vector3(top_corner.x, bottom_corner.y, bottom_corner.z), number_of_steps); casting_rays[7] = new ray(top_corner, new Vector3(bottom_corner.x, bottom_corner.y, top_corner.z), number_of_steps); casting_rays[8] = new ray(top_corner, new Vector3(bottom_corner.x, top_corner.y, bottom_corner.z), number_of_steps); casting_rays[9] = new ray(bottom_corner, new Vector3(bottom_corner.x, top_corner.y, top_corner.z), number_of_steps); casting_rays[10] = new ray(bottom_corner, new Vector3(top_corner.x, top_corner.y, bottom_corner.z), number_of_steps); casting_rays[11] = new ray(bottom_corner, new Vector3(top_corner.x, bottom_corner.y, top_corner.z), number_of_steps); upper_collision = false; lower_collision = false; for (int s = 0; s < number_of_steps; s++) { for (int i = 0; i < 12; i++) { if (i < 3) { upper_collision = upper_collision || casting_rays[i].cast(subject); } else { lower_collision = lower_collision || casting_rays[i].cast(subject); } } } } }
public ray(ray r) { origin = r.Origin; direction = r.Direction; }
void UpdateBatch(QueryBatch queryBatch) { Profiler.BeginSample("UpdateBatch"); var queryCount = queryBatch.queryIds.Count; Profiler.BeginSample("Get hitcollision entities"); var hitCollEntityArray = m_colliderGroup.GetEntityArraySt(); var hitCollDataArray = m_colliderGroup.GetComponentDataArraySt <HitCollisionData>(); var hitColliders = new NativeList <Entity>(hitCollEntityArray.Length, Allocator.TempJob); var hitColliderData = new NativeList <HitCollisionData>(hitCollEntityArray.Length, Allocator.TempJob); var hitColliderFlags = new NativeList <uint>(hitCollEntityArray.Length, Allocator.TempJob); for (int i = 0; i < hitCollEntityArray.Length; i++) { var hitCollisionOwner = EntityManager.GetComponentData <HitCollisionOwnerData>(hitCollDataArray[i].hitCollisionOwner); if (hitCollisionOwner.collisionEnabled == 0) { continue; } queryBatch.boundsHistory.Add(EntityManager.GetBuffer <HitCollisionData.BoundsHistory>(hitCollEntityArray[i])); hitColliderData.Add(hitCollDataArray[i]); hitColliders.Add(hitCollEntityArray[i]); hitColliderFlags.Add(hitCollisionOwner.colliderFlags); } Profiler.EndSample(); // Environment test Profiler.BeginSample("Environment test"); var envTestCommands = new NativeArray <RaycastCommand>(queryCount, Allocator.TempJob); var envTestResults = new NativeArray <RaycastHit>(queryCount, Allocator.TempJob); for (int nQuery = 0; nQuery < queryCount; nQuery++) { var queryId = queryBatch.queryIds[nQuery]; var queryData = m_queries[queryId]; // Start environment test var query = queryData.query; envTestCommands[nQuery] = new RaycastCommand(query.origin, query.direction, query.distance, m_environmentMask); } var envTestHandle = RaycastCommand.ScheduleBatch(envTestCommands, envTestResults, 10); envTestHandle.Complete(); Profiler.EndSample(); Profiler.BeginSample("Handle environment test"); for (int nQuery = 0; nQuery < queryCount; nQuery++) { var queryId = queryBatch.queryIds[nQuery]; var queryData = m_queries[queryId]; var result = envTestResults[nQuery]; var impact = result.collider != null; // query distance is adjusted so followup tests only are done before environment hit point if (impact) { queryData.query.distance = result.distance; // Set environment as default hit. Will be overwritten if HitCollision is hit queryData.result.hit = 1; queryData.result.hitPoint = result.point; queryData.result.hitNormal = result.normal; if (result.collider.gameObject.layer == m_hitCollisionLayer) { var hitCollision = result.collider.GetComponent <HitCollision>(); if (hitCollision != null) { queryData.result.hitCollisionOwner = hitCollision.owner; } } } } Profiler.EndSample(); // Start broadphase tests Profiler.BeginSample("Broadphase test"); var broadphaseHandels = new NativeArray <JobHandle>(queryCount, Allocator.Temp); for (int nQuery = 0; nQuery < queryCount; nQuery++) { var queryId = queryBatch.queryIds[nQuery]; var queryData = m_queries[queryId]; var query = queryData.query; queryData.broadPhaseBounds = new NativeArray <sphere>(hitColliderData.Length, Allocator.TempJob); for (int i = 0; i < hitColliderData.Length; i++) { // Get bounds for tick var histIndex = hitColliderData[i].GetHistoryIndex(query.hitCollisionTestTick); var boundSphere = primlib.sphere(queryBatch.boundsHistory[i][histIndex].pos, hitColliderData[i].boundsRadius); queryData.broadPhaseBounds[i] = boundSphere; } queryData.broadTestJob = new BroadPhaseSphereCastJob(hitColliders, hitColliderData, hitColliderFlags, queryData.broadPhaseBounds, query.ExcludeOwner, Entity.Null, query.mask, new ray(query.origin, query.direction), query.distance, query.radius); broadphaseHandels[nQuery] = queryData.broadTestJob.Schedule(); } var broadphaseHandle = JobHandle.CombineDependencies(broadphaseHandels); broadphaseHandels.Dispose(); broadphaseHandle.Complete(); Profiler.EndSample(); // Start narrow tests Profiler.BeginSample("Narrow test"); // TODO (mogensh) find out how to combine jobs without "write to same native" issue //var handles = new NativeArray<JobHandle>(queryCount, Allocator.TempJob); for (int nQuery = 0; nQuery < queryCount; nQuery++) { var queryId = queryBatch.queryIds[nQuery]; var queryData = m_queries[queryId]; var query = queryData.query; var broadPhaseResult = queryData.broadTestJob.result; // Start narrow tests queryData.narrowTestJobs.Clear(); //var narrowTestHandles = new NativeArray<JobHandle>(broadPhaseResult.Length, Allocator.Temp); for (var i = 0; i < broadPhaseResult.Length; i++) { var entity = broadPhaseResult[i]; var ray = new ray(query.origin, query.direction); queryData.narrowTestJobs.Add(new SphereCastSingleJob(EntityManager, entity, ray, query.distance, query.radius, query.hitCollisionTestTick)); // narrowTestHandles[i] = queryData.narrowTestJobs[i].Schedule(); var handle = queryData.narrowTestJobs[i].Schedule(); handle.Complete(); } //handles[nQuery] = JobHandle.CombineDependencies(narrowTestHandles); // narrowTestHandles.Dispose(); } // var handle = JobHandle.CombineDependencies(handles); // handles.Dispose(); // handle.Complete(); Profiler.EndSample(); // Find closest Profiler.BeginSample("Find closest"); for (int nQuery = 0; nQuery < queryBatch.queryIds.Count; nQuery++) { var queryId = queryBatch.queryIds[nQuery]; var queryData = m_queries[queryId]; var query = queryData.query; var closestIndex = -1; var closestDist = float.MaxValue; for (int i = 0; i < queryData.narrowTestJobs.Count; i++) { var result = queryData.narrowTestJobs[i].result[0]; var hit = result.hit == 1; if (hit) { var dist = math.distance(query.origin, result.primCenter); if (dist < closestDist) { closestDist = dist; closestIndex = i; } } } if (closestIndex != -1) { var result = queryData.narrowTestJobs[closestIndex].result[0]; queryData.result.hit = 1; queryData.result.hitPoint = result.primCenter; queryData.result.hitNormal = -queryData.query.direction; // TODO (mogensh) find correct normal var hitCollisionData = EntityManager.GetComponentData <HitCollisionData>( queryData.narrowTestJobs[closestIndex].hitCollObject); queryData.result.hitCollisionOwner = hitCollisionData.hitCollisionOwner; } // TODO (mogensh) keep native arrays for next query queryData.broadTestJob.Dispose(); for (int i = 0; i < queryData.narrowTestJobs.Count; i++) { queryData.narrowTestJobs[i].Dispose(); } } Profiler.EndSample(); for (int nQuery = 0; nQuery < queryBatch.queryIds.Count; nQuery++) { var queryId = queryBatch.queryIds[nQuery]; var queryData = m_queries[queryId]; queryData.broadPhaseBounds.Dispose(); } envTestCommands.Dispose(); envTestResults.Dispose(); hitColliders.Dispose(); hitColliderData.Dispose(); hitColliderFlags.Dispose(); Profiler.EndSample(); }
var(ray, throughput) = Camera.CameraRay(in sample, sampler);