public virtual void UpdateVelocity(CCPoint point) { // Calculate distance and angle from the center. float dx = point.X - PositionX; float dy = point.Y - PositionY; dycache = dy; float dSq = dx * dx + dy * dy; if (dSq <= DeadRadiusSq) { Velocity = CCPoint.Zero; Degrees = 0.0f; StickPosition = Center; return; } float angle = (float)Math.Atan2(dy, dx); // in radians if (angle < 0) { angle += CCMathHelper.TwoPi; } float cosAngle; float sinAngle; if (isDPad) { float anglePerSector = 360.0f / CCMathHelper.ToRadians(NumberOfDirections); // NumberOfDirections * ((float)Math.PI / 180.0f); angle = (float)Math.Round(angle / anglePerSector) * anglePerSector; } cosAngle = CCMathHelper.Cos(angle); sinAngle = CCMathHelper.Sin(angle); // NOTE: Velocity goes from -1.0 to 1.0. if (dSq > JoystickRadiusSq || isDPad) { dx = cosAngle * joystickRadius; dy = sinAngle * joystickRadius; } Velocity = new CCPoint(dx / joystickRadius, dy / joystickRadius); Degrees = CCMathHelper.ToDegrees(angle); // Update the thumb's position var newLoc = new CCPoint(dx + ContentSize.Width / 2, dy + ContentSize.Height / 2); StickPosition = newLoc; }