Calculate a circular wrapper?

I just started learning iphone development and after a friend recommended the Corona SDK for ease of use, I finally decided to give it a try.

Now I just started to learn how to use the accelerometer and draw shapes and make them move by tilting the device. So I thought that I could make a level tool as my first application, and everything works for me when I lean, but now I decided to try to make a level level, but I can’t figure out how to make the bubble stay inside the circle.

Here's how I restrict the bubble from moving out of the bottle horizontally:

function bubbleBounds()
    -- left side
    if bubble.x < (_W/2 - vial.width/2 + bubble.width/2) then 
       bubble.x = (_W/2 - vial.width/2 + bubble.width/2)
    end

            -- right side
    if bubble.x > (_W/2 + vial.width/2 - bubble.width/2) then
       bubble.x = (_W/2 + vial.width/2 - bubble.width/2)
    end
    end

, 2, 256 , - 64 . , ?


@Mac, , 2 , ?

@Tim C;

, , :

local bubbleRadius = 32
local circleRadius = 128
local sqrt = math.sqrt
local centerX = display.contentWidth/2;
local centerY = display.contentHeight/2;

local function bubbleBounds()
       Length = sqrt(centerX * centerX + centerY * centerY)
       normalizedX = centerX/Length;
       normalizedY = centerY/Length;

       limitedX = normalizedX * circleRadius;
       limitedY = normalizedY * circleRadius;

       if bubble.x < centerX - limitedX + bubbleRadius then
          bubble.x = centerX - limitedX + bubbleRadius
       end

       if bubble.x > centerX + limitedX - bubbleRadius then
          bubble.x = centerX + limitedX - bubbleRadius
       end

       if bubble.y < centerY - limitedY + bubbleRadius then
          bubble.y = centerY - limitedY + bubbleRadius
       end

       if bubble.y > centerY + limitedY - bubbleRadius then
          bubble.y = centerY + limitedY - bubbleRadius
       end

 end
 Runtime:addEventListener("enterFrame", bubbleBounds)

, , , . :

    local acc = {}

function acc:accelerometer(event)
        bubble.x = centerX - (centerX * event.yGravity * 2);

    bubble.y = centerY - (centerY * event.xGravity * 2);
    end
    Runtime:addEventListener("accelerometer", acc)

?

+3
1

, .

, , , . , , .

, 0,0, x, y.

:

Length = sqrt(x*x + y*y);  //pythagorean theorem

normalizedX = x/Length;
normalizedY = y/Length;

limitedX = normalizedX * circleRadius;
limitedY = normalizedY * circleRadius;

, , , .


, .

, , .
-, - . , , .

- :

local bubbleRadius = 32;
local circleRadius = 128;
local sqrt = math.sqrt;
local centerX = display.contentWidth/2;
local centerY = display.contentHeight/2;

local function bubbleBounds()

   bubbleX = bubble.x - centerX;
   bubbleY = bubble.y - centerY;

   Length = sqrt(bubbleX * bubbleX + bubbleY * bubbleY);

   normalizedX = bubbleX/Length;
   normalizedY = bubbleY/Length;

   if Length > circleRadius then
       bubbleX = normalizedX * circleRadius;
       bubbleY = normalizedY * circleRadius;
       bubble.x = bubbleX + centerX;
       bubble.y = bubbleY + centerY;
   end
end
+1

All Articles