Video Game Controllers

Bingo! That solved it. Thank you @jlslate. I didnt even know i was allowed to write something outside a function…

It seems like with the number of gotcha’s that occur with this that TLL could maybe check of it these statements are in a function, and error if they are. Maybe you could put in a bug/feature request for it?

Ok. I tried to go to this link in the FAQ to file the bug, https://bitbucket.org/TwoLivesLeft/codea/issues, but is dead. Where sshould i file the bug?

The link at the top of the forum is right, Bitbucket recently upgraded itself and loads of links changed.

Obligatory link: http://www.w3.org/Provider/Style/URI.html

Thanks Andrew. Issue #212 .

I’ve updated the link in the FAQ now.

can someone tell me what math funcion use to get direction from stick controller?

Hello @Cabernet. Look at the definition of the virtualStick:

    controller1 = VirtualStick {
        moved = function(v) steer = v end,
        released = function(v) steer = vec2(0,0) end
    }

this means that when you will move your stick, the global variable “steer” will be updated with “v”. Here “v” is a vect2 about equal to the distance between your starting touch point and the current position of your touch. So the “direction” you want is now in “steer”. You can read the coordinates of steer by steer.x and steer.y (See vec2 definition). There is no “math” function involved. Did that help?

it is more complicated in my case becouse I dont have original version of this code and I have 2 controllers for one player but finally I did it. Thank you for good explanation.

To clarify what Jmv38 wrote:

The VirtualStick controller is a virtual analog stick. The vector passed to the moved callback is scaled between a magnitude of zero when the stick is in the dead zone, and a magnitude of 1 when the stick is at the maximum radius.

To get a movement vector for your sprite, multiply the vector passed to the callback with the maximum speed of your sprite.

If you want only to get the direction (and don’t care about the speed), normalise the vector passed to the callback. That will give you a unit vector pointing in the direction of movement.

If you only want a direction angle, you can obtain than from the x and y coordinates of the vector using trigonometry.

Thank you so much Nat. Your post was very helpfull for me…

Hi @Nat, any chance to have a small Catapult example? Thanks in advance.

Hello @Nat. I tried to use your controllers for my ‘game’ but i dicovered 1 contoller was eating 8 fps, so i changed them to use sprites then i loose no fps. Also i wanted to use your contollers for my experiments because they are so much better that iparameter() (the starting touch doesn’t have to be as accurate with your’s, that’s what i like, and the 2d also). So i modified the init function to be able to define a smaller zone of activation, to put many of them on the screen. But then i needed to see where is the zone exactly, so i added an initial show (10s) of where is each zone, and write a small title to remember where is the control. I kind of like this new version, let me know what you think. You can find it there:
http://db.tt/Xh9yz7eD

Can anybody help me with 8 ways controller? I am not good at math :frowning: (english too, sorry).

I have a stick whitch return position in vec2 and I need to know borders between directions. For example if -0.25<x<0.25 and y>0.75 then direction is 1.Thanks

Hi @cabernet. Pass your vec2 tothis function:

function dir(v)
    local angle = math.deg(vec2(1,0):angleBetween(v))
    local txt
    if -45/2 < angle and angle <= 45/2 then txt = "right"
    elseif 45/2 < angle and angle <= 45*3/2 then txt = "top-right"
    elseif 45*3/2 < angle and angle <= 45*5/2 then txt = "top"
    elseif 45*5/2 < angle and angle <= 45*7/2 then txt = "top-left"
    elseif 45*7/2 < angle or angle <= -45*7/2 then txt = "left"
    elseif -45*3/2 < angle and angle <= -45*1/2 then txt = "bot-right"
    elseif -45*5/2 < angle and angle <= -45*3/2 then txt = "bot"
    elseif -45*7/2 < angle and angle <= -45*5/2 then txt = "bot-left"
    end
    return txt
end

Thank you so much, works great @Jmv38!

How do I deactivate Nat’s controllers if I want to return to menu? There are not drawing but still functional. Thanks

I dont remember if there is an ‘activated’ field. If not, add 1 boolean and check if true before all:touch() and all:draw()

Te question seems unanswered, I have the same issue: once the controllers override touched(), how do you get it back? Right now,once I use a controller,I have to use controls everywhere on each screen for the app to operate. I wanted to basically turn them off, but was unable to…

Are you guys using the original Nat version or the one I posted (deeply modified)?