/// <summary> /// SV_HullPointContents /// </summary> private Int32 HullPointContents(BspHull hull, Int32 num, ref Vector3 p) { while (num >= 0) { if (num < hull.firstclipnode || num > hull.lastclipnode) { Utilities.Error("SV_HullPointContents: bad node number"); } var node_children = hull.clipnodes[num].children; var plane = hull.planes[hull.clipnodes[num].planenum]; Single d; if (plane.type < 3) { d = MathLib.Comp(ref p, plane.type) - plane.dist; } else { d = Vector3.Dot(plane.normal, p) - plane.dist; } if (d < 0) { num = node_children[1]; } else { num = node_children[0]; } } return(num); }
/// <summary> /// SV_HullForEntity /// Returns a hull that can be used for testing or clipping an object of mins/maxs size. /// Offset is filled in to contain the adjustment that must be added to the /// testing object's origin to get a point to use with the returned hull. /// </summary> private BspHull HullForEntity(MemoryEdict ent, ref Vector3 mins, ref Vector3 maxs, out Vector3 offset) { BspHull hull = null; // decide which clipping hull to use, based on the size if (ent.v.solid == Solids.SOLID_BSP) { // explicit hulls in the BSP model if (ent.v.movetype != Movetypes.MOVETYPE_PUSH) { Utilities.Error("SOLID_BSP without MOVETYPE_PUSH"); } var model = ( BrushModelData )sv.models[( Int32 )ent.v.modelindex]; if (model == null || model.Type != ModelType.Brush) { Utilities.Error("MOVETYPE_PUSH with a non bsp model"); } var size = maxs - mins; if (size.X < 3) { hull = model.Hulls[0]; } else if (size.X <= 32) { hull = model.Hulls[1]; } else { hull = model.Hulls[2]; } // calculate an offset value to center the origin offset = hull.clip_mins - mins; offset += Utilities.ToVector(ref ent.v.origin); } else { // create a temp hull from bounding box sizes var hullmins = Utilities.ToVector(ref ent.v.mins) - maxs; var hullmaxs = Utilities.ToVector(ref ent.v.maxs) - mins; hull = HullForBox(ref hullmins, ref hullmaxs); offset = Utilities.ToVector(ref ent.v.origin); } return(hull); }
/// <summary> /// SV_RecursiveHullCheck /// </summary> public Boolean RecursiveHullCheck(BspHull hull, Int32 num, Single p1f, Single p2f, ref Vector3 p1, ref Vector3 p2, Trace_t trace) { // check for empty if (num < 0) { if (num != ( Int32 )Q1Contents.Solid) { trace.allsolid = false; if (num == ( Int32 )Q1Contents.Empty) { trace.inopen = true; } else { trace.inwater = true; } } else { trace.startsolid = true; } return(true); // empty } if (num < hull.firstclipnode || num > hull.lastclipnode) { Utilities.Error("SV_RecursiveHullCheck: bad node number"); } // // find the point distances // var node_children = hull.clipnodes[num].children; var plane = hull.planes[hull.clipnodes[num].planenum]; Single t1, t2; if (plane.type < 3) { t1 = MathLib.Comp(ref p1, plane.type) - plane.dist; t2 = MathLib.Comp(ref p2, plane.type) - plane.dist; } else { t1 = Vector3.Dot(plane.normal, p1) - plane.dist; t2 = Vector3.Dot(plane.normal, p2) - plane.dist; } if (t1 >= 0 && t2 >= 0) { return(RecursiveHullCheck(hull, node_children[0], p1f, p2f, ref p1, ref p2, trace)); } if (t1 < 0 && t2 < 0) { return(RecursiveHullCheck(hull, node_children[1], p1f, p2f, ref p1, ref p2, trace)); } // put the crosspoint DIST_EPSILON pixels on the near side Single frac; if (t1 < 0) { frac = (t1 + DIST_EPSILON) / (t1 - t2); } else { frac = (t1 - DIST_EPSILON) / (t1 - t2); } if (frac < 0) { frac = 0; } if (frac > 1) { frac = 1; } var midf = p1f + (p2f - p1f) * frac; var mid = p1 + (p2 - p1) * frac; var side = (t1 < 0) ? 1 : 0; // move up to the node if (!RecursiveHullCheck(hull, node_children[side], p1f, midf, ref p1, ref mid, trace)) { return(false); } if (HullPointContents(hull, node_children[side ^ 1], ref mid) != ( Int32 )Q1Contents.Solid) { // go past the node return(RecursiveHullCheck(hull, node_children[side ^ 1], midf, p2f, ref mid, ref p2, trace)); } if (trace.allsolid) { return(false); // never got out of the solid area } //================== // the other side of the node is solid, this is the impact point //================== if (side == 0) { trace.plane.normal = plane.normal; trace.plane.dist = plane.dist; } else { trace.plane.normal = -plane.normal; trace.plane.dist = -plane.dist; } while (HullPointContents(hull, hull.firstclipnode, ref mid) == ( Int32 )Q1Contents.Solid) { // shouldn't really happen, but does occasionally frac -= 0.1f; if (frac < 0) { trace.fraction = midf; trace.endpos = mid; Host.Console.DPrint("backup past 0\n"); return(false); } midf = p1f + (p2f - p1f) * frac; mid = p1 + (p2 - p1) * frac; } trace.fraction = midf; trace.endpos = mid; return(false); }