// Update is called once per frame void Update() { Debug.Log("Angle between planet and player is " + Vector3.Angle(planet.transform.position, player.transform.position)); Debug.Log("Angle between planet and enemy is " + Vector3.Angle(planet.transform.position, transform.position)); float sphereAngle = angleBetweenTwoPointsOnSphere(transform.position, player.transform.position, planet.transform.position); Debug.Log("Sphere angle is " + sphereAngle); if (sphereAngle <= 20.0f) { Plane2 plane = new Plane2(transform.parent.up, transform.position); Vector2 mappedPoint = plane.GetMappedPoint(player.transform.position) - plane.GetMappedPoint(transform.position); //Debug.Log("mappedPoint x is " + mappedPoint.x + " and mappedPoint y is " + mappedPoint.y); //Debug.Log("Unmapped point is " + (mappedPoint.x * plane.xDir + mappedPoint.y * plane.yDir)); Debug.DrawLine(transform.position, transform.position + mappedPoint.x * plane.xDir + mappedPoint.y * plane.yDir, Color.red); if (mappedPoint.magnitude < 10f) { Debug.Log("Magnitiude tiny"); return; } float angle = (float)(Math.Atan2((transform.position + mappedPoint.x * plane.xDir + mappedPoint.y * plane.yDir).z - transform.position .z, (transform.position + mappedPoint.x * plane.xDir + mappedPoint.y * plane.yDir).x - transform.position.x) * 180 / Math.PI); //Debug.Log("Calculated angle is " + angle + " and Vector3 angle is " + Vector3.Angle(transform.position, player.transform.position)); //transform.rotation = Quaternion.Euler(0f, 270 - angle, 0f); //transform.localRotation = Quaternion.AngleAxis(angle, player.transform.position); } }
public void golemSphereMovement() { //This code moves the golem towards the player using plane-logic //For shrab, instead of transform.position.normalized, raycast to the center of the planet and use hit.normal Plane2 plane = new Plane2(transform.position.normalized, transform.position); Vector2 mappedPoint = plane.GetMappedPoint(player.transform.position) - plane.GetMappedPoint(transform.position); if (mappedPoint.magnitude > 1) { transform.LookAt(mappedPoint.x * plane.xDir + mappedPoint.y * plane.yDir + transform.position, transform.position.normalized); } RaycastHit hit; if (Physics.Raycast(transform.position, (planet.transform.position - transform.position).normalized, out hit, Mathf.Infinity)) { Plane2 alignPlane = new Plane2(hit.normal, transform.position); //Vector2 mappedPoint2 = alignPlane.GetMappedPoint(player.transform.position) - alignPlane.GetMappedPoint(transform.position); //rb.AddForce((mappedPoint2.x * alignPlane.xDir + mappedPoint2.y * alignPlane.yDir).normalized * speed); if (Vector3.Distance(hit.point, transform.position) >= 1f) { rb.AddForce(transform.position.normalized * gravity * 2); //rb.AddForce((mappedPoint2.x * alignPlane.xDir + mappedPoint2.y * alignPlane.yDir).normalized * speed/-2f); } } //adding force towards gravity, adding force towards direction faced rb.AddForce(transform.forward * speed); rb.AddForce(transform.position.normalized * gravity); }
public override YAMLNode ExportYAML(IExportContainer container) { #warning TODO: values acording to read version (current 2017.3.0f3) YAMLMappingNode node = (YAMLMappingNode)base.ExportYAML(container); node.AddSerializedVersion(GetSerializedVersion(container.Version)); node.Add("type", (int)Type); node.Add("collisionMode", (int)CollisionMode); node.Add("colliderForce", ColliderForce); node.Add("multiplyColliderForceByParticleSize", MultiplyColliderForceByParticleSize); node.Add("multiplyColliderForceByParticleSpeed", MultiplyColliderForceByParticleSpeed); node.Add("multiplyColliderForceByCollisionAngle", GetExportMultiplyColliderForceByCollisionAngle(container.Version)); node.Add("plane0", Plane0.ExportYAML(container)); node.Add("plane1", Plane1.ExportYAML(container)); node.Add("plane2", Plane2.ExportYAML(container)); node.Add("plane3", Plane3.ExportYAML(container)); node.Add("plane4", Plane4.ExportYAML(container)); node.Add("plane5", Plane5.ExportYAML(container)); node.Add("m_Dampen", Dampen.ExportYAML(container)); node.Add("m_Bounce", Bounce.ExportYAML(container)); node.Add("m_EnergyLossOnCollision", EnergyLossOnCollision.ExportYAML(container)); node.Add("minKillSpeed", MinKillSpeed); node.Add("maxKillSpeed", GetExportMaxKillSpeed(container.Version)); node.Add("radiusScale", GetExportRadiusScale(container.Version)); node.Add("collidesWith", GetExportCollidesWith(container.Version).ExportYAML(container)); node.Add("maxCollisionShapes", GetExportMaxCollisionShapes(container.Version)); node.Add("quality", (int)Quality); node.Add("voxelSize", GetExportVoxelSize(container.Version)); node.Add("collisionMessages", CollisionMessages); node.Add("collidesWithDynamic", GetExportCollidesWithDynamic(container.Version)); node.Add("interiorCollisions", InteriorCollisions); return(node); }
/// <summary> /// Determines the intersection between a 2d line and a plane /// </summary> /// <param name="start">Line start</param> /// <param name="end">Line end</param> /// <param name="plane">Plane</param> /// <returns>Returns intersection details, or null if there was no intersection.</returns> public static Line2Intersection GetLinePlaneIntersection( Point2 start, Point2 end, Plane2 plane ) { Vector2 vec = end - start; float len = vec.Length; vec /= len; float startDot = plane.Normal.Dot( start ); float diffDot = plane.Normal.Dot( vec ); if ( !Utils.CloseToZero( diffDot ) ) { float t = ( startDot + plane.Distance ) / -diffDot; if ( ( t >= 0 ) && ( t <= len ) ) { Line2Intersection result = new Line2Intersection( ); result.IntersectionPosition = start + ( vec * t ); result.IntersectionNormal = plane.Normal; result.Distance = t; result.StartInside = ( startDot + plane.Distance ) < 0; return result; } } return null; }
public override YAMLNode ExportYAML(IExportContainer container) { YAMLMappingNode node = (YAMLMappingNode)base.ExportYAML(container); node.AddSerializedVersion(ToSerializedVersion(container.ExportVersion)); node.Add(TypeName, (int)Type); node.Add(CollisionModeName, (int)CollisionMode); node.Add(ColliderForceName, ColliderForce); node.Add(MultiplyColliderForceByParticleSizeName, MultiplyColliderForceByParticleSize); node.Add(MultiplyColliderForceByParticleSpeedName, MultiplyColliderForceByParticleSpeed); node.Add(MultiplyColliderForceByCollisionAngleName, GetExportMultiplyColliderForceByCollisionAngle(container.Version)); node.Add(Plane0Name, Plane0.ExportYAML(container)); node.Add(Plane1Name, Plane1.ExportYAML(container)); node.Add(Plane2Name, Plane2.ExportYAML(container)); node.Add(Plane3Name, Plane3.ExportYAML(container)); node.Add(Plane4Name, Plane4.ExportYAML(container)); node.Add(Plane5Name, Plane5.ExportYAML(container)); node.Add(DampenName, Dampen.ExportYAML(container)); node.Add(BounceName, Bounce.ExportYAML(container)); node.Add(EnergyLossOnCollisionName, EnergyLossOnCollision.ExportYAML(container)); node.Add(MinKillSpeedName, MinKillSpeed); node.Add(MaxKillSpeedName, GetExportMaxKillSpeed(container.Version)); node.Add(RadiusScaleName, GetExportRadiusScale(container.Version)); node.Add(CollidesWithName, GetExportCollidesWith(container.Version).ExportYAML(container)); node.Add(MaxCollisionShapesName, GetExportMaxCollisionShapes(container.Version)); node.Add(QualityName, (int)Quality); node.Add(VoxelSizeName, GetExportVoxelSize(container.Version)); node.Add(CollisionMessagesName, CollisionMessages); node.Add(CollidesWithDynamicName, GetExportCollidesWithDynamic(container.Version)); node.Add(InteriorCollisionsName, InteriorCollisions); return(node); }
public void sphericalMovement(Vector3 target, float speed) { orientEnemy(target); target = findRaycastPointOnSphere(target); RaycastHit hit; if (Physics.Raycast(transform.position + transform.position.normalized * 10.0f, (planet.transform.position - transform.position).normalized, out hit, Mathf.Infinity)) { Plane2 alignPlane = new Plane2(hit.normal, transform.position); if (hit.transform.gameObject.tag != "Player") { transform.position = hit.point + hit.point.normalized * 0.75f; } //Vector2 mappedPoint2 = alignPlane.GetMappedPoint(player.transform.position) - alignPlane.GetMappedPoint(transform.position); //rb.AddForce((mappedPoint2.x * alignPlane.xDir + mappedPoint2.y * alignPlane.yDir).normalized * speed); //if (Vector3.Distance(hit.point, transform.position) >= 1f) //{ // rb.AddForce(transform.position.normalized * gravity); //rb.AddForce((mappedPoint2.x * alignPlane.xDir + mappedPoint2.y * alignPlane.yDir).normalized * speed/-2f); //} } //adding force towards gravity, adding force towards direction faced //rb.AddForce(transform.forward * speed); //rb.velocity = (findRaycastPointOnSphere(target) - transform.position).normalized * speed; Debug.Log("Distance between target and position is " + Vector3.Distance(target, transform.position)); //else //{ rb.velocity = (target - transform.position).normalized * speed; //} //rb.AddForce(transform.position.normalized * gravity); }
private void PointInRelationToPlane(MyVector2 a, MyVector2 b, MyVector2 testPoint) { MyVector2 planeDir = b - a; MyVector2 planeNormal = MyVector2.Normalize(new MyVector2(planeDir.y, -planeDir.x)); MyVector2 planePos = a + planeDir * 0.5f; Plane2 plane = new Plane2(planePos, planeNormal); //Positive if infront, negative if behind float distanceToPlane = _Geometry.GetSignedDistanceFromPointToPlane(plane, testPoint); Debug.Log("Distance: " + distanceToPlane); //Display //Plane Gizmos.color = Color.blue; Gizmos.DrawLine(a.ToVector3(), b.ToVector3()); //Plane normal Gizmos.DrawLine(planePos.ToVector3(), planePos.ToVector3() + planeNormal.ToVector3() * 0.1f); //Point Gizmos.color = Color.white; Gizmos.DrawWireSphere(testPoint.ToVector3(), 0.1f); }
public void orientEnemy(Vector3 target) { Plane2 plane = new Plane2(transform.position.normalized, transform.position); Vector2 mappedPoint = plane.GetMappedPoint(target) - plane.GetMappedPoint(transform.position); Vector3 mappedPoint3D = mappedPoint.x * plane.xDir + mappedPoint.y * plane.yDir; Debug.Log("Mapped point magnitude is " + mappedPoint.magnitude); if (mappedPoint.magnitude > 1) { transform.LookAt(mappedPoint3D + transform.position, transform.position.normalized); } }
public void shrabSphereMovement(Vector3 target) { //This code moves the golem towards the player using plane-logic //For shrab, instead of transform.position.normalized, raycast to the center of the planet and use hit.normal Vector3 targetPosition = findRaycastPointOnSphere(target); Vector3 planeInstantiationPoint = Vector3.zero; RaycastHit planeHit; if (Physics.Raycast(transform.position.normalized * 5f + transform.position, (-transform.position).normalized, out planeHit, Mathf.Infinity)) { planeInstantiationPoint = planeHit.normal; //if(Vector3.Distance(transform.position, planeHit.point) >= 2.0f) //{ transform.position = planeHit.point + transform.position.normalized * distanceAbovePlanetSurface; //} } Debug.Log("Plane instantiation point is " + planeInstantiationPoint); Plane2 plane = new Plane2(planeInstantiationPoint, transform.position); Vector2 mappedPoint = plane.GetMappedPoint(target) - plane.GetMappedPoint(transform.position); if (mappedPoint.magnitude > 1) { transform.LookAt(mappedPoint.x * plane.xDir + mappedPoint.y * plane.yDir + transform.position, transform.position.normalized); } /* * RaycastHit hit; * if (Physics.Raycast(transform.position, (planet.transform.position - transform.position).normalized, out hit, Mathf.Infinity)) * { * Plane2 alignPlane = new Plane2(hit.normal, transform.position); * //Vector2 mappedPoint2 = alignPlane.GetMappedPoint(player.transform.position) - alignPlane.GetMappedPoint(transform.position); * //rb.AddForce((mappedPoint2.x * alignPlane.xDir + mappedPoint2.y * alignPlane.yDir).normalized * speed); * if (Vector3.Distance(hit.point, transform.position) >= 1f) * { * rb.AddForce(transform.position.normalized * gravity * 2); * //rb.AddForce((mappedPoint2.x * alignPlane.xDir + mappedPoint2.y * alignPlane.yDir).normalized * speed/-2f); * } * } */ //adding force towards gravity, adding force towards direction faced //float step = Time.deltaTime * speed; rb.AddForce((mappedPoint.x * plane.xDir + mappedPoint.y * plane.yDir).normalized * speed); //transform.position = Vector3.MoveTowards(transform.position, (mappedPoint.x * plane.xDir + mappedPoint.y * plane.yDir), step); //rb.velocity = (mappedPoint.x * plane.xDir + mappedPoint.y * plane.yDir) * speed; //rb.AddForce((mappedPoint.x * plane.xDir + mappedPoint.y * plane.yDir) * speed); //rb.AddForce(transform.position.normalized * gravity * rb.mass); Debug.DrawLine(transform.position, transform.position + mappedPoint.x * plane.xDir + mappedPoint.y * plane.yDir, Color.red); }
//Is a plane intersecting with a plane private void PlanePlane() { Vector3 planeNormal_1 = planeTrans.forward; Vector3 planePos_1 = planeTrans.position; Vector3 planeNormal_2 = rayTrans.forward; Vector3 planePos_2 = rayTrans.position; //3d to 2d MyVector2 normal_1 = planeNormal_1.ToMyVector2(); MyVector2 normal_2 = planeNormal_2.ToMyVector2(); MyVector2 pos1 = planePos_1.ToMyVector2(); MyVector2 pos2 = planePos_2.ToMyVector2(); Plane2 plane_1 = new Plane2(pos1, normal_1); Plane2 plane_2 = new Plane2(pos2, normal_2); //Intersections bool isIntersecting = _Intersections.PlanePlane(plane_1, plane_2); Debug.Log("Are planes intersecting: " + isIntersecting); //Display //if (isIntersecting) //{ // MyVector2 intersectionPoint = Intersections.GetPlanePlaneIntersectionPoint(pos1, normal_1, pos2, normal_2); // Gizmos.DrawWireSphere(intersectionPoint.ToVector3(), 0.2f); //} //Color planeColor = isIntersecting ? Color.red : Color.white; //TestAlgorithmsHelpMethods.DrawPlane(pos1, normal_1, planeColor); //TestAlgorithmsHelpMethods.DrawPlane(pos2, normal_2, planeColor); //Display with mesh TestAlgorithmsHelpMethods.DisplayPlaneMesh(pos1, normal_1, 0.5f, Color.blue); TestAlgorithmsHelpMethods.DisplayPlaneMesh(pos2, normal_2, 0.5f, Color.blue); if (isIntersecting) { MyVector2 intersectionPoint = _Intersections.GetPlanePlaneIntersectionPoint(plane_1, plane_2); TestAlgorithmsHelpMethods.DisplayCircleMesh(intersectionPoint, 1f, 20, Color.red); } }
/// <summary> /// Determines the intersection point between two planes /// </summary> /// <param name="plane0">First plane</param> /// <param name="plane1">Second plane</param> /// <returns>Returns the intersection point, or null if there is no intersection point</returns> public static Point2? GetPlanePlaneIntersection( Plane2 plane0, Plane2 plane1 ) { float den = plane0.Normal.X * plane1.Normal.Y - plane0.Normal.Y * plane1.Normal.X; if ( Utils.CloseToZero( den ) ) { return null; } float x = ( plane0.Normal.Y * plane1.Distance - plane1.Normal.Y * plane0.Distance ) / den; float y = ( plane1.Normal.X * plane0.Distance - plane0.Normal.X * plane1.Distance ) / den; return new Point2( x, y ); }
//Help method to calculate the intersection point between two planes offset in normal direction by a width private static MyVector2 GetIntersectionPoint(MyVector2 a, MyVector2 b, MyVector2 c, float halfWidth, bool isTopPoint) { //Direction of the lines going to and from point b MyVector2 beforeDir = MyVector2.Normalize(b - a); MyVector2 afterDir = MyVector2.Normalize(c - b); MyVector2 beforeNormal = GetNormal(a, b); MyVector2 afterNormal = GetNormal(b, c); //Compare the normals! //normalDirFactor is used to determine if we want to top point (same direction as normal) float normalDirFactor = isTopPoint ? 1f : -1f; //If they are the same it means we have a straight line and thus we cant do plane-plane intersection //if (beforeNormal.Equals(afterNormal)) //When comparing the normals, we cant use the regular small value because then //the line width goes to infinity when doing plane-plane intersection float dot = MyVector2.Dot(beforeNormal, afterNormal); //Dot is 1 if the point in the same dir and -1 if the point in the opposite dir float one = 1f - 0.01f; if (dot > one || dot < -one) { MyVector2 averageNormal = MyVector2.Normalize((afterNormal + beforeNormal) * 0.5f); MyVector2 intersectionPoint = b + averageNormal * halfWidth * normalDirFactor; return(intersectionPoint); } else { //Now we can calculate where the plane starts MyVector2 beforePlanePos = b + beforeNormal * halfWidth * normalDirFactor; MyVector2 afterPlanePos = b + afterNormal * halfWidth * normalDirFactor; Plane2 planeBefore = new Plane2(beforePlanePos, beforeNormal); Plane2 planeAfter = new Plane2(afterPlanePos, afterNormal); //Calculate the intersection point //We know they are intersecting, so we don't need to test that MyVector2 intersectionPoint = _Intersections.GetPlanePlaneIntersectionPoint(planeBefore, planeAfter); return(intersectionPoint); } }
public IEnumerable <Object> FetchDependencies(ISerializedFile file, bool isLog = false) { yield return(Plane0.FetchDependency(file, isLog, () => nameof(CollisionModule), "m_Plane0")); yield return(Plane1.FetchDependency(file, isLog, () => nameof(CollisionModule), "m_Plane1")); yield return(Plane2.FetchDependency(file, isLog, () => nameof(CollisionModule), "m_Plane2")); yield return(Plane3.FetchDependency(file, isLog, () => nameof(CollisionModule), "m_Plane3")); yield return(Plane4.FetchDependency(file, isLog, () => nameof(CollisionModule), "m_Plane4")); yield return(Plane5.FetchDependency(file, isLog, () => nameof(CollisionModule), "m_Plane5")); }
void Update() { time1 += Time.deltaTime; time2 += Time.deltaTime; time3 += Time.deltaTime; if (time1 > 7.0f) { if (Plane1.activeSelf == false) { Plane1.SetActive(true); time1 = 0; } else { Plane1.SetActive(false); time1 = 0; } } if (time2 > 7.0f) { if (Plane2.activeSelf == false) { Plane2.SetActive(true); time2 = 0; } else { Plane2.SetActive(false); time2 = 0; } } if (time3 > 7.0f) { if (Plane3.activeSelf == false) { Plane3.SetActive(true); time3 = 0; } else { Plane3.SetActive(false); time3 = 0; } } }
//Method is not in use right now public void Attract() { //Debug.Log("In attract function"); Vector3 gravityUp = (transform.position - planet.transform.position).normalized; Vector3 localUp = transform.up; //Quaternion targetRotation = Quaternion.FromToRotation(Vector3.up, transform.position.normalized); //Quaternion finalRotation = Quaternion.RotateTowards(transform.rotation, targetRotation, float.PositiveInfinity); // Debug.Log("Final rotation is " + finalRotation.eulerAngles); //transform.rotation = finalRotation; Plane2 plane = new Plane2(transform.position.normalized, transform.position); Vector2 mappedPoint = plane.GetMappedPoint(player.transform.position) - plane.GetMappedPoint(transform.position); transform.LookAt(mappedPoint.x * plane.xDir + mappedPoint.y * plane.yDir + transform.position, transform.position.normalized); //float angle = (float)(Math.Atan2(mappedPoint.y, mappedPoint.x) * 180 / Math.PI); //Debug.Log("Calculated angle is " + angle + " and Vector3 angle is " + Vector3.Angle(transform.position, player.transform.position)); // transform.localRotation = Quaternion.Euler(0f, angle, 0f); //rb.rotation = Quaternion.FromToRotation(localUp, gravityUp) * rb.rotation; }
public override void Read(AssetReader reader) { base.Read(reader); Type = (ParticleSystemCollisionType)reader.ReadInt32(); if (IsReadCollisionMode(reader.Version)) { CollisionMode = (ParticleSystemCollisionMode)reader.ReadInt32(); } if (IsReadColliderForce(reader.Version)) { ColliderForce = reader.ReadSingle(); MultiplyColliderForceByParticleSize = reader.ReadBoolean(); MultiplyColliderForceByParticleSpeed = reader.ReadBoolean(); MultiplyColliderForceByCollisionAngle = reader.ReadBoolean(); reader.AlignStream(AlignType.Align4); } Plane0.Read(reader); Plane1.Read(reader); Plane2.Read(reader); Plane3.Read(reader); Plane4.Read(reader); Plane5.Read(reader); if (IsReadDampenSingle(reader.Version)) { float dampenSingle = reader.ReadSingle(); float bounceSingle = reader.ReadSingle(); float energyLossOnCollisionSingle = reader.ReadSingle(); Dampen = new MinMaxCurve(dampenSingle); Bounce = new MinMaxCurve(bounceSingle); EnergyLossOnCollision = new MinMaxCurve(energyLossOnCollisionSingle); } else { Dampen.Read(reader); Bounce.Read(reader); EnergyLossOnCollision.Read(reader); } MinKillSpeed = reader.ReadSingle(); if (IsReadMaxKillSpeed(reader.Version)) { MaxKillSpeed = reader.ReadSingle(); } if (IsReadRadiusScale(reader.Version)) { RadiusScale = reader.ReadSingle(); CollidesWith.Read(reader); } if (IsReadMaxCollisionShapes(reader.Version)) { MaxCollisionShapes = reader.ReadInt32(); } if (IsReadQuality(reader.Version)) { Quality = (ParticleSystemCollisionQuality)reader.ReadInt32(); VoxelSize = reader.ReadSingle(); } if (IsReadCollisionMessages(reader.Version)) { CollisionMessages = reader.ReadBoolean(); } if (IsReadCollidesWithDynamic(reader.Version)) { CollidesWithDynamic = reader.ReadBoolean(); InteriorCollisions = reader.ReadBoolean(); reader.AlignStream(AlignType.Align4); } }
/// <summary> /// Determines the intersection between a 2d line and a plane /// </summary> /// <param name="x">Line start x coordinate</param> /// <param name="y">Line start y coordinate</param> /// <param name="endX">Line end x coordinate</param> /// <param name="endY">Line end y coordinate</param> /// <param name="plane">Plane</param> /// <returns>Returns intersection details, or null if there was no intersection.</returns> public static Line2Intersection GetLinePlaneIntersection( float x, float y, float endX, float endY, Plane2 plane) { return GetLinePlaneIntersection( new Point2( x, y ), new Point2( endX, endY ), plane ); }
public override void Read(AssetStream stream) { base.Read(stream); Type = stream.ReadInt32(); if (IsReadCollisionMode(stream.Version)) { CollisionMode = stream.ReadInt32(); } if (IsReadColliderForce(stream.Version)) { ColliderForce = stream.ReadSingle(); MultiplyColliderForceByParticleSize = stream.ReadBoolean(); MultiplyColliderForceByParticleSpeed = stream.ReadBoolean(); MultiplyColliderForceByCollisionAngle = stream.ReadBoolean(); stream.AlignStream(AlignType.Align4); } Plane0.Read(stream); Plane1.Read(stream); Plane2.Read(stream); Plane3.Read(stream); Plane4.Read(stream); Plane5.Read(stream); if (IsReadDampenSingle(stream.Version)) { DampenSingle = stream.ReadSingle(); BounceSingle = stream.ReadSingle(); EnergyLossOnCollisionSingle = stream.ReadSingle(); } else { Dampen.Read(stream); Bounce.Read(stream); EnergyLossOnCollision.Read(stream); } MinKillSpeed = stream.ReadSingle(); if (IsReadMaxKillSpeed(stream.Version)) { MaxKillSpeed = stream.ReadSingle(); } if (IsReadRadiusScale(stream.Version)) { RadiusScale = stream.ReadSingle(); CollidesWith.Read(stream); } if (IsReadMaxCollisionShapes(stream.Version)) { MaxCollisionShapes = stream.ReadInt32(); } if (IsReadQuality(stream.Version)) { Quality = stream.ReadInt32(); VoxelSize = stream.ReadSingle(); } if (IsReadCollisionMessages(stream.Version)) { CollisionMessages = stream.ReadBoolean(); } if (IsReadCollidesWithDynamic(stream.Version)) { CollidesWithDynamic = stream.ReadBoolean(); InteriorCollisions = stream.ReadBoolean(); stream.AlignStream(AlignType.Align4); } }
//Is a ray intersecting with a plane? private void RayPlane() { Vector3 planeNormal = planeTrans.forward; Vector3 planePos = planeTrans.position; Vector3 rayPos = rayTrans.position; Vector3 rayDir = rayTrans.forward; //2d space Plane2 plane_2d = new Plane2(planePos.ToMyVector2(), planeNormal.ToMyVector2()); Ray2 ray_2d = new Ray2(rayPos.ToMyVector2(), rayDir.ToMyVector2()); //Might as well test the distance from the point to the plane as well float distance = _Geometry.GetSignedDistanceFromPointToPlane(plane_2d, ray_2d.origin); Debug.Log(distance); bool isIntersecting = _Intersections.RayPlane(plane_2d, ray_2d); //Debug Gizmos.color = Color.blue; TestAlgorithmsHelpMethods.DrawPlane(plane_2d.pos, plane_2d.normal, Color.blue); //Ray //Gizmos.color = Color.white; //Gizmos.DrawWireSphere(rayPos, 0.1f); //if (isIntersecting) //{ // Gizmos.color = Color.red; // MyVector2 intersectionPoint = Intersections.GetRayPlaneIntersectionPoint(planePos_2d, planeNormal_2d, rayPos_2d, rayDir_2d); // Gizmos.DrawWireSphere(intersectionPoint.ToVector3(), 0.2f); //} //Gizmos.DrawRay(rayPos, rayDir * 100f); //Display with mesh //Plane TestAlgorithmsHelpMethods.DisplayPlaneMesh(plane_2d.pos, plane_2d.normal, 0.5f, Color.blue); //Ray TestAlgorithmsHelpMethods.DisplayArrowMesh(ray_2d.origin, ray_2d.origin + ray_2d.dir * 6f, 0.5f, 0.5f + 0.5f, Color.white); if (isIntersecting) { MyVector2 intersectionPoint = _Intersections.GetRayPlaneIntersectionPoint(plane_2d, ray_2d); TestAlgorithmsHelpMethods.DisplayCircleMesh(intersectionPoint, 1f, 20, Color.red); } }
//Is a line intersecting with a plane? private void LinePlane() { Vector3 planeNormal = planeTrans.forward; Vector3 planePos = planeTrans.position; Vector3 line_p1 = t1_p1_trans.position; Vector3 line_p2 = t1_p2_trans.position; //2d space MyVector2 planeNormal_2d = planeNormal.ToMyVector2(); MyVector2 planePos_2d = planePos.ToMyVector2(); MyVector2 line_p1_2d = line_p1.ToMyVector2(); MyVector2 line_p2_2d = line_p2.ToMyVector2(); Plane2 plane = new Plane2(planePos_2d, planeNormal_2d); Edge2 line = new Edge2(line_p1_2d, line_p2_2d); bool isIntersecting = _Intersections.LinePlane(plane, line); //Debug //TestAlgorithmsHelpMethods.DrawPlane(planePos_2d, planeNormal_2d, Color.blue); ////Line //Gizmos.color = Color.white; //Gizmos.DrawWireSphere(line_p1, 0.1f); //Gizmos.DrawWireSphere(line_p2, 0.1f); //if (isIntersecting) //{ // Gizmos.color = Color.red; // MyVector2 intersectionPoint = Intersections.GetLinePlaneIntersectionPoint(planePos_2d, planeNormal_2d, line_p1_2d, line_p2_2d); // Gizmos.DrawWireSphere(intersectionPoint.ToVector3(), 0.2f); //} //Gizmos.DrawLine(line_p1, line_p2); //Display with mesh //Plane TestAlgorithmsHelpMethods.DisplayPlaneMesh(planePos_2d, planeNormal_2d, 0.5f, Color.blue); //Line TestAlgorithmsHelpMethods.DisplayLineMesh(line_p1_2d, line_p2_2d, 0.5f, Color.white); if (isIntersecting) { MyVector2 intersectionPoint = _Intersections.GetLinePlaneIntersectionPoint(plane, line); TestAlgorithmsHelpMethods.DisplayCircleMesh(intersectionPoint, 1f, 20, Color.red); } }
/// <summary> /// Tests plane-plane intersection code /// </summary> public static void TestPlanePlaneIntersection( ) { // TODO: Send to unit testing project System.Random rand = new System.Random( ); for ( int i = 0; i < 100; ++i ) { Point2 p0 = RandomPoint( rand ); Point2 p1 = RandomPoint( rand ); Point2 p2 = RandomPoint( rand ); Plane2 plane0 = new Plane2( p0, p1 ); Plane2 plane1 = new Plane2( p1, p2 ); Point2? intersection = GetPlanePlaneIntersection( plane0, plane1 ); if ( intersection != null ) { System.Diagnostics.Debug.Assert( intersection.Value.DistanceTo( p1 ) < 0.001f ); } } }