Skip to content

Instantly share code, notes, and snippets.

@robmiracle
robmiracle / part7.lua
Created August 6, 2015 02:24
Code snippets for the Corona SDK Tutorial: Game controllers and axes (https://coronalabs.com/blog/2013/09/24/tutorial-controllers-and-axis)
local function onInputDeviceStatusChanged( event )
-- Handle the input device change
if ( event.connectionStateChanged ) then
print( event.device.displayName .. ": " .. event.device.connectionState, event.device.descriptor, event.device.type, event.device.canVibrate )
end
end
Runtime:addEventListener( "inputDeviceStatus", onInputDeviceStatusChanged )
@robmiracle
robmiracle / part6.lua
Created August 6, 2015 02:23
Code snippets for the Corona SDK Tutorial: Game controllers and axes (https://coronalabs.com/blog/2013/09/24/tutorial-controllers-and-axis)
-- Fetch all input devices currently connected to the system
local inputDevices = system.getInputDevices()
-- Traverse all input devices
for deviceIndex = 1, #inputDevices do
-- Fetch the input device's axes
print( deviceIndex, "andoridDeviceid", inputDevices[deviceIndex].androidDeviceId )
print( deviceIndex, "canVibrate", inputDevices[deviceIndex].canVibrate )
print( deviceIndex, "connectionState", inputDevices[deviceIndex].connectionState )
@robmiracle
robmiracle / part5.lua
Created August 6, 2015 02:21
Code snippets for the Corona SDK Tutorial: Game controllers and axes (https://coronalabs.com/blog/2013/09/24/tutorial-controllers-and-axis)
local function onAxisEvent( event )
-- Display some info on the screen about this axis event
local message = "Axis '" .. event.axis.descriptor .. "' was moved " .. tostring( event.normalizedValue )
myAxisDisplayText.text = message
-- Map event data to simple variables
local abs = math.abs
local controller = event.device.descriptor
local thisAxis = event.axis.number
@robmiracle
robmiracle / part4.lua
Created August 6, 2015 02:20
Code snippets for the Corona SDK Tutorial: Game controllers and axes (https://coronalabs.com/blog/2013/09/24/tutorial-controllers-and-axis)
-- Since controllers don't generate constant values, but simply events when
-- the values change, we need to set a movement amount when the event happens,
-- and also have the game loop continuously apply it
-- We can also calculate our rotation angle here
local function moveRedPlayer()
-- Set the .isMovingX and .isMovingY values in our event handler
-- If this number isn't 0 (stopped moving), move the player
@robmiracle
robmiracle / part3.lua
Created August 6, 2015 02:18
Code snippets for the Corona SDK Tutorial: Game controllers and axes (https://coronalabs.com/blog/2013/09/24/tutorial-controllers-and-axis)
-- Calculate the angle to rotate the square. Using simple right angle math, we can
-- determine the base and height of a right triangle where one point is 0,0
-- (stick center) and the values returned from the two axis numbers returned
-- from the stick
-- This will give us a 0-90 value, so we have to map it to the quadrant
-- based on if the values for the two axis are positive or negative
-- Negative Y, positive X is top-right area
-- Positive X, Positive Y is bottom-right area
-- Negative X, positive Y is bottom-left area
@robmiracle
robmiracle / part2.lua
Created August 6, 2015 02:11
Code snippets for the Corona SDK Tutorial: Game controllers and axes (https://coronalabs.com/blog/2013/09/24/tutorial-controllers-and-axis)
local redPlayer = display.newRect( display.contentCenterX-15, display.contentCenterY-15, 30, 30 )
redPlayer:setFillColor( 1, 0, 0 )
redPlayer.x = display.contentCenterX
redPlayer.y = display.contentCenterY
redPlayer.isMovingX = 0
redPlayer.isMovingY = 0
redPlayer.isRotatingX = 0
redPlayer.isRotatingY = 0
redPlayer.thisAngle = 0
redPlayer.lastAngle = 0
@robmiracle
robmiracle / part1.lua
Created August 6, 2015 02:07
Code snippets for the Corona SDK Tutorial: Game controllers and axes (https://coronalabs.com/blog/2013/09/24/tutorial-controllers-and-axis)
-- Map the names to identify an axis with a device's physical inputs
local axisMap = {}
axisMap["OUYA Game Controller"] = {}
axisMap["OUYA Game Controller"][1] = "left_x"
axisMap["OUYA Game Controller"][2] = "left_y"
axisMap["OUYA Game Controller"][3] = "left_trigger"
axisMap["OUYA Game Controller"][4] = "right_x"
axisMap["OUYA Game Controller"][5] = "right_y"
axisMap["OUYA Game Controller"][6] = "right_trigger"
axisMap["DualShock Controller"] = {}
iphone =
{
plist =
{
CFBundleIconFile = "Icon.png",
CFBundleIconFiles =
{
"Icon.png",
"Icon@2x.png",
"Icon-60.png",
@robmiracle
robmiracle / collisions.lua
Created January 3, 2014 22:29
Two functions to test for collisions between rectangles and circles without needing physics.
-- rectangle based
local function hasCollided(obj1, obj2)
if obj1 == nil then
return false
end
if obj2 == nil then
return false
end
local left = obj1.contentBounds.xMin <= obj2.contentBounds.xMin and obj1.contentBounds.xMax >= obj2.contentBounds.xMin