A ray in 2D space.
static public int constructor(IntPtr l) { try { #if DEBUG var method = System.Reflection.MethodBase.GetCurrentMethod(); string methodName = GetMethodName(method); #if UNITY_5_5_OR_NEWER UnityEngine.Profiling.Profiler.BeginSample(methodName); #else Profiler.BeginSample(methodName); #endif #endif UnityEngine.Ray2D o; UnityEngine.Vector2 a1; checkType(l, 2, out a1); UnityEngine.Vector2 a2; checkType(l, 3, out a2); o = new UnityEngine.Ray2D(a1, a2); pushValue(l, true); pushValue(l, o); return(2); } catch (Exception e) { return(error(l, e)); } #if DEBUG finally { #if UNITY_5_5_OR_NEWER UnityEngine.Profiling.Profiler.EndSample(); #else Profiler.EndSample(); #endif } #endif }
private Ray2D[] GetPuntRays() { BoxCollider2D box = GetComponent<BoxCollider2D>(); Ray2D[] rays = new Ray2D[6]; Vector2 origin = new Vector2(); // shoot rays to the right, on top origin.x = transform.position.x + box.center.x - box.size.x/2; origin.y = transform.position.y + box.center.y + box.size.y/2; rays[0] = new Ray2D(origin, Vector2.right); // middle origin.y = transform.position.y + box.center.y; rays[1] = new Ray2D(origin, Vector2.right); // bottom origin.y = transform.position.y + box.center.y - box.size.y / 2; rays[2] = new Ray2D(origin, Vector2.right); // also shoot rays on the left, starting with top origin.x = transform.position.x - box.center.x + box.size.x / 2; origin.y = transform.position.y + box.center.y + box.size.y/2; rays[3] = new Ray2D(origin, -Vector2.right); // middle origin.y = transform.position.y + box.center.y; rays[4] = new Ray2D(origin, -Vector2.right); // bottom origin.y = transform.position.y + box.center.y - box.size.y / 2; rays[5] = new Ray2D(origin, -Vector2.right); return rays; }
static public int constructor(IntPtr l) { try { int argc = LuaDLL.lua_gettop(l); UnityEngine.Ray2D o; if (argc == 3) { UnityEngine.Vector2 a1; checkType(l, 2, out a1); UnityEngine.Vector2 a2; checkType(l, 3, out a2); o = new UnityEngine.Ray2D(a1, a2); pushValue(l, true); pushValue(l, o); return(2); } else if (argc <= 2) { o = new UnityEngine.Ray2D(); pushValue(l, true); pushValue(l, o); return(2); } return(error(l, "New object failed.")); } catch (Exception e) { return(error(l, e)); } }
static public int set_direction(IntPtr l) { UnityEngine.Ray2D o = (UnityEngine.Ray2D)checkSelf(l); UnityEngine.Vector2 v; checkType(l, 2, out v); o.direction = v; setBack(l, o); return(0); }
static public int constructor(IntPtr l) { UnityEngine.Ray2D o; UnityEngine.Vector2 a1; checkType(l, 2, out a1); UnityEngine.Vector2 a2; checkType(l, 3, out a2); o = new UnityEngine.Ray2D(a1, a2); pushObject(l, o); return(1); }
static bool Ray2D_ToString(JSVCall vc, int argc) { int len = argc; if (len == 0) { UnityEngine.Ray2D argThis = (UnityEngine.Ray2D)vc.csObj; JSApi.setStringS((int)JSApi.SetType.Rval, argThis.ToString()); JSMgr.changeJSObj(vc.jsObjID, argThis); } return(true); }
// methods static bool Ray2D_GetPoint__Single(JSVCall vc, int argc) { int len = argc; if (len == 1) { System.Single arg0 = (System.Single)JSApi.getSingle((int)JSApi.GetType.Arg); UnityEngine.Ray2D argThis = (UnityEngine.Ray2D)vc.csObj; JSApi.setVector2S((int)JSApi.SetType.Rval, argThis.GetPoint(arg0)); JSMgr.changeJSObj(vc.jsObjID, argThis); } return(true); }
static bool Ray2D_ToString__String(JSVCall vc, int argc) { int len = argc; if (len == 1) { System.String arg0 = (System.String)JSApi.getStringS((int)JSApi.GetType.Arg); UnityEngine.Ray2D argThis = (UnityEngine.Ray2D)vc.csObj; JSApi.setStringS((int)JSApi.SetType.Rval, argThis.ToString(arg0)); JSMgr.changeJSObj(vc.jsObjID, argThis); } return(true); }
static public int GetPoint(IntPtr l) { try{ UnityEngine.Ray2D self = (UnityEngine.Ray2D)checkSelf(l); System.Single a1; checkType(l, 2, out a1); UnityEngine.Vector2 ret = self.GetPoint(a1); pushValue(l, ret); return(1); } catch (Exception e) { LuaDLL.luaL_error(l, e.ToString()); return(0); } }
static public int constructor(IntPtr l) { try { UnityEngine.Ray2D o; UnityEngine.Vector2 a1; checkType(l, 2, out a1); UnityEngine.Vector2 a2; checkType(l, 3, out a2); o = new UnityEngine.Ray2D(a1, a2); pushValue(l, o); return(1); } catch (Exception e) { return(error(l, e)); } }
static public int constructor(IntPtr l) { try { UnityEngine.Ray2D o; UnityEngine.Vector2 a1; checkType(l,2,out a1); UnityEngine.Vector2 a2; checkType(l,3,out a2); o=new UnityEngine.Ray2D(a1,a2); pushValue(l,true); pushValue(l,o); return 2; } catch(Exception e) { return error(l,e); } }
static void Ray2D_direction(JSVCall vc) { if (vc.bGet) { UnityEngine.Ray2D _this = (UnityEngine.Ray2D)vc.csObj; var result = _this.direction; JSApi.setVector2S((int)JSApi.SetType.Rval, result); } else { UnityEngine.Vector2 arg0 = (UnityEngine.Vector2)JSApi.getVector2S((int)JSApi.GetType.Arg); UnityEngine.Ray2D _this = (UnityEngine.Ray2D)vc.csObj; _this.direction = arg0; JSMgr.changeJSObj(vc.jsObjID, _this); } }
public static int constructor(IntPtr l) { try { UnityEngine.Ray2D o; UnityEngine.Vector2 a1; checkType(l,2,out a1); UnityEngine.Vector2 a2; checkType(l,3,out a2); o=new UnityEngine.Ray2D(a1,a2); pushValue(l,o); return 1; } catch(Exception e) { LuaDLL.luaL_error(l, e.ToString()); return 0; } }
/// <summary> /// the gun shoots a bullet when it's ready /// </summary> /// <returns>true when a shot is fired, false when it is not</returns> public bool Shoot(Vector3 playerPosition, float angle) { var direction = MathHelpers.GetForwardVector(angle, 1); Vector2 gunfireOrigin = playerPosition + (direction); Vector2 pangLocation = playerPosition + (direction / 2); if (!Ready()) { return false; } var sound = GetComponent<AudioSource>(); sound.Play(); _lastShot = Stopwatch.StartNew(); var pang = Instantiate(Pang, pangLocation, Quaternion.identity); Destroy(pang, 0.05f); var ray = new Ray2D(gunfireOrigin, direction); const float shotDistance = 20; var hit = Physics2D.Raycast(ray.origin, ray.direction, shotDistance); if (hit.collider == null) return true; //nothing hit, so return //we hit something, draw explosion @ impact location var vector2 = ray.origin + ray.direction * hit.distance; var explosion = Instantiate(Explosion, vector2, Quaternion.identity); Destroy(explosion, 0.03f); if (hit.collider.attachedRigidbody != null) { HitSomething(hit.collider.attachedRigidbody.gameObject); return true; } if (hit.collider.gameObject != null) { HitSomething(hit.collider.gameObject); } return true; }
static public int constructor(IntPtr l) { LuaDLL.lua_remove(l, 1); UnityEngine.Ray2D o; if (matchType(l, 1, typeof(UnityEngine.Vector2), typeof(UnityEngine.Vector2))) { UnityEngine.Vector2 a1; checkType(l, 1, out a1); UnityEngine.Vector2 a2; checkType(l, 2, out a2); o = new UnityEngine.Ray2D(a1, a2); pushObject(l, o); return(1); } LuaDLL.luaL_error(l, "New object failed."); return(0); }
/// <summary> /// Read the data using the reader. /// </summary> /// <param name="reader">Reader.</param> public override object Read(ISaveGameReader reader) { UnityEngine.Ray2D ray2D = new UnityEngine.Ray2D(); foreach (string property in reader.Properties) { switch (property) { case "origin": ray2D.origin = reader.ReadProperty <UnityEngine.Vector2> (); break; case "direction": ray2D.direction = reader.ReadProperty <UnityEngine.Vector2> (); break; } } return(ray2D); }
// Update is called once per frame void Update() { Vector3 vPlayerPos = m_player.transform.position; vPlayerPos.z = transform.position.z; Ray2D sightRay = new Ray2D(transform.position, vPlayerPos - transform.position); float distToTarget = Vector2.Distance(vPlayerPos, transform.position); float fSightBlockedDist = IsSightBlockedByBlockedTiles? RpgMapHelper.Raycast(sightRay, distToTarget) : -1f; // NOTE: fSightBlockedDist will be -1f if sight line is not blocked by blocked collision tile bool isPlayerSeen = distToTarget < SightDistance && fSightBlockedDist == -1f; if (isPlayerSeen) { m_pathFindingBehaviour.TargetPos = vPlayerPos; } bool isTargetReached = Vector2.Distance(m_pathFindingBehaviour.TargetPos, transform.position) <= MinDistToReachTarget; if (!isPlayerSeen && isTargetReached) { // Move around m_pathFindingBehaviour.enabled = false; vPlayerPos = transform.position; m_fAngOff += Random.Range(-AngRandOff, AngRandOff); Vector3 vOffset = Quaternion.AngleAxis(m_fAngOff, Vector3.forward) * (AngRandRadious * Vector3.right); vPlayerPos += vOffset; m_moving.Arrive(vPlayerPos); } else // Follow the player { // stop following the path when closed enough to target m_pathFindingBehaviour.enabled = !isTargetReached; if (!m_pathFindingBehaviour.enabled) { m_fAngOff += Random.Range(-AngRandOff, AngRandOff); Vector3 vOffset = Quaternion.AngleAxis(m_fAngOff, Vector3.forward) * (AngRandRadious * Vector3.right); vPlayerPos += vOffset; Debug.DrawLine(transform.position, m_player.transform.position, Color.blue); Debug.DrawRay(m_player.transform.position, vOffset, Color.blue); m_moving.Arrive(vPlayerPos); } } //+++avoid obstacles Vector3 vTurnVel = Vector3.zero; if ( 0 != (m_phyChar.CollFlags & PhysicCharBehaviour.eCollFlags.RIGHT)) { vTurnVel.x = -m_moving.MaxSpeed; } else if ( 0 != (m_phyChar.CollFlags & PhysicCharBehaviour.eCollFlags.LEFT)) { vTurnVel.x = m_moving.MaxSpeed; } if ( 0 != (m_phyChar.CollFlags & PhysicCharBehaviour.eCollFlags.DOWN)) { vTurnVel.y = m_moving.MaxSpeed; } else if ( 0 != (m_phyChar.CollFlags & PhysicCharBehaviour.eCollFlags.UP)) { vTurnVel.y = -m_moving.MaxSpeed; } if( vTurnVel != Vector3.zero ) { m_moving.ApplyForce(vTurnVel - m_moving.Veloc); } //--- //fix to avoid flickering of the creature when collides with wall if (Time.frameCount % 16 == 0) //--- { if (!LockAnimDir) UpdateAnimDir(); } }
static public int get_direction(IntPtr l) { UnityEngine.Ray2D o = (UnityEngine.Ray2D)checkSelf(l); pushValue(l, o.direction); return(1); }
/// <summary> /// Use a Ray2D to check if there is a collision with the map trough the ray. If there is a collision, return value will be >= 0f. /// </summary> /// <param name="ray">Ray2D used for raycasting</param> /// <param name="distance">Distance to be checked</param> /// <param name="precission">Distance increment from ray origin to check with map for collisions. Leave it <= 0 for default value.</param> /// <returns></returns> public static float Raycast( Ray2D ray, float distance, float precission = 0f ) { if( precission <= 0 ) { precission = AutoTileMap.Instance.CellSize.x / 2f; } Vector2 vInc = ray.direction.normalized * precission; Vector2 curPos = ray.origin; for (float d = 0; d <= distance; d += precission ) { eTileCollisionType collType = AutoTileMap.Instance.GetAutotileCollisionAtPosition(curPos); Debug.DrawLine(curPos, curPos + vInc, collType == eTileCollisionType.BLOCK? Color.red : Color.green); if (collType == eTileCollisionType.BLOCK) { return d; } curPos += vInc; } return -1f; }
} // CollectVertices // Find intersection of RAY & SEGMENT Intersection getIntersection(Ray2D ray, Segment2D segment) { Intersection o = new Intersection(); // RAY in parametric: Point + Delta*T1 float r_px = ray.a.x; float r_py = ray.a.y; float r_dx = ray.b.x-ray.a.x; float r_dy = ray.b.y-ray.a.y; // SEGMENT in parametric: Point + Delta*T2 float s_px = segment.a.x; float s_py = segment.a.y; float s_dx = segment.b.x-segment.a.x; float s_dy = segment.b.y-segment.a.y; // Are they parallel? If so, no intersect var r_mag = Mathf.Sqrt(r_dx*r_dx+r_dy*r_dy); var s_mag = Mathf.Sqrt(s_dx*s_dx+s_dy*s_dy); if(r_dx/r_mag==s_dx/s_mag && r_dy/r_mag==s_dy/s_mag) // Unit vectors are the same { return o; } // SOLVE FOR T1 & T2 // r_px+r_dx*T1 = s_px+s_dx*T2 && r_py+r_dy*T1 = s_py+s_dy*T2 // ==> T1 = (s_px+s_dx*T2-r_px)/r_dx = (s_py+s_dy*T2-r_py)/r_dy // ==> s_px*r_dy + s_dx*T2*r_dy - r_px*r_dy = s_py*r_dx + s_dy*T2*r_dx - r_py*r_dx // ==> T2 = (r_dx*(s_py-r_py) + r_dy*(r_px-s_px))/(s_dx*r_dy - s_dy*r_dx) var T2 = (r_dx*(s_py-r_py) + r_dy*(r_px-s_px))/(s_dx*r_dy - s_dy*r_dx); var T1 = (s_px+s_dx*T2-r_px)/r_dx; // Must be within parametic whatevers for RAY/SEGMENT if(T1<0) return o; if(T2<0 || T2>1) return o; o.v = new Vector3(r_px+r_dx*T1, r_py+r_dy*T1, 0); o.angle = T1; // Return the POINT OF INTERSECTION return o; } // getIntersection
void GenCastShadows () { // DEBUGGING //stopwatch.Start(); // Get mouse position Vector3 currentPos = transform.position; // Get all angles uniqueAngles.Clear(); for(var j=0;j<uniquePoints.Count;j++) { float angle = Mathf.Atan2(uniquePoints[j].y-currentPos.y,uniquePoints[j].x-currentPos.x); uniqueAngles.Add(angle-0.00001f); uniqueAngles.Add(angle); uniqueAngles.Add(angle+0.00001f); } // Rays in all directions intersects.Clear(); for(var j=0;j<uniqueAngles.Count;j++) { float angle = uniqueAngles[j]; // Calculate dx & dy from angle float dx = Mathf.Cos(angle); float dy = Mathf.Sin(angle); // Ray from center of screen to mouse Ray2D ray = new Ray2D(new Vector2(currentPos.x,currentPos.y), new Vector2(currentPos.x+dx,currentPos.y+dy)); // Find CLOSEST intersection Intersection closestIntersect = new Intersection(); bool founded = false; for(int i=0;i<segments.Count;i++) { Intersection intersect = getIntersection(ray,segments[i]); if(intersect.v==null) continue; // if(!closestIntersect.v==null || intersect.angle<closestIntersect.angle) if(!founded || intersect.angle<closestIntersect.angle) { founded = true; closestIntersect=intersect; } } // for segments // Intersect angle if(closestIntersect==null) continue; closestIntersect.angle = angle; // Add to list of intersects intersects.Add(closestIntersect); } // for uniqueAngles // Sort intersects by angle intersects.Sort((x, y) =>{ return Comparer<float?>.Default.Compare(x.angle, y.angle); }); // Mesh generation List<Vector3> verts = new List<Vector3>(); List<int> tris = new List<int>(); verts.Clear(); tris.Clear(); // TODO: UV's // Place first vertex at mouse position ("dummy triangulation") verts.Add(transform.InverseTransformPoint(transform.position)); for(var i=0;i<intersects.Count;i++) { if (intersects[i].v!=null) { verts.Add(transform.InverseTransformPoint((Vector3)intersects[i].v)); if ( intersects[(i+1) % intersects.Count].v != null) verts.Add(transform.InverseTransformPoint((Vector3)intersects[(i+1) % intersects.Count].v)); //GLDebug.DrawLine((Vector3)intersects[i].v,(Vector3)intersects[(i+1) % intersects.Count].v,Color.red,0,false); } } // for intersects // Build triangle list for(var i=0;i<verts.Count+1;i++) { tris.Add((i+1) % verts.Count); tris.Add((i) % verts.Count); tris.Add(0); } // Create mesh lightMesh.Clear(); lightMesh.vertices = verts.ToArray(); lightMesh.triangles = tris.ToArray(); lightMesh.RecalculateNormals(); // FIXME: no need if no lights..or just assign fixed value.. Vector2[] uvs = new Vector2[lightMesh.vertices.Length]; int iteration = 0; while (iteration < uvs.Length) { float dist = Vector2.Distance (lightMesh.vertices[iteration], currentPos); uvs[iteration] = new Vector2 (0.5f, dist/uvScale); //Debug.Log (uvs[iteration]); iteration++; } lightMesh.uv = uvs; meshFilter.mesh = lightMesh; // Debug lines from mouse to intersection /* for(var i=0;i<intersects.Count;i++) { if (intersects[i].v!=null) { GLDebug.DrawLine(new Vector3(currentPos.x,currentPos.y,0),(Vector3)intersects[i].v,Color.red,0,false); } } */ // DEBUG TIMER //stopwatch.Stop(); //Debug.Log("Stopwatch: " + stopwatch.Elapsed); // Debug.Log("Stopwatch: " + stopwatch.ElapsedMilliseconds); //stats.text = ""+stopwatch.ElapsedMilliseconds+"ms"; //stopwatch.Reset(); }
protected override void raycasting()//remove raycasting since it is not needed { RaycastHit2D hit; Ray2D ray = new Ray2D(transform.position, targetDir); hit = Physics2D.Raycast(start.position,targetDir,range,1<< shieldTargetLayer); }
/// <summary> /// Write the specified value using the writer. /// </summary> /// <param name="value">Value.</param> /// <param name="writer">Writer.</param> public override void Write(object value, ISaveGameWriter writer) { UnityEngine.Ray2D ray2D = (UnityEngine.Ray2D)value; writer.WriteProperty("origin", ray2D.origin); writer.WriteProperty("direction", ray2D.direction); }