public static Paddle paddleWithTexture(CCTexture2D aTexture) { Paddle pPaddle = new Paddle(); pPaddle.initWithTexture(aTexture); //pPaddle->autorelease(); return pPaddle; }
public void collideWithPaddle(Paddle paddle) { CCRect paddleRect = paddle.rect(); paddleRect.Origin.X += paddle.Position.X; paddleRect.Origin.Y += paddle.Position.Y; float lowY = CCRect.CCRectGetMinY(paddleRect); float midY = CCRect.CCRectGetMidY(paddleRect); float highY = CCRect.CCRectGetMaxY(paddleRect); float leftX = CCRect.CCRectGetMinX(paddleRect); float rightX = CCRect.CCRectGetMaxX(paddleRect); if (Position.X > leftX && Position.X < rightX) { bool hit = false; float angleOffset = 0.0f; if (Position.Y > midY && Position.Y <= highY + radius()) { Position = new CCPoint(Position.X, highY + radius()); hit = true; angleOffset = (float)Math.PI / 2; } else if (Position.Y < midY && Position.Y >= lowY - radius()) { Position = new CCPoint(Position.X, lowY - radius()); hit = true; angleOffset = -(float)Math.PI / 2; } if (hit) { float hitAngle = (float)Math.Atan2(new CCPoint(paddle.Position.X - Position.X, paddle.Position.Y - Position.Y).Y, new CCPoint(paddle.Position.X - Position.X, paddle.Position.Y - Position.Y).X) + angleOffset; float scalarVelocity = (float)Math.Sqrt((double)(m_velocity.X * m_velocity.X + m_velocity.Y * m_velocity.Y)) * 1.05f; float velocityAngle = -(float)Math.Atan2(m_velocity.Y, m_velocity.X) + 0.5f * hitAngle; m_velocity = new CCPoint(new CCPoint((float)Math.Cos(velocityAngle), (float)Math.Sin(velocityAngle)).X * scalarVelocity, new CCPoint((float)Math.Cos(velocityAngle), (float)Math.Sin(velocityAngle)).Y * scalarVelocity); } } }
public void collideWithPaddle(Paddle paddle) { CCRect paddleRect = paddle.rect(); paddleRect.origin.x += paddle.position.x; paddleRect.origin.y += paddle.position.y; float lowY = CCRect.CCRectGetMinY(paddleRect); float midY = CCRect.CCRectGetMidY(paddleRect); float highY = CCRect.CCRectGetMaxY(paddleRect); float leftX = CCRect.CCRectGetMinX(paddleRect); float rightX = CCRect.CCRectGetMaxX(paddleRect); if (position.x > leftX && position.x < rightX) { bool hit = false; float angleOffset = 0.0f; if (position.y > midY && position.y <= highY + radius()) { position = new CCPoint(position.x, highY + radius()); hit = true; angleOffset = (float)Math.PI / 2; } else if (position.y < midY && position.y >= lowY - radius()) { position = new CCPoint(position.x, lowY - radius()); hit = true; angleOffset = -(float)Math.PI / 2; } if (hit) { float hitAngle = (float)Math.Atan2(new CCPoint(paddle.position.x - position.x, paddle.position.y - position.y).y, new CCPoint(paddle.position.x - position.x, paddle.position.y - position.y).x) + angleOffset; float scalarVelocity = (float)Math.Sqrt((double)(m_velocity.x * m_velocity.x + m_velocity.y * m_velocity.y)) * 1.05f; float velocityAngle = -(float)Math.Atan2(m_velocity.y, m_velocity.x) + 0.5f * hitAngle; m_velocity = new CCPoint(new CCPoint((float)Math.Cos(velocityAngle), (float)Math.Sin(velocityAngle)).x * scalarVelocity, new CCPoint((float)Math.Cos(velocityAngle), (float)Math.Sin(velocityAngle)).y * scalarVelocity); } } }
public void collideWithPaddle(Paddle paddle) { CCRect paddleRect = paddle.rect(); paddleRect.Origin.X += paddle.Position.X; paddleRect.Origin.Y += paddle.Position.Y; float lowY = paddleRect.MinY; float midY = paddleRect.MidY; float highY = paddleRect.MaxY; float leftX = paddleRect.MinX; float rightX = paddleRect.MaxX; if (Position.X > leftX && Position.X < rightX) { bool hit = false; float angleOffset = 0.0f; if (Position.Y > midY && Position.Y <= highY + radius()) { Position = new CCPoint(Position.X, highY + radius()); hit = true; angleOffset = (float)Math.PI / 2; } else if (Position.Y < midY && Position.Y >= lowY - radius()) { Position = new CCPoint(Position.X, lowY - radius()); hit = true; angleOffset = -(float)Math.PI / 2; } if (hit) { float hitAngle = (paddle.Position - Position).Angle + angleOffset; float scalarVelocity = m_velocity.Length * 1.05f; float velocityAngle = -m_velocity.Angle + 0.5f * hitAngle; m_velocity = CCPoint.ForAngle(velocityAngle) * scalarVelocity; } } }
public PongLayer() { m_ballStartingVelocity = new CCPoint(20.0f, -100.0f); m_ball = Ball.ballWithTexture(CCTextureCache.SharedTextureCache.AddImage(s_Ball)); m_ball.Position = new CCPoint(160.0f, 240.0f); m_ball.Velocity = m_ballStartingVelocity; AddChild(m_ball); CCTexture2D paddleTexture = CCTextureCache.SharedTextureCache.AddImage(s_Paddle); List<object> paddlesM = new List<object>(4); Paddle paddle = new Paddle(paddleTexture); paddle.Position = new CCPoint(160, 15); paddlesM.Add(paddle); paddle = new Paddle(paddleTexture); paddle.Position = new CCPoint(160, 480 - 20f - 15); paddlesM.Add(paddle); paddle = new Paddle(paddleTexture); paddle.Position = new CCPoint(160, 100); paddlesM.Add(paddle); paddle = new Paddle(paddleTexture); paddle.Position = new CCPoint(160, 480 - 20.0f - 100); paddlesM.Add(paddle); m_paddles = paddlesM; for (int i = 0; i < m_paddles.Count; i++) { paddle = (Paddle)m_paddles[i]; if (paddle == null) break; AddChild(paddle); } Schedule(this.doStep); }
public PongLayer() { m_ballStartingVelocity = new CCPoint(20.0f, -100.0f); m_ball = Ball.ballWithTexture(CCTextureCache.SharedTextureCache.AddImage(s_Ball)); m_ball.Velocity = m_ballStartingVelocity; AddChild(m_ball); CCTexture2D paddleTexture = CCTextureCache.SharedTextureCache.AddImage(s_Paddle); var paddlesM = new List<Paddle>(4); Paddle paddle = new Paddle(paddleTexture); paddlesM.Add(paddle); paddle = new Paddle(paddleTexture); paddlesM.Add(paddle); paddle = new Paddle(paddleTexture); paddlesM.Add(paddle); paddle = new Paddle(paddleTexture); paddlesM.Add(paddle); paddles = paddlesM; for (int i = 0; i < paddles.Count; i++) { paddle = paddles[i]; if (paddle == null) break; AddChild(paddle); } Schedule(this.doStep); }