public void TryCameraCollision(BaseCamera camera) { ThirdPersonCamera thirdPersonCamera = camera as ThirdPersonCamera; if (thirdPersonCamera == null) { return; } // Start moving from camera target to it's seek position float distanceFromTargetToCamera = thirdPersonCamera.MaxDistanceFromTargetToCamera; Vector3 cameraForwardVector = thirdPersonCamera.GetEyeSpaceForwardVector(); Vector3 startPosition = thirdPersonCamera.GetTargetVector(); float safeInterval = thirdPersonCamera.CameraCollisionSphereRadius; for (float interval = safeInterval; interval <= distanceFromTargetToCamera; interval += (thirdPersonCamera.CameraCollisionSphereRadius / 10.0f)) { var intermediatePosition = startPosition - cameraForwardVector * interval; FSphere cameraCollisionSphere = new FSphere(intermediatePosition, thirdPersonCamera.CameraCollisionSphereRadius); var boundingBoxes = GetBoundingBoxesForCameraCollisionTest(ref cameraCollisionSphere, thirdPersonCamera.GetThirdPersonTarget().GetRootComponent()); if (boundingBoxes.Count > 0) { //if (IsCameraCollisionWithBoundingBoxes(ref cameraCollisionSphere, boundingBoxes)) //{ // break; //} } safeInterval = interval; } thirdPersonCamera.SetDistanceFromTargetToCamera(safeInterval); }
private bool IsCameraCollisionWithBoundingBoxes(ref FSphere cameraCollisionSphere, List <BoundBase> collidedRootBounds) { bool bSphereAndRegularBoundingBoxCollision = false; CheckRegularBoundingBoxAndCameraSphereCollision(ref bSphereAndRegularBoundingBoxCollision, ref collidedRootBounds, ref cameraCollisionSphere); return(bSphereAndRegularBoundingBoxCollision); }
public static bool IsSphereVsSphereIntersection(ref FSphere sphere1, ref FSphere sphere2) { Vector3 SphereOriginDistance = sphere1.Origin - sphere2.Origin; float SquaredDistance = Vector3.Dot(SphereOriginDistance, SphereOriginDistance); float SquaredRadiuses = (sphere1.Radius * sphere1.Radius) + (sphere2.Radius * sphere2.Radius); return(SquaredDistance <= SquaredRadiuses); }
private void UpdateBounds() { if (!StaticMesh) { return; } BoundBox = Geometry.CaculateWorldBound(StaticMesh.bounds, Matrix_LocalToWorld); BoundSphere = new FSphere(Geometry.CaculateBoundRadius(BoundBox), BoundBox.center); }
public virtual void IsLitByLightSource(List <PointLight> LightList) { m_lightVisibilityMap.Init(LightList.Count, false); for (Int32 i = 0; i < LightList.Count; i++) { BoundBase bound = GetAABBFromAllChildComponents(); FSphere boundSphere = (FSphere)bound; FSphere lightSphere = new FSphere(LightList[i].Position.Xyz, LightList[i].AttenuationRadius); m_lightVisibilityMap[i] = GeometryMath.IsSphereVsSphereIntersection(ref boundSphere, ref lightSphere); } }
private void CheckRegularBoundingBoxAndCameraSphereCollision(ref bool bSphereAndFrameBoundingBoxCollision, ref List <BoundBase> collidedRootBounds, ref FSphere cameraCollisionSphere) { foreach (var testingBound in collidedRootBounds) { FSphere obbCollisionSphere = (FSphere)testingBound; if (GeometryMath.IsSphereVsSphereIntersection(ref cameraCollisionSphere, ref obbCollisionSphere)) { bSphereAndFrameBoundingBoxCollision = true; break; } } }
private void CalculateModelBounds() { // Iterate through the shapes and calculate a bounding box which encompasses all of them. Vector3 min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue); Vector3 max = new Vector3(float.MinValue, float.MinValue, float.MinValue); foreach (var shape in SHP1Tag.Shapes) { Vector3 sMin = shape.BoundingBox.Min; Vector3 sMax = shape.BoundingBox.Max; if (sMin.X < min.X) { min.X = sMin.X; } if (sMax.X > max.X) { max.X = sMax.X; } if (sMin.Y < min.Y) { min.Y = sMin.Y; } if (sMax.Y > max.Y) { max.Y = sMax.Y; } if (sMin.Z < min.Z) { min.Z = sMin.Z; } if (sMax.Z > max.Z) { max.Z = sMax.Z; } } BoundingBox = new FAABox(min, max); BoundingSphere = new FSphere(BoundingBox.Center, BoundingBox.Max.Length); }
// CAMERA private List <BoundBase> GetBoundingBoxesForCameraCollisionTest(ref FSphere cameraCollisionSphere, Component characterRootComponent) { List <BoundBase> resultCollidedRootBounds = new List <BoundBase>(); foreach (var unit in CollisionUnits) { if (unit.RootComponent == characterRootComponent) { continue; } FSphere aabbCollisionSphere = (FSphere)(unit.GetAndTryUpdateFramingBoundingBox()); if (GeometryMath.IsSphereVsSphereIntersection(ref cameraCollisionSphere, ref aabbCollisionSphere)) { resultCollidedRootBounds.AddRange(unit.GetBoundingBoxes()); } } return(resultCollidedRootBounds); }
private void LoadTagDataFromFile(EndianBinaryReader reader, int tagCount, bool dumpTextures, bool dumpShaders) { for (int i = 0; i < tagCount; i++) { long tagStart = reader.BaseStream.Position; string tagName = reader.ReadString(4); int tagSize = reader.ReadInt32(); switch (tagName) { // INFO - Vertex Count, Scene Hierarchy case "INF1": INF1Tag = new INF1(); INF1Tag.LoadINF1FromStream(reader, tagStart); break; // VERTEX - Stores vertex arrays for pos/normal/color0/tex0 etc. // Contains VertexAttributes which describe how the data is stored/laid out. case "VTX1": VTX1Tag = new VTX1(); VTX1Tag.LoadVTX1FromStream(reader, tagStart, tagSize); break; // ENVELOPES - Defines vertex weights for skinning case "EVP1": EVP1Tag = new EVP1(); EVP1Tag.LoadEVP1FromStream(reader, tagStart); break; // DRAW (Skeletal Animation Data) - Stores which matrices (?) are weighted, and which are used directly case "DRW1": DRW1Tag = new DRW1(); DRW1Tag.LoadDRW1FromStream(reader, tagStart); break; // JOINTS - Stores the skeletal joints (position, rotation, scale, etc...) case "JNT1": JNT1Tag = new JNT1(); JNT1Tag.LoadJNT1FromStream(reader, tagStart); JNT1Tag.CalculateParentJointsForSkeleton(INF1Tag.HierarchyRoot); break; // SHAPE - Face/Triangle information for model. case "SHP1": SHP1Tag = new SHP1(); SHP1Tag.ReadSHP1FromStream(reader, tagStart, VTX1Tag.VertexData); break; // MATERIAL - Stores materials (which describes how textures, etc. are drawn) case "MAT3": MAT3Tag = new MAT3(); MAT3Tag.LoadMAT3FromStream(reader, tagStart); break; // TEXTURES - Stores binary texture images. case "TEX1": TEX1Tag = new TEX1(); TEX1Tag.LoadTEX1FromStream(reader, tagStart, dumpTextures); break; // MODEL - Seems to be bypass commands for Materials and invokes GX registers directly. case "MDL3": break; } // Skip the stream reader to the start of the next tag since it gets moved around during loading. reader.BaseStream.Position = tagStart + tagSize; } // To generate shaders we need to know which vertex attributes need to be enabled for the shader. However, // the shader has no knowledge in our book as to what attributes are enabled. Theoretically we could enable // them on the fly as something requested it, but that'd involve more code that I don't want to do right now. // To resolve, we iterate once through the hierarchy to see which mesh is called after a material and bind the // vertex descriptions. Material dummyMat = null; AssignVertexAttributesToMaterialsRecursive(INF1Tag.HierarchyRoot, ref dummyMat, MAT3Tag); // Now that the vertex attributes are assigned to the materials, generate a shader from the data. GenerateShadersForMaterials(MAT3Tag, dumpShaders); // Iterate through the shapes and calculate a bounding box which encompasses all of them. Vector3 min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue); Vector3 max = new Vector3(float.MinValue, float.MinValue, float.MinValue); foreach (var shape in SHP1Tag.Shapes) { Vector3 sMin = shape.BoundingBox.Min; Vector3 sMax = shape.BoundingBox.Max; if (sMin.X < min.X) { min.X = sMin.X; } if (sMax.X > max.X) { max.X = sMax.X; } if (sMin.Y < min.Y) { min.Y = sMin.Y; } if (sMax.Y > max.Y) { max.Y = sMax.Y; } if (sMin.Z < min.Z) { min.Z = sMin.Z; } if (sMax.Z > max.Z) { max.Z = sMax.Z; } } BoundingBox = new FAABox(min, max); BoundingSphere = new FSphere(BoundingBox.Center, BoundingBox.Max.Length); }