public override void OnCollide(PhysicsEntity other) { other.AddForce(accelerationChange); if (HasParent) { parent.OnCollide(other); /* having problems * if (other.Rect.Contains(parent.Rect)) * { * X = parentCentrer.X; * Y = parentCentrer.Y; * rectangle.Width = 0; * rectangle.Height = 0; * return; * } * if (other.X >= parentCentrer.X && other.X < rectangle.Width + X && other.X > X) * { * rectangle.Width = X + (other.X - X); * } * else if (other.X >= parentCentrer.X && other.X + other.Rect.Width > X && other.X < X) * { * rectangle.Width = 0; * } * else if (other.X + other.Rect.Width <= parentCentrer.X && other.X + other.Rect.Width < rectangle.Width + X && other.X + other.Rect.Width > X) * { * rectangle.Width = other.X + other.Rect.Width + ((X+rectangle.Width) - (other.X + other.Rect.Width)); * rectangle.X = other.X + other.Rect.Width; * } * else if (other.X + other.Rect.Width <= parentCentrer.X && other.X + other.Rect.Width > X + rectangle.Width && other.X > X) * { * rectangle.Width = 0; * } * * if (other.Y >= parentCentrer.Y && other.Y < rectangle.Height + Y && other.Y > Y) * { * rectangle.Height = Y + (other.Y - Y); * } * else if (other.Y >= parentCentrer.Y && other.Y + other.Rect.Height > Y && other.Y < Y) * { * rectangle.Height = 0; * } * * else if (other.Y + other.Rect.Height <= parentCentrer.Y && other.Y + other.Rect.Height < rectangle.Height + Y && other.Y + other.Rect.Height > Y) * { * rectangle.Height = other.Y + other.Rect.Height + ((Y + rectangle.Height) - (other.Y + other.Rect.Height)); * rectangle.Y = other.Y + other.Rect.Height; * } * else if (other.Y + other.Rect.Height <= parentCentrer.Y && other.Y + other.Rect.Height > Y + rectangle.Height && other.Y > Y) * { * rectangle.Height = 0; * } */ } }
public void DetectCollisions() { bool newCollision; if (objects != null) { for (int i = 0; i < entities.Count; i++) { newCollision = true; physEntity = entities[i];//if it hasn't move, it won't have new collisions, if somehting hits it, that entity will take care of collision if (physEntity.X == physEntity.PrevX && physEntity.Y == physEntity.PrevY) { newCollision = false; } // Reset collision array physEntity.colliderArray[0] = false; physEntity.colliderArray[1] = false; physEntity.colliderArray[2] = false; physEntity.colliderArray[3] = false; // Calculate the bottom and right side locations for the physEntity float entBottom = physEntity.Y + physEntity.Rect.Height; float entRight = physEntity.X + physEntity.Rect.Width; float entTop = physEntity.Y; float entLeft = physEntity.X; for (int j = 0; j < objects.Count; j++) { gameObject = objects[j]; if (gameObject == physEntity) { continue; } if (gameObject is Bottle && physEntity is Bottle) { continue; } if (((gameObject is Door || gameObject is Button || gameObject is EffectBox) && physEntity is Player) || (gameObject is Player && (physEntity is Door || physEntity is Button || gameObject is EffectBox))) { } else if (Game1.developerMode && (gameObject is Player || physEntity is Player)) { continue; } // Calculate the bottom and right side locations for the gameObject float objBottom = gameObject.Y + gameObject.Rect.Height; float objRight = gameObject.X + gameObject.Rect.Width; // Check distances between the sides of the objects. float tDistance = objBottom - entTop; float bDistance = entBottom - gameObject.Y; float rDistance = entRight - gameObject.X; float lDistance = objRight - entLeft; // Whichever side is closest is the side they are colliding on. if (physEntity.Rect.Intersects(gameObject.Rect)) { if ((gameObject is HoppingEnemy || gameObject is Ooze) && (physEntity is HoppingEnemy || physEntity is Ooze)) { continue; } gameObject.OnCollide(physEntity); physEntity.OnCollide(gameObject); if (gameObject is Door)//stops nast problems from happening during room spawn, should be changed in case of like locked doors or something { continue; } if (!gameObject.IsCollidable) { continue; } if (gameObject == physEntity) { continue; } if ((gameObject is Player && physEntity is Button) || (gameObject is Button && physEntity is Player)) { continue; } //below, sets collide array and creates new collision object //TOP if (tDistance < bDistance && tDistance < lDistance && tDistance < rDistance) { physEntity.colliderArray[0] = true; if (physEntity.Y != physEntity.PrevY && newCollision) { collisions.Add(new Collision(physEntity, gameObject, CollisionSide.top)); } } //RIGHT else if (rDistance < bDistance && rDistance < lDistance && rDistance < tDistance) { if ((gameObject is Player && physEntity is Ooze) || (gameObject is Ooze && physEntity is Player)) { } else { physEntity.colliderArray[1] = true; if (physEntity.X != physEntity.PrevX && newCollision) { collisions.Add(new Collision(physEntity, gameObject, CollisionSide.right)); } } } //BOTTOM else if (bDistance < tDistance && bDistance < lDistance && bDistance < rDistance) { physEntity.colliderArray[2] = true; if (physEntity.Y != physEntity.PrevY && newCollision) { collisions.Add(new Collision(physEntity, gameObject, CollisionSide.bottom)); } } //LEFT else if (lDistance < bDistance && lDistance < tDistance && lDistance < rDistance) { if ((gameObject is Player && physEntity is Ooze) || (gameObject is Ooze && physEntity is Player)) { } else { physEntity.colliderArray[3] = true; if (physEntity.X != physEntity.PrevX && newCollision) { collisions.Add(new Collision(physEntity, gameObject, CollisionSide.left)); } } } } } } } }