// check ball2ball and ball2ship collisions public void OnCollision(Gamelet sender, GameletEventArgs e) { /* if (e.otherItem is Ball && Parent is Ball) { Ball parentBall = Parent as Ball; Ball collidingBall = e.otherItem as Ball; if (parentBall.nrgCharge != null) { isEndOfLifeMode = true; Charged = false; } } */ if (e.otherItem is Ship && !isEndOfLifeMode ) { Ship ship = e.otherItem as Ship; // uncouple from my parent (whatever it was) LinkedToParent = false; Position = Parent.PositionAbsolute; // reduce velocity Velocity = new Vector2(Parent.Velocity.X * 0.75f, Parent.Velocity.Y * 0.75f); isEndOfLifeMode = true; Charged = false; // score ship.player.Score += 100.0f; // energy increaes ship.player.NRG += 2.0f; // TODO } }
/** * called upon collision event of my associated NRGCharge (or Ball or ... other) */ public void OnBallCollided(Gamelet item, GameletEventArgs e) { if (e.otherItem is Ship) { // switch off my dimming behaviour. this.Active = false; } }
public void OnCollision(Gamelet item, GameletEventArgs e) { if (!(e.otherItem is Ship)) return; Ship ship = e.otherItem as Ship; Ball ball = Parent as Ball; Vector2 ballPos = ball.PositionAbsolute; Vector2 shipPos = ship.PositionAbsolute; Vector2 localNormal; float h2, r; // never let ball bounce back twice in the same period of time. if (ball.Velocity.X > 0) return; if (ballPos.X < (shipPos.X)) // if already left-of ship return; r = ship.WidthAbsolute / 2; h2 = ship.HeightAbsolute / 2 - r; // check where ball is: up, down or middle zone of ship if (ballPos.Y < shipPos.Y - h2) { // up zone Vector2 p = new Vector2(shipPos.X, shipPos.Y - h2); localNormal = ballPos - p; localNormal.Normalize(); } else if (ballPos.Y > shipPos.Y + h2) { // down zone Vector2 p = new Vector2(shipPos.X, shipPos.Y + h2); localNormal = ballPos - p; localNormal.Normalize(); } else { // middle zone ball.Velocity = new Vector2(-ball.Velocity.X, ball.Velocity.Y); return; } // reduce mass after refl ball.Mass /= 10.0f; //Vector2 newItemVelocity; //Vector2 itemVelocity = item.Velocity; //Vector2.Reflect(ref itemVelocity, ref localNormal, out newItemVelocity); ball.Velocity = localNormal * ball.Velocity.Length(); //new Vector2(-newItemVelocity.X, newItemVelocity.Y); }
public void OnCollision(Gamelet item, GameletEventArgs e) { if (!(e.otherItem is Ball)) return; Ship ship = Parent as Ship; Ball ball = e.otherItem as Ball; // impact vector of Ball Vector2 v = ball.Velocity; v.Y = -v.Y; // calc rotation float dy = (ball.PositionAbsolute.Y - ship.PositionAbsolute.Y); float rotAmpl = ball.Mass * dy / 1.5f; ship.AddFront(new LashbackBehavior(ball.Mass * v / 30.0f, rotAmpl)); //ball.Mass /= 10.0f; }
public void OnCollision(Gamelet item, GameletEventArgs e) { Gamelet withItem = e.otherItem; // TODO reset the lastCollide when not-colliding if (lastCollidedWith != withItem) { if (withItem is Ball) { Ball ball = withItem as Ball; // swap the velocity vecs - pseudo-phyics eff Vector2 v = ball.Velocity; ball.Velocity = item.Velocity; item.Velocity = v; } lastCollidedWith = withItem; } }