Skip to content

Instantly share code, notes, and snippets.

@shawngrimes
Created October 20, 2012 18:44
Show Gist options
  • Save shawngrimes/3924315 to your computer and use it in GitHub Desktop.
Save shawngrimes/3924315 to your computer and use it in GitHub Desktop.
Corona SDK: Moving a Rocket Mouse with Physics
--Load Physics Library
local physics = require "physics"
--Show physics drawing lines, for help with debugging
physics.setDrawMode( "hybrid" )
--Start Physics
physics.start()
--Load rocket mouse physics values
local scaleFactor = 2.0
local physicsData = (require "rocketMousePhysics").physicsData(scaleFactor)
--Create the rocket mouse image
local rocketMouse=display.newImage("rocketmouse_1_run.png")
rocketMouse:scale(scaleFactor,scaleFactor)
rocketMouse:setReferencePoint(display.BottomCenterReferencePoint)
rocketMouse.y=display.contentCenterY
rocketMouse.x=rocketMouse.contentWidth
--Attach the rocket mouse physics body to the image
physics.addBody( rocketMouse, physicsData:get("rocketmouse_1_run") )
--create the floor physics object so we don't fall through
local ground = display.newRect(0, display.contentHeight-50, display.contentWidth, 50)
ground.myName="ground"
ground:setFillColor( 255, 0, 0 )
ground.isVisible = false -- optional
physics.addBody( ground, "static", { friction=0.5, bounce=0.3 })
--create the ceiling so we don't end up in space
local ceiling = display.newRect(0,0,display.contentWidth,50)
ceiling.myName="ceiling"
ceiling:setFillColor( 255, 0, 0 )
ceiling.isVisible = false -- optional
physics.addBody( ceiling, "static", { friction=0.5, bounce=0.3 })
--We are creating an anchor point so the mouse can only move up and down
local pistonGroundPoint=display.newRect(0,display.contentHeight-rocketMouse.contentHeight,rocketMouse.contentWidth,rocketMouse.contentHeight)
pistonGroundPoint.myName="pistonGround"
pistonGroundPoint:setFillColor( 0, 255, 0 )
pistonGroundPoint.isVisible = false -- optional
physics.addBody( pistonGroundPoint, "static", { friction=0.5, bounce=0.3 })
--Now we connect the mouse to the piston point
myPistonJoint = physics.newJoint( "piston", rocketMouse, pistonGroundPoint, rocketMouse.x,rocketMouse.y, 0,-display.contentHeight )
--This value tells us if the mouse is flying or not
rocketMouse.vy=0
--This function will be used to control if the mouse should move
local function moveMouse()
if(rocketMouse.vy<0) then
--This applies a force to the rocket mouse (pushing him upward)
rocketMouse:applyForce(0, rocketMouse.vy, rocketMouse.x, rocketMouse.y)
end
end
--This detects if we touch the screen to start making the mouse fly
local function onTouch(event)
if ( event.phase == "began" ) then
rocketMouse.vy = -600 * scaleFactor
elseif ( event.phase == "ended" ) then
rocketMouse.vy = 0
end
end
--This event listener, listens for the screen to be touched
Runtime:addEventListener("touch", onTouch)
--This event listener will be called every time the frame loads
Runtime:addEventListener("enterFrame",moveMouse)
-- This file is for use with Corona(R) SDK
--
-- This file is automatically generated with PhysicsEdtior (http://physicseditor.de). Do not edit
--
-- Usage example:
-- local scaleFactor = 1.0
-- local physicsData = (require "shapedefs").physicsData(scaleFactor)
-- local shape = display.newImage("objectname.png")
-- physics.addBody( shape, physicsData:get("objectname") )
--
-- copy needed functions to local scope
local unpack = unpack
local pairs = pairs
local ipairs = ipairs
local M = {}
function M.physicsData(scale)
local physics = { data =
{
["rocketmouse_1_run"] = {
{
pe_fixture_id = "rocketMouse", density = 2, friction = 0, bounce = 0,
filter = { categoryBits = 1, maskBits = 65535, groupIndex = 0 },
shape = { -33.5, 36 , -19.5, -16 , 24.5, -17 , -10.5, 49 , -31.5, 58 }
}
,
{
pe_fixture_id = "rocketMouse", density = 2, friction = 0, bounce = 0,
filter = { categoryBits = 1, maskBits = 65535, groupIndex = 0 },
shape = { 4.5, 62 , -10.5, 49 , 8.5, 45 , 16.5, 62 }
}
,
{
pe_fixture_id = "rocketMouse", density = 2, friction = 0, bounce = 0,
filter = { categoryBits = 1, maskBits = 65535, groupIndex = 0 },
shape = { -19.5, -16 , -33.5, 36 , -64.5, 23 , -45.5, -27 , -29.5, -32 }
}
,
{
pe_fixture_id = "rocketMouse", density = 2, friction = 0, bounce = 0,
filter = { categoryBits = 1, maskBits = 65535, groupIndex = 0 },
shape = { 24.5, -56 , 39.5, -31 , 24.5, -17 , -19.5, -16 , -9.5, -53 }
}
,
{
pe_fixture_id = "rocketMouse", density = 2, friction = 0, bounce = 0,
filter = { categoryBits = 1, maskBits = 65535, groupIndex = 0 },
shape = { 8.5, 45 , -10.5, 49 , 24.5, -17 , 39.5, -7 }
}
}
} }
-- apply scale factor
local s = scale or 1.0
for bi,body in pairs(physics.data) do
for fi,fixture in ipairs(body) do
if(fixture.shape) then
for ci,coordinate in ipairs(fixture.shape) do
fixture.shape[ci] = s * coordinate
end
else
fixture.radius = s * fixture.radius
end
end
end
function physics:get(name)
return unpack(self.data[name])
end
function physics:getFixtureId(name, index)
return self.data[name][index].pe_fixture_id
end
return physics;
end
return M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment