public void NotifyObjectCollision(CustomCollider g, SCollisionInfo collInfo) { ICollideable[] listeners = g.gameObject.GetComponents <ICollideable> (); foreach (ICollideable l in listeners) { l.OnCollide(collInfo); } }
public void OnCollide(SCollisionInfo collInfo) { CustomRigidBody rb = collInfo.otherObj.GetComponent <CustomRigidBody> (); if (rb != null) { rb.SetWindVelocity(m_currentWind); } }
public void OnCollide(SCollisionInfo collInfo) { //When fly colliders with water or an end zone, it disappears switch (collInfo.otherObj.gameObject.tag) { case "Water": Destroy(this.gameObject); break; } }
public void OnCollide(SCollisionInfo collInfo) { if (collInfo.otherObj.tag == "Bug") { FlyAI fly = collInfo.otherObj.GetComponent <FlyAI> (); if (fly != null) { fly.DropFly(); } Destroy(this.gameObject); } }
public void OnCollide(SCollisionInfo collInfo) { if (bounceOnImpact && !collInfo.otherObj.isTrigger) { //This is gonna be some weird jank. Vector2 currentVelocity = GetActiveVelocity(); Vector2 normal = collInfo.normal; m_inputVelocity += Vector2.Dot(-currentVelocity, normal) * normal * (1 + bounceCoeff); } collisionPoint = (Vector3)collInfo.collisionPoint; }
//TODO: Have a more general way of checking collisions if I implement arbitrary shape collision //TODO: OctTree Spatial partitioning to reduce number of objects we check against each other public void LateUpdate() { List <CustomCollider> tempList = new List <CustomCollider> (m_colliders); tempList.RemoveAll(item => item == null); tempList.Sort(delegate(CustomCollider x, CustomCollider y) { return(x.updateOrder - y.updateOrder); }); foreach (CustomCollider mainCollider in tempList) { //Ignore static objects or deactivated objects if (mainCollider.isStatic) { continue; } if (mainCollider.GetType() == typeof(CustomCircleCollider)) { foreach (CustomCollider otherCollider in tempList) { //If we're comparing with ourselves skip, or if the other collider isn't active if (mainCollider == otherCollider) { continue; } // We use the convenient CollisionMatrix unity provides as a data structure to determine whether two objects should collide or not // Since we aren't using any unity physics, it won't affect anything else in the game accidentally if (Physics2D.GetIgnoreLayerCollision(mainCollider.gameObject.layer, otherCollider.gameObject.layer)) { continue; } Circle c1 = ((CustomCircleCollider)mainCollider).GetCircle(); bool collided = false; SIntersectionInfo inxInfo = new SIntersectionInfo(); //Get collision information if it applies if (otherCollider.GetType() == typeof(CustomCircleCollider)) { Circle c2 = ((CustomCircleCollider)otherCollider).GetCircle(); collided = Geometry.Intersect(c1, c2, out inxInfo); } else if (otherCollider.GetType() == typeof(CustomRectangleCollider)) { Box b = ((CustomRectangleCollider)otherCollider).GetBox(); collided = Geometry.Intersect(c1, b, out inxInfo); } if (collided) { //Case 0: Collisions are ignored because of IgnoreCollisionsMatrix //Case 1: Dynamic - Static Collision // Case 1.1 One of the two isTrigger : No displacement, notify both objects // Case 1.2 Neither are isTrigger : Displace first Object by the full intersectionDepth, notify both objects //Case 2: Dynamic - Dynamic Collision // Case 2.1: One of the two isTrigger : No displacement, notify first object (second object will get notified once we get to it in the list) // Case 2.2: Neither object isTrigger : Displace each in opposite direction by half the intersectionDepth, notify both objects (because once we get to the second object, there should be no collision) bool triggerCollision = mainCollider.isTrigger || otherCollider.isTrigger; //Calculating the displacement amount, also used to set the intersection point of the second object Vector2 pushBack = c1.Center - inxInfo.intersectionPoint; pushBack = pushBack.normalized * inxInfo.intersectionDepth; SCollisionInfo mainCollInfo = new SCollisionInfo(otherCollider, inxInfo.intersectionPoint, inxInfo.intersectionDepth, pushBack.normalized); SCollisionInfo secondaryCollInfo = new SCollisionInfo(mainCollider, inxInfo.intersectionPoint + pushBack, inxInfo.intersectionDepth, -(pushBack.normalized)); //Always notify first Object of the collision NotifyObjectCollision(mainCollider, mainCollInfo); //Case 1.2, 2.1, 2.2 notify all objects if it's not a trigger collision //Case 1.1 Notify both objects if it's a trigger, but second object is static if (!triggerCollision || otherCollider.isStatic) { NotifyObjectCollision(otherCollider, secondaryCollInfo); } //Never any displacement if it's a trigger if (!triggerCollision) { //Case 1.2 if (otherCollider.isStatic) { //If the gameobject decided to self destruct or deactive when it was notified of the collision, no need to move it if (mainCollider != null && mainCollider.gameObject.activeInHierarchy) { mainCollider.transform.position += (Vector3)pushBack; } } //Case 2.2 else { //If either object was destroyed or deactivated when it was notified of a collision, no need to displace either of them if (mainCollider != null && otherCollider != null && mainCollider.gameObject.activeInHierarchy && otherCollider.gameObject.activeInHierarchy) { mainCollider.transform.position += (Vector3)pushBack * 0.5f; otherCollider.transform.position -= (Vector3)pushBack * 0.5f; } } } } } } } }
//Destroys anything which enters it public void OnCollide(SCollisionInfo collInfo) { Destroy(collInfo.otherObj.gameObject); }
static void Main(String[] args) { var Begin = new SPoint(0, 0); var End = new SPoint(10, 5); while (!CPhysics.MoveTowards(Begin, End, 0.5f)) { Console.WriteLine(Begin.X + " " + Begin.Y); } Console.WriteLine(Begin.X + " " + Begin.Y); return; var MyCenter = new SPoint(0.7059453529691696f, 0.95074967547869216f); var TargetCenter = new SPoint(0.69332838119407825f, 0.78701901149100739f); var Vel0 = new SPoint(0.19000000000000197f, -0.38262210845947314f); var Theta0 = Math.Atan2(Vel0.Y, Vel0.X); // My Scalar var Scalar = Vel0.GetScalar(); // Get Me To Target Vector var LinkVector = TargetCenter.GetSub(MyCenter); // 나의 속도벡터를 나와 대상을 연결한 직선과 나란한 성분A와 A와 수직이 B를 구하고 // A, B의 X,Y 성분을 구하고, B-A 를 구하면 이것이 BalloonFight 의 충돌후의 내 Vector var LinkTheta = CBase.ThetaOfTwoVectors(LinkVector, Vel0); var ScalarA = Mathf.Abs(Mathf.Cos(LinkTheta) * Scalar); var ThetaA = Mathf.Atan2(LinkVector.Y, LinkVector.X); var VecA = new SPoint(Mathf.Cos(ThetaA) * ScalarA, Mathf.Sin(ThetaA) * ScalarA); var ScalarB = Mathf.Abs(Mathf.Sin(LinkTheta) * Scalar); var ThetaB = ThetaA - (Mathf.PI * 0.5f); var VecB = new SPoint(Mathf.Cos(ThetaB) * ScalarB, Mathf.Sin(ThetaB) * ScalarB); var BouncedVec = VecB.GetSub(VecA); Console.WriteLine(VecA.X + " " + VecA.Y); Console.WriteLine(VecB.X + " " + VecB.Y); Console.WriteLine(BouncedVec.X + " " + BouncedVec.Y); return; if (CPhysics.IsOverlappedRectRect(new SRect(0.0f, 1.0f, 0.0f, 1.0f), new SRect(1.0f, 3.0f, 0.0f, 1.0f))) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } return; var CollisionInfo = new SCollisionInfo(); if (CPhysics.IsCollidedRectRect2(new SRect(0.0f, 2.0f, 0.0f, 1.0f), new SRect(1.0f, 3.0f, 0.0f, 1.0f), new SPoint(1.0f, 0.0f), new SPoint(0.0f, 0.0f), CollisionInfo)) { Console.WriteLine("true" + CollisionInfo.Time); } else { Console.WriteLine("false" + CollisionInfo.Time); } SPoint p = new SPoint(0.00000001f, 0.0f); SLine l = new SLine(new SPoint(0.0f, 1.0f), new SPoint(2.0f, 1.0f)); var o = CBase.SymmetryPoint(p, l); Console.WriteLine(o.X + " " + o.Y); }
public void Set(SCollisionInfo Obj_) { Time = Obj_.Time; Point.Set(Obj_.Point); Normal.Set(Obj_.Normal); }
public static bool IsCollidedPointVLine(SPoint Point_, SVLine VLine_, SPoint Dir_, SCollisionInfo CollisionInfo_) { if (Dir_.X == 0.0) { return(false); } if (Dir_.X < 0.0 && Point_.X < VLine_.X) { return(false); } if (Dir_.X > 0.0 && Point_.X > VLine_.X) { return(false); } var ContactY = (Dir_.Y / Dir_.X) * (VLine_.X - Point_.X) + Point_.Y; CollisionInfo_.Time = (VLine_.X - Point_.X) / Dir_.X; if (CollisionInfo_.Time < 0.0) { return(false); } CollisionInfo_.Point.X = VLine_.X; CollisionInfo_.Point.Y = ContactY; CollisionInfo_.Normal.X = -Dir_.X; CollisionInfo_.Normal.Y = 0.0f; return(true); }
public SCollisionInfo(SCollisionInfo Obj_) { Time = Obj_.Time; Point = Obj_.Point; Normal = Obj_.Normal; }
public static bool IsCollidedRectRect2(SRect Rect0_, SRect Rect1_, SPoint Dir0_, SPoint Dir1_, SCollisionInfo CollisionInfo_) { if (!IsCollidedRectRect(Rect0_, Rect1_, Dir0_.GetSub(Dir1_), CollisionInfo_)) { return(false); } CollisionInfo_.Point.Add(Dir1_.GetMulti(CollisionInfo_.Time)); return(true); }
public static bool IsCollidedRectRect(SRect Rect0_, SRect Rect1_, SPoint Dir_, SCollisionInfo CollisionInfo_) { if (Dir_.X > 0.0) { if (IsCollidedVSegmentVSegment( new SVSegment(new SVLine(Rect0_.Right), Rect0_.Bottom, Rect0_.Top), new SVSegment(new SVLine(Rect1_.Left), Rect1_.Bottom, Rect1_.Top), Dir_, CollisionInfo_)) { return(true); } } else if (Dir_.X < 0.0) { if (IsCollidedVSegmentVSegment( new SVSegment(new SVLine(Rect0_.Left), Rect0_.Bottom, Rect0_.Top), new SVSegment(new SVLine(Rect1_.Right), Rect1_.Bottom, Rect1_.Top), Dir_, CollisionInfo_)) { return(true); } } if (Dir_.Y > 0.0) { if (IsCollidedHSegmentHSegment( new SHSegment(new SHLine(Rect0_.Top), Rect0_.Left, Rect0_.Right), new SHSegment(new SHLine(Rect1_.Bottom), Rect1_.Left, Rect1_.Right), Dir_, CollisionInfo_)) { return(true); } } else if (Dir_.Y < 0.0) { if (IsCollidedHSegmentHSegment( new SHSegment(new SHLine(Rect0_.Bottom), Rect0_.Left, Rect0_.Right), new SHSegment(new SHLine(Rect1_.Top), Rect1_.Left, Rect1_.Right), Dir_, CollisionInfo_)) { return(true); } } return(false); }
public static bool IsCollidedHSegmentHSegment(SHSegment HSegment0_, SHSegment HSegment1_, SPoint Dir_, SCollisionInfo CollisionInfo_) { if (!IsCollidedPointHLine(new SPoint(HSegment0_.Left, HSegment0_.Y), HSegment1_, Dir_, CollisionInfo_)) { return(false); } // 라인의 중첩영역을 임시로 CollisionInfo의 Point에 저장 CollisionInfo_.Point.Y = CollisionInfo_.Point.X + (HSegment0_.Right - HSegment0_.Left); if (CollisionInfo_.Point.X < HSegment1_.Left) // Point.Y : ContactLeft { CollisionInfo_.Point.X = HSegment1_.Left; } if (CollisionInfo_.Point.Y > HSegment1_.Right) // Point.X : ContactRight { CollisionInfo_.Point.Y = HSegment1_.Right; } if (CollisionInfo_.Point.Y < CollisionInfo_.Point.X) // 중첩영역이 없으면 { return(false); } CollisionInfo_.Point.X = ((CollisionInfo_.Point.Y + CollisionInfo_.Point.X) / 2.0f); CollisionInfo_.Point.Y = HSegment1_.Y; CollisionInfo_.Normal.X = 0.0f; CollisionInfo_.Normal.Y = -Dir_.Y; return(true); }
public static bool IsCollidedVSegmentVSegment(SVSegment VSegment0_, SVSegment VSegment1_, SPoint Dir_, SCollisionInfo CollisionInfo_) { if (!IsCollidedPointVLine(new SPoint(VSegment0_.X, VSegment0_.Bottom), VSegment1_, Dir_, CollisionInfo_)) { return(false); } // 라인의 중첩영역을 임시로 CollisionInfo의 Point에 저장 CollisionInfo_.Point.X = CollisionInfo_.Point.Y + (VSegment0_.Top - VSegment0_.Bottom); if (CollisionInfo_.Point.Y < VSegment1_.Bottom) // Point.Y : ContactBottom { CollisionInfo_.Point.Y = VSegment1_.Bottom; } if (CollisionInfo_.Point.X > VSegment1_.Top) // Point.X : ContactTop { CollisionInfo_.Point.X = VSegment1_.Top; } if (CollisionInfo_.Point.X < CollisionInfo_.Point.Y) // 중첩영역이 없으면 { return(false); } CollisionInfo_.Point.Y = ((CollisionInfo_.Point.X + CollisionInfo_.Point.Y) / 2.0f); CollisionInfo_.Point.X = VSegment1_.X; CollisionInfo_.Normal.X = -Dir_.X; CollisionInfo_.Normal.Y = 0.0f; return(true); }
public static bool IsCollidedPointHLine(SPoint Point_, SHLine HLine_, SPoint Dir_, SCollisionInfo CollisionInfo_) { if (Dir_.Y == 0.0) { return(false); } if (Dir_.Y < 0.0 && Point_.Y < HLine_.Y) { return(false); } if (Dir_.Y > 0.0 && Point_.Y > HLine_.Y) { return(false); } var ContactX = (Dir_.X / Dir_.Y) * (HLine_.Y - Point_.Y) + Point_.X; CollisionInfo_.Time = (HLine_.Y - Point_.Y) / Dir_.Y; if (CollisionInfo_.Time < 0.0) { return(false); } CollisionInfo_.Point.X = ContactX; CollisionInfo_.Point.Y = HLine_.Y; CollisionInfo_.Normal.X = 0.0f; CollisionInfo_.Normal.Y = -Dir_.Y; return(true); }