static float CollisionTest_Sphere_Plane(Vector3 ori, Vector3 dir, float step, float dt, float radius, NPlane plane) { var offset = dir * step; var s = ori - plane.point; var dis = Vector3.Dot(s, plane.normal); if (dis <= radius) { // already collided: ignore... return(-1); } var dst = ori + offset; var s2 = dst - plane.point; var dis2 = Vector3.Dot(s2, plane.normal); if (dis2 < radius) { var vel = offset / dt; var speed = Vector3.Dot(offset / dt, -plane.normal); var t = (dis - radius) / speed; if (t >= 0) { return(t); } } return(-1); }
void Start() { m_buffer = new List <CellLine>(); m_cellRoot = transform.Find("Cell"); m_aimer = transform.Find("Aimer"); m_original = transform; if (m_cellRoot == null) { m_cellRoot = new GameObject("Cell").transform; m_cellRoot.parent = transform; } m_bounds = transform.Find("Bounds"); m_bubbleGO = Resources.Load <GameObject>("Bubble"); m_bounds.gameObject.SetActive(true); m_aimer.gameObject.SetActive(true); if (m_cellInfo != null) { var src = m_cellInfo.text; var lines = src.Split('\n'); var slines = new List <String>(); var width = int.MaxValue; for (int i = 0; i < lines.Length; ++i) { var l = lines[i].Trim(); if (!String.IsNullOrEmpty(l)) { slines.Add(l); if (l.Length < width) { width = l.Length; } } } m_bubbleDataOffset = slines.Count - 1; m_bubbleLevelData = new int[slines.Count][]; for (int j = 0; j < slines.Count; ++j) { m_bubbleLevelData[j] = new int[width]; var s = slines[j]; for (int i = 0; i < width; ++i) { var c = s[i]; if (Char.IsNumber(c)) { m_bubbleLevelData[j][i] = s[i] - '0'; } } if (j == slines.Count - 1) { m_firstLineOddLeading = m_bubbleLevelData[j][0] & 1; } } var sphere = m_bubbleGO.GetComponent <SphereCollider>(); m_bubbleRadius = sphere.radius; m_lineHeight = (m_bubbleRadius * 2.0f) * Sqrt3Div2; var frameWidth = width * m_bubbleRadius + m_bubbleRadius; var frameHeight = m_bubbleRadius * 2 + (m_maxLine - 1) * m_lineHeight; if (m_bounds != null) { var frameScale = m_bounds.localScale; frameScale.z = m_bubbleRadius * 2; m_bounds.localScale = frameScale; m_planes = new NPlane[4]; var l = m_bounds.Find("Left"); var r = m_bounds.Find("Right"); var t = m_bounds.Find("Top"); var b = m_bounds.Find("Bottom"); var lpos = l.localPosition; var lscale = l.localScale; lpos.x = -BoundFrameWidth * 0.5f; lpos.y = frameHeight * 0.5f; l.localPosition = lpos; lscale.y = frameHeight + BoundFrameWidth * 2; l.localScale = lscale; m_planes[0] = new NPlane { point = new Vector3(lpos.x + BoundFrameWidth * 0.5f, lpos.y, lpos.z), normal = new Vector3(1, 0, 0) }; var rpos = r.localPosition; var rscale = l.localScale; rpos.x = frameWidth + BoundFrameWidth * 0.5f; rpos.y = frameHeight * 0.5f; r.localPosition = rpos; rscale.y = frameHeight + BoundFrameWidth * 2; r.localScale = rscale; m_planes[1] = new NPlane { point = new Vector3(rpos.x - BoundFrameWidth * 0.5f, rpos.y, rpos.z), normal = new Vector3(-1, 0, 0) }; var tpos = t.localPosition; var tscale = t.localScale; tpos.x = frameWidth * 0.5f; tpos.y = -BoundFrameWidth * 0.5f; t.localPosition = tpos; tscale.x = frameWidth; t.localScale = tscale; m_planes[2] = new NPlane { point = new Vector3(tpos.x, tpos.y + BoundFrameWidth * 0.5f, tpos.z), normal = new Vector3(0, 1, 0) }; var bpos = b.localPosition; var bscale = t.localScale; bpos.x = frameWidth * 0.5f; bpos.y = frameHeight + BoundFrameWidth * 0.5f; b.localPosition = bpos; bscale.x = frameWidth; b.localScale = bscale; m_planes[3] = new NPlane { point = new Vector3(bpos.x, bpos.y - BoundFrameWidth * 0.5f, bpos.z), normal = new Vector3(0, -1, 0) }; m_boundsWidth = frameWidth; m_boundsHeight = frameHeight; if (m_aimer != null) { m_aimer.localPosition = new Vector3( frameWidth * 0.5f, frameHeight - m_bubbleRadius - BoundFrameWidth * 0.5f, 0 ); } if (m_camera == null) { m_camera = GetComponentInChildren <Camera>(); if (m_camera != null) { var pos = m_camera.transform.localPosition; pos.x = frameWidth * 0.5f; pos.y = frameHeight * 0.5f; m_camera.transform.localPosition = pos; } } for (int i = 0; i < m_initLineCount && i < m_maxLine; ++i) { NewLine(false); } } } }