public bool IsObjectInArea(ISpaceObject obj, Shape area) { if (null == obj || null == area) { return(false); } Shape shape = obj.GetCollideShape(); if (null == shape) { LogSystem.Error("IsObjectInArea:obj({0}[{1}])'s shape is null", obj.GetID(), obj.GetObjType()); return(false); } return(collide_detector_.Intersect(shape, area)); }
public bool CheckCollide(ISpaceObject hiter, ISpaceObject dest) { Shape srcShape = hiter.GetCollideShape(); Shape destShape = dest.GetCollideShape(); if (null == srcShape) { LogSystem.Error("CheckCollide:hiter obj({0}[{1}])'s shape is null", hiter.GetID(), hiter.GetObjType()); return(false); } if (null == destShape) { LogSystem.Error("CheckCollide:dest obj({0}[{1}])'s shape is null", dest.GetID(), dest.GetObjType()); return(false); } bool is_collide = collide_detector_.Intersect(srcShape, destShape); bool is_allready_collide = IsAllreadyCollide(hiter, dest); if (is_collide) // 碰撞 { if (!is_allready_collide) // 之前没有碰撞,记录下相撞的物体 //DLog._("spatialsystem", "{4} spatial collide obj({0},{1}) with obj({2},{3})", // hiter.GetID(), hiter.GetObjType(), dest.GetID(), // dest.GetObjType(), current_spatial_id_); { hiter.OnCollideObject(dest); dest.OnCollideObject(hiter); } } else if (is_allready_collide) // 当前没有碰撞,但是之前碰撞,即分离的情况 //DLog._("spatialsystem", "{4} spatial depart obj({0},{1}) with obj({2},{3})", // hiter.GetID(), hiter.GetObjType(), // dest.GetID(), dest.GetObjType(), current_spatial_id_); { hiter.OnDepartObject(dest); dest.OnDepartObject(hiter); } return(is_collide); }
private List <ISpaceObject> GetObjectCrossByLine(Vector3 startpos, Vector3 endpos, bool includeCanShoot, bool includeCanLeap, bool includeCantLeap, bool includeLevel, MyFunc <float, ISpaceObject, bool> pred) { List <ISpaceObject> crossed_objects = new List <ISpaceObject>(); Line line = new Line(startpos, endpos); line.IdenticalTransform(); // 考虑阻挡对结果的影响 float distance_with_block = -1; bool considerblock = (includeCanShoot || includeCanLeap || includeCantLeap); if (considerblock) { distance_with_block = (float)GetFirstBlockCellDistance(startpos, endpos, includeCanShoot, includeCanLeap, includeCantLeap, includeLevel); if (distance_with_block < 0) { considerblock = false; } } m_KdTree.Query(line.GetCenter(), (float)line.GetRadius(), (float distSqr, KdTreeObject kdObj) => { ISpaceObject obj = kdObj.SpaceObject; if (null != obj) { Shape shape = obj.GetCollideShape(); if (null != shape) { if (collide_detector_.IsLineCrossShape(line, obj.GetCollideShape()) && pred(distSqr, obj)) { if (considerblock) { if (GetDistanceWithPos(obj, startpos) < distance_with_block) { crossed_objects.Add(obj); } } else { crossed_objects.Add(obj); } } } else { LogSystem.Error("GetObjectCrossByLine:obj({0}[{1}])'s shape is null", obj.GetID(), obj.GetObjType()); } } }); crossed_objects.Sort((obj1, obj2) => { float d1 = GetDistanceWithPos(obj1, startpos); float d2 = GetDistanceWithPos(obj2, startpos); if (d1 > d2) { return(1); } else if (Math.Abs(d1 - d2) < 0.0001) { return(0); } else { return(-1); } }); return(crossed_objects); }