Issue with my wheel

Hi Everybody,

I’m a beginner with LUA and i’m having some issue with my spinning wheel, when i touch the wheel it follows my finger (correct) but when i leave the screen it stops (not correct) i would like that the wheel keep spinning and slow down stops here is my part of code:

[lua] local wheel = display.newImage(“wheel1.png”, true)

wheel.x = display.contentWidth - 195

wheel.y = display.contentHeight - 268

physics.addBody(wheel,“dynamic”,{bounce=0, friction=0.2, radius=1})

wheel.angularDamping = 0.2

local skull = display.newImage(“skull1.png”, true)

skull.x = display.contentWidth - 190

skull.y = display.contentHeight - 379

local function gameLoop()

    angle = wheel.rotation

    if wheel.angularVelocity == 0 and angle ~= startingAngle then

        --print(angle % 360)

        local wedge = math.floor(((angle + 45) % 360) /45) + 1

        --print(wedge … " " … wedgeName[wedge])

        startingAngle = angle

        if movementEnded then timer.performWithDelay(1000,playRound); end

    end

end

local function spinObject(event)

    local t = event.target

    local phase = event.phase

    --print("Phase: " … phase)

    if (phase == “began”) then

            display.getCurrentStage():setFocus( t )

            t.isFocus = true

            – Store initial position of finger

            t.x1 = event.x

            t.y1 = event.y

    elseif t.isFocus then

            if (phase == “moved”) then

                    t.x2 = event.x

                    t.y2 = event.y

                    local angle1 = 180/math.pi * math.atan2(t.y1 - t.y , t.x1 - t.x)

                    local angle2 = 180/math.pi * math.atan2(t.y2 - t.y , t.x2 - t.x)

                    local rotationAmt = angle1 - angle2

                    t.rotation = t.rotation - rotationAmt

                    t.x1 = t.x2

                    t.y1 = t.y2

                    

            elseif (phase == “ended”) or (phase == “cancelled”) then

                display.getCurrentStage():setFocus( nil )

                t.isFocus = false

            end

    end

    

    – Stop further propagation of touch event

    return true

end

wheel:addEventListener(“touch”, spinObject)

Runtime:addEventListener(“enterFrame”, gameLoop)

    physics.start() [/lua]

Thanks for the Help! 

If this is the only code in your program, can you please define what “leave the screen” means?

If this is not the only code in your program, are you referring to when a different scene is loaded? In that case, it could be that the physics bodies are being removed and when you return to the scene the bodies are recreated and therefore not moving.

Hi Horace,
For “leave the screen” i mean when i remove my finger from the screen.

I am assuming from this statement:

i would like that the wheel keep spinning and slow down stops

That you would like to spin the wheel with a touch on the screen and when you let go of the wheel that the wheel keeps spinning, slows down and eventually stops due to something like friction. Is that correct?

If I’m right, the code you’re using to spin the wheel is wrong. Generally, you’re code is ok, but you’re trying to use the display object property ‘rotation’ to rotate the wheel. You shouldn’t do this on physics bodies because it is considered fighting the physics engine and things will either not work (the way you expect) or go horribly wrong.

In this case, you’re not giving the wheel any inertia because you’re not moving the body. You need to use the physics engine to tell it that the user is touching the wheel. You do this with a touch joint when the user begins their screen touch, move it when they move their touch and remove it when they let go of the screen. (I wouldn’t use the term ‘leave’ the screen, but that’s me.)

You have the angularDamping value set correctly, to slow the wheel.

If you want to fix the wheel in place and have it rotate around a central point you should place a static object first and create a pivot joint to join the wheel to it. This will stop the wheel from getting dragged around the screen when the user touches it.

Let me know how you get on and if you’re having trouble I can post some code, but you’re so close I don’t think you’ll need it. You can just remove the maths code in your sample and replace it with the touch joint code.

Horace what can i say…

Thanks man! you gave me the right way for the solution, now the wheel spins with the “touch joint” and keep spinning (i’ve also done an anchor object).

Now i would like to focus the touch event only on the wheel and not on the other objects like the background.

Is it possible?

Thanks a lot!

Hi @kkaaoss,

