public override bool CheckCollisionWith(GameTime gameTime, GDComp other) { //Early out if we have another ScoreBall. if (other is ScoreBall) { return false; } else if (other is PlayerBall) { if (CircleCircleCollision(other)) { PlayerHit(other as PlayerBall); return false; } return false; } else if (other is PowerBall) { if (!Remove && ScoreState == State.Enemy && CircleCircleCollision(other)) { Spawner.GetInstance().RemoveBall(this); } return false; } else { return other.CheckCollisionWith(gameTime, this); } }
public override bool CheckCollisionWith(GameTime gameTime, GDComp other) { //Ignore all walls and powerballs. if (other is Wall || other is PowerBall || other is ShieldBall) { return false; } //Is there actually a collision? if (!base.CheckCollisionWith(gameTime, other)) { return false; } //From here on it can only be a ball. But just to be sure, check it. if (other.Shape == Components.Shape.Circle) { CircleBoxCollisionHandling(gameTime, other as Ball); } //This only triggers handling so just return false. return false; }
/// <summary> /// Insert a component in the tree. /// </summary> /// <param name="comp"></param> public void Insert(GDComp comp) { //Get the rectangle to work with. Rectangle rect = comp.GetRect(); //Do we have any child node? if (nodes[0] != null) { int index = GetIndex(rect); //Does it fit completely in a child node? if (index != -1) { //Insert it in a child node and return afterwards. nodes[index].Insert(comp); return; } } //Object doesnt fit in a child, add it to this leaf. leaves.Add(comp); //Do we need to split? if (nodes[0] == null && level < MaxLevel && leaves.Count > MaxLeaves) { Split(); int i = 0; while (i < leaves.Count) { Rectangle tempRect = leaves[i].GetRect(); int index = GetIndex(tempRect); //Does the object fit in a child node? if (index != -1) { //Replace the object to the child node. nodes[index].Insert(leaves[i]); leaves.RemoveAt(i); } else { i++; } } } }
public virtual bool CheckCollisionWith(GameTime gameTime, GDComp other) { //Is either shape collidable? if (Shape == Shape.None || other.Shape == Shape.None) { return false; } //Is this a circle? if (Shape == Shape.Circle) { //Is the other a circle? if (other.Shape == Shape.Circle) { return CircleCircleCollision(other); } //Is the other a box? else { return CircleBoxCollision(other); } } //Is this a box? else { //Is the other a circle? if (other.Shape == Shape.Circle) { return other.CircleBoxCollision(this); } //Is the other a box? else { return BoxBoxCollision(other); } } }
/// <summary> /// Check if 2 box shaped objects collide. /// </summary> /// <param name="other">The other box.</param> private bool BoxBoxCollision(GDComp other) { bool left, right, up, down; //Simple collision check using the position and halfsizes. left = position.X - halfSize.X < other.Position.X + other.HalfSize.X; right = position.X + halfSize.X > other.Position.X - other.HalfSize.X; up = position.Y - halfSize.Y < other.Position.Y + other.HalfSize.Y; down = position.Y + halfSize.Y > other.Position.Y - other.HalfSize.Y; return left && right && up && down; }
/// <summary> /// Checks if 2 circles collide or not. /// </summary> /// <param name="other">The other circle.</param> protected bool CircleCircleCollision(GDComp other) { //Simple collision check using both the circles' radius. float distSQ = (position - other.Position).LengthSquared(); float radiiSQ = (HalfSize.X + other.HalfSize.X) * (HalfSize.X + other.HalfSize.X); return distSQ < radiiSQ; }
/// <summary> /// Checks if a circle and box collide. /// This function assumes that this is the circle and the other a the box. /// </summary> /// <param name="other">The box.</param> public bool CircleBoxCollision(GDComp other) { //Calculate the distance between origins. Vector2 circleDist = new Vector2(); circleDist.X = Math.Abs(position.X - other.Position.X); circleDist.Y = Math.Abs(position.Y - other.Position.Y); //Are the 2 objects close to eachother? if (circleDist.X > (other.HalfSize.X + HalfSize.X) || circleDist.Y > (other.HalfSize.Y + HalfSize.X)) { return false; } //Is the circle in the box? if (circleDist.X <= other.HalfSize.X || circleDist.Y <= other.HalfSize.Y) { return true; } //Now use Pythagoras to check if the distance from the box is smaller //than the circle's radius. float cornerDistSQ = (circleDist - other.HalfSize).LengthSquared(); return cornerDistSQ <= HalfSize.X * HalfSize.X; }
public override bool CheckCollisionWith(GameTime gameTime, GDComp other) { other.CheckCollisionWith(gameTime, this); return false; }
public override bool CheckCollisionWith(GameTime gameTime, GDComp other) { //Respawn if we spawned inside a wall. if (justSpawned && other is Wall && CircleBoxCollision(other)) { Respawn(); } //Kill all the enemies (if the player is inside). if (!(GDGame.GetActiveScreen() is GameplayScreen)) { return false; } PlayerBall player = ((GameplayScreen)GDGame.GetActiveScreen()).Player; if (other is ScoreBall && CircleCircleCollision(other) && CircleCircleCollision(player)) { Spawner.GetInstance().RemoveBall(other as ScoreBall); player.AddScore(); } //Is the player in the shield? if (other is PlayerBall && CircleCircleCollision(other)) { const float growthPerSecond = (MaxSize - MinSize) * 0.5f; float dt = (float)gameTime.ElapsedGameTime.TotalSeconds; //Is the player moving? if (other.Velocity.LengthSquared() > 0) { //Time to grow (if possible). if (Scale < MaxSize) { //How much do we grow? float growth = growthPerSecond * dt; //Now grow. Scale = Scale + growth < MaxSize ? Scale + growth : MaxSize; } //Now time to exhaust the shield. aliveCounter += dt; if (aliveCounter >= TimeAlive) { Respawn(); } } else { //Time to shrink (if possible). if (Scale > MinSize) { //How much do we shrink? float shrink = growthPerSecond * dt * 0.25f; //Now shrink. Scale = Scale - shrink > MinSize ? Scale - shrink : MinSize; } } } return false; }