private void OnPhysicsCollisionExit(PhysicsCollider other) { if (PlayerOn && other.GetComponent <PlayerMovement>()) { Debug.Log("Player leaving planet"); PlayerOn = null; } }
void OnPhysicsCollisionExit(PhysicsCollider other) { if (other.CompareTag("Terrain")) { Debug.Log("Left planet"); physics.attractionActive = true; onPlanet = null; } }
public static PhysicsBody Reflect(PhysicsBody body, PhysicsCollider other) { //Get vector from other to body (collision normal) Vector2 norm = other.Center - body.Collider.Center; //Reflect body velocity across normal return(body); }
public override bool Overlapping(PhysicsCollider other) { if (other is CirclePhysicsCollider) { Vector2 btwn = other.Center - Center; float r = radius + ((CirclePhysicsCollider)other).radius; return(Vector2.SqrMagnitude(btwn) <= r * r); } else { throw new System.NotImplementedException(); } }
void Update() { //TODO: make more efficient (don't test EVERY single collider pair every frame) //ideas: use a different data structure? (HashSet lookup is O(1) though, perhaps worse in practice) //find a smarter way to choose comparisons? Recursive Sectoring? //check each collider pair for collisions for (int l = 0; l < cols.Count; l++) { for (int r = l + 1; r < cols.Count; r++) { PhysicsCollider lcol = cols[l]; PhysicsCollider rcol = cols[r]; CollisionInfo info = new CollisionInfo(lcol, rcol); if (lcol.Overlapping(rcol)) { if (atlas.Contains(info)) { //continuous collision lcol.SendMessage("OnPhysicsCollisionStay", rcol, SendMessageOptions.DontRequireReceiver); rcol.SendMessage("OnPhysicsCollisionStay", lcol, SendMessageOptions.DontRequireReceiver); } else { //new collision //Debug.Log("Adding collision"); atlas.Add(info); lcol.SendMessage("OnPhysicsCollisionEnter", rcol, SendMessageOptions.DontRequireReceiver); rcol.SendMessage("OnPhysicsCollisionEnter", lcol, SendMessageOptions.DontRequireReceiver); } ResolveCollision(info); } else if (atlas.Contains(info)) { //collision ended atlas.Remove(info); lcol.SendMessage("OnPhysicsCollisionExit", rcol, SendMessageOptions.DontRequireReceiver); rcol.SendMessage("OnPhysicsCollisionExit", lcol, SendMessageOptions.DontRequireReceiver); } } } }
void OnPhysicsCollisionEnter(PhysicsCollider other) { if (other.CompareTag("Terrain")) //hit planet { if (onPlanet == null) { Debug.Log("Hit planet"); physics.attractionActive = false; physics.Stop(); onPlanet = other.GetComponent <Planet>(); } else //squished between planets //TODO: kill player (probably a different script tho) { } } }
public override Vector2 GetOverlapVector(PhysicsCollider other) { //returns a vector pointing away from other collider if (other is CirclePhysicsCollider) { if (!Overlapping(other)) { return(Vector2.zero); } Vector2 dist = Center - ((CirclePhysicsCollider)other).Center; return(dist * (radius + ((CirclePhysicsCollider)other).radius - dist.magnitude)); } else { throw new System.NotImplementedException(); } }
public abstract Vector2 GetOverlapVector(PhysicsCollider other);
public abstract bool Overlapping(PhysicsCollider other);
private void OnPhysicsCollisionEnter(PhysicsCollider other) { Debug.Log("Player entering planet"); PlayerOn = other.GetComponent <PlayerMovement>(); }
void Start() { velocity = Vector2.zero; acceleration = Vector2.zero; Collider = GetComponent <PhysicsCollider>(); }
public void RemoveCollider(PhysicsCollider collider) { cols.Remove(collider); }
public void RegisterCollider(PhysicsCollider collider) { cols.Add(collider); }
public CollisionInfo(PhysicsCollider l, PhysicsCollider r) { col1 = l; col2 = r; }