Well, I won’t argue with the end results, but I still don’t understand why you need to use a touch joint and an “anchor”. If you have zero gravity in the physics simulation, and no other linear forces on the wheel, there’s no reason why the wheel should move off its central axis, even when you apply an angular/radial force to it.

But regardless, for the “touch the wheel only” part, I would suggest that you create a vector circle that’s the same size as the wheel image, and place it directly in front. Then, make it invisible (alpha of 0, or .isVisible = false), but also make it hit-testable (.isHitTestable = true). Then, instead of detecting the touch on the wheel image, detect it on the vector circle. Corona will then only detect touches on that object (the vector), but you can apply the result of the touch to the wheel image. Basically, the vector circle is used only to detect the touch, but you’re “transferring” those results to the wheel image instead.

Brent

Hi Brent,

i got the same result with the vector instead of my wheel, i’m not a master in coding with corona maybe i made some basic mistake.

Another thing when i push the “back” botton (returns to “menu”) in my game and then turn back in game it shows me an error.

Hi @kkaaoss,

The issue related to your “back” button is separate to this “wheel” issue. If you want to seek help on the button, please begin a new forum topic so that it doesn’t get mixed up with this one.

Thanks,

Brent

If this is the only code in your program, can you please define what “leave the screen” means?

If this is not the only code in your program, are you referring to when a different scene is loaded? In that case, it could be that the physics bodies are being removed and when you return to the scene the bodies are recreated and therefore not moving.

Hi Horace,
For “leave the screen” i mean when i remove my finger from the screen.

I am assuming from this statement:

i would like that the wheel keep spinning and slow down stops

That you would like to spin the wheel with a touch on the screen and when you let go of the wheel that the wheel keeps spinning, slows down and eventually stops due to something like friction. Is that correct?

If I’m right, the code you’re using to spin the wheel is wrong. Generally, you’re code is ok, but you’re trying to use the display object property ‘rotation’ to rotate the wheel. You shouldn’t do this on physics bodies because it is considered fighting the physics engine and things will either not work (the way you expect) or go horribly wrong.

In this case, you’re not giving the wheel any inertia because you’re not moving the body. You need to use the physics engine to tell it that the user is touching the wheel. You do this with a touch joint when the user begins their screen touch, move it when they move their touch and remove it when they let go of the screen. (I wouldn’t use the term ‘leave’ the screen, but that’s me.)

You have the angularDamping value set correctly, to slow the wheel.

If you want to fix the wheel in place and have it rotate around a central point you should place a static object first and create a pivot joint to join the wheel to it. This will stop the wheel from getting dragged around the screen when the user touches it.

Let me know how you get on and if you’re having trouble I can post some code, but you’re so close I don’t think you’ll need it. You can just remove the maths code in your sample and replace it with the touch joint code.

Horace what can i say…

Thanks man! you gave me the right way for the solution, now the wheel spins with the “touch joint” and keep spinning (i’ve also done an anchor object).

Now i would like to focus the touch event only on the wheel and not on the other objects like the background.

Is it possible?

Thanks a lot!

Hi @kkaaoss,

Well, I won’t argue with the end results, but I still don’t understand why you need to use a touch joint and an “anchor”. If you have zero gravity in the physics simulation, and no other linear forces on the wheel, there’s no reason why the wheel should move off its central axis, even when you apply an angular/radial force to it.

But regardless, for the “touch the wheel only” part, I would suggest that you create a vector circle that’s the same size as the wheel image, and place it directly in front. Then, make it invisible (alpha of 0, or .isVisible = false), but also make it hit-testable (.isHitTestable = true). Then, instead of detecting the touch on the wheel image, detect it on the vector circle. Corona will then only detect touches on that object (the vector), but you can apply the result of the touch to the wheel image. Basically, the vector circle is used only to detect the touch, but you’re “transferring” those results to the wheel image instead.

Brent

Hi Brent,

i got the same result with the vector instead of my wheel, i’m not a master in coding with corona maybe i made some basic mistake.

Another thing when i push the “back” botton (returns to “menu”) in my game and then turn back in game it shows me an error.

Hi @kkaaoss,

The issue related to your “back” button is separate to this “wheel” issue. If you want to seek help on the button, please begin a new forum topic so that it doesn’t get mixed up with this one.

Thanks,

Brent