public void InsertObject(NetGameObject target) { var x = (short)Math.Round(target.GetLocation().x); var y = (short)Math.Round(target.GetLocation().y); var z = (short)Math.Round(target.GetLocation().z); var dest_tile = GetTile(x, y, z); if (dest_tile == null) { dest_tile = CreateTile(x, y, z); } dest_tile.add(target); }
public void RemoveObject(NetGameObject target) { var x = (short)Math.Round(target.GetLocation().x); var y = (short)Math.Round(target.GetLocation().y); var z = (short)Math.Round(target.GetLocation().z); var src_tile = GetTile(x, y, z); if (src_tile != null) { src_tile.del(target); } else { LogHelper.LogWarning(string.Format("cannot find object {0}", target.NetworkId)); } }
public static bool IsSameFloorWithHeight(float current_floor, NetGameObject other) { var target_floor = other.GetLocation().y; float heightMin = target_floor - other.heightHalf; float heightMax = target_floor + other.heightHalf; #if UNITY_EDITOR || DEBUG LogHelper.LogInfo($"curr {current_floor} heightMin {heightMin} heightMax {heightMax} target {target_floor}"); #endif return(current_floor <= heightMax && current_floor >= heightMin); }
public static bool IsSameFloor(float current_floor, NetGameObject other) { var target_floor = other.GetLocation().y; // 비교 대상이 플레이어이면 층 보정 x // 이전에 구해놓은 층으로 비교 if (other.GetAsActor() != null) { target_floor = other.floor; } return(current_floor == target_floor); }
private bool Collision(float sourceRadius, Vector3 sourceLocation, NetGameObject target) { if (target.GetNetworkId() != this.GetNetworkId() && !target.DoesWantToDie()) { // 같은 층이 아닌 경우는 충돌 검사에서 제외 if (target.DetectCollision(sourceRadius, sourceLocation) && IsSameFloor(target)) { //first, tell the other guy there was a collision with a cat, so it can do something... if (target.HandleCollisionWithActor(this)) { #if _USE_INPUT_SYNC //simple collision test for spheres- are the radii summed less than the distance? Vector3 targetLocation = target.GetLocation(); float targetRadius = target.GetCollisionRadius(); Vector3 delta = targetLocation - sourceLocation; float distSq = delta.sqrMagnitude; float collisionDist = (sourceRadius + targetRadius); //if (distSq < (collisionDist * collisionDist)) collisionDist = 1.02f; #if UNITY_EDITOR || DEBUG LogHelper.LogInfo($"collision source{sourceLocation}, target{targetLocation}, delta{delta}, dist{collisionDist}, mapID{target.GetMapId()}, objectCount{World.Instance(WorldId).GetGameObjects().Count+ World.mStaticGameObjects.Count}"); LogHelper.LogDrawRay(sourceLocation, delta, new Vector3(1, 0, 1), 1); Vector3 startPos = target.GetLocation() + Vector3.up * (target.rh * 0.5f) + Vector3.left * 0.5f + Vector3.back * (0.5f + target.rh - 1); LogHelper.LogDrawLine(startPos, startPos + Vector3.right * (target.rw), new Vector3(1, 1, 1), 1); LogHelper.LogDrawLine(startPos, startPos + Vector3.forward * (target.rh), new Vector3(1, 1, 1), 1); startPos = target.GetLocation() + Vector3.up * (target.rh * 0.5f) + Vector3.right * (0.5f + target.rw - 1) + Vector3.forward * 0.5f; LogHelper.LogDrawLine(startPos, startPos + Vector3.left * (target.rw), new Vector3(1, 1, 1), 1); LogHelper.LogDrawLine(startPos, startPos + Vector3.back * (target.rh), new Vector3(1, 1, 1), 1); #endif #if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_IOS || UNITY_ANDROID if (distSq < ((targetLocation - lastLocation).sqrMagnitude - 0.01f)) { LogHelper.LogInfo($"cur {distSq}, last {(targetLocation - lastLocation).sqrMagnitude}"); SolveCollision(sourceRadius, sourceLocation, target.GetLocation().x, target.GetLocation().z, target.rw, target.rh); } return(true); #endif //okay, you hit something! //so, project your location far enough that you're not colliding Vector3 dirToTarget = delta; dirToTarget.Normalize(); Vector3 acceptableDeltaFromSourceToTarget = dirToTarget * collisionDist; //important note- we only move this cat. the other cat can take care of moving itself SetLocation(targetLocation - acceptableDeltaFromSourceToTarget); // Collision 크기가 1보다 큰 경우 처리 if (target.rw > 1 || target.rh > 1) { Vector3 movePosRW = Vector3.zero; Vector3 movePosRH = Vector3.zero; if (target.rw > 1 && sourceLocation.x >= targetLocation.x + 0.5f) { movePosRW = Vector3.right; } if (target.rh > 1 && sourceLocation.z >= targetLocation.z + 0.5f) { movePosRH = Vector3.forward; } Vector3 deltaRect = (targetLocation + movePosRW + movePosRH) - sourceLocation; Vector3 dirToTargetRect = deltaRect; dirToTargetRect.Normalize(); Vector3 acceptableDeltaFromSourceToTargetRect = dirToTargetRect * collisionDist; SetLocation((targetLocation + movePosRW + movePosRH) - acceptableDeltaFromSourceToTargetRect); #if UNITY_EDITOR || DEBUG LogHelper.LogDrawRay(sourceLocation, deltaRect, new Vector3(0, 1, 1), 1); startPos = target.GetLocation() + Vector3.up * (target.rh * 0.5f) + Vector3.left * 0.5f + Vector3.back * (0.5f + target.rh - 1); LogHelper.LogDrawLine(startPos, startPos + Vector3.right * (target.rw), new Vector3(1, 1, 1), 1); LogHelper.LogDrawLine(startPos, startPos + Vector3.forward * (target.rh), new Vector3(1, 1, 1), 1); startPos = target.GetLocation() + Vector3.up * (target.rh * 0.5f) + Vector3.right * (0.5f + target.rw - 1) + Vector3.forward * 0.5f; LogHelper.LogDrawLine(startPos, startPos + Vector3.left * (target.rw), new Vector3(1, 1, 1), 1); LogHelper.LogDrawLine(startPos, startPos + Vector3.back * (target.rh), new Vector3(1, 1, 1), 1); #endif } Vector3 relVel = mVelocity; //if other object is a cat, it might have velocity, so there might be relative velocity... Actor targetActor = target.GetAsActor(); if (targetActor != null) { relVel -= targetActor.mVelocity; } //got vel with dir between objects to figure out if they're moving towards each other //and if so, the magnitude of the impulse ( since they're both just balls ) float relVelDotDir = Vector3.Dot(relVel, dirToTarget); if (relVelDotDir > 0.0f) { Vector3 impulse = relVelDotDir * dirToTarget; if (targetActor != null) { mVelocity -= impulse; mVelocity *= mActorRestitution; } else { mVelocity -= impulse * 2.0f; mVelocity *= mWallRestitution; } } return(true); #else // Solve collision disable // LogHelper.LogInfo($"collision source{sourceLocation}, objectCount{World.Instance(WorldId).GetGameObjects().Count + World.mStaticGameObjects.Count}"); return(true); #endif } } else { target.HandleExitCollisionWithActor(this); } } return(false); }