I completed the Balloon tap game from Corona, I’d like to change it a bit, but I’m not sure how to start. I would like to add directional input from tap. IE if you tap the balloon in the center it goes straight up, but if you tap it slightly to the right of center it goes to the left, and vise versa. This would be on a gradient, the closer to center you tap the balloon, the less direction changes. The further the tap is from center the more force is applied in the opposite direction.
Elements of the solution:
- 2d math to measure distance of touch from center:
- physics force or impulse:
Without looking at the balloon tutorial, my guess is the solution would be something like this (many variants are possible)
- Download, then install ssk to get math2d lib:
a. https://github.com/roaminggamer/SSK2
b. https://roaminggamer.github.io/RGDocs/pages/SSK2/#installing-ssk2
-
In your code (something like this)
local balloon = display.new… local forceMag = 0.2-- modify to suit your needs function balloon.tap( self, event ) local vec = ssk.math2d.sub( self, event ) vec = ssk.math2d.scale( vec, forceMag * self.mass ) self:applyLinearImpulse( vec.x, vec.y, self.x, self.y ) end balloon:addEventListener(“tap”)
Original is like this
local tapCount = 0 local background = display.newImageRect( "background.png", 360, 570 ) background.x = display.contentCenterX background.y = display.contentCenterY local tapText = display.newText( tapCount, display.contentCenterX, 20, native.systemFont, 40 ) tapText:setFillColor( 0, 0, 0 ) local platform = display.newImageRect( "platform.png", 300, 50 ) platform.x = display.contentCenterX platform.y = display.contentHeight-10 local balloon = display.newImageRect ( "balloon.png", 112, 112 ) balloon.x = display.contentCenterX balloon.y = display.contentCenterY balloon.alpha = 0.7 local physics = require( "physics" ) physics.start() physics.addBody( platform, "static" ) physics.addBody( balloon, "dynamic", { radius=50, bounce=0.3 } ) local function pushBalloon() balloon:applyLinearImpulse( 0, -.75, balloon.x, balloon.y ) tapCount = tapCount + 1 tapText.text = tapCount end balloon:addEventListener( "tap", pushBalloon )
I’m having trouble launching SSK
I added
require "ssk2.loadSSK" \_G.ssk.init()
But It’s not loading
11:54:29.320 module 'ssk2.loadSSK' not found: 11:54:29.320 no field package.preload['ssk2.loadSSK'] 11:54:29.320 no file 'C:\Users\aslee\AppData\Roaming\Corona Labs\Corona Simulator\Plugins\ssk2\loadSSK.lua' 11:54:29.320 no file 'C:\Users\aslee\OneDrive\Documents\Corona Projects\BallonTap\ssk2\loadSSK.lua' 11:54:29.320 no file 'C:\Program Files (x86)\Corona Labs\Corona\Resources\ssk2\loadSSK.lua' 11:54:29.320 no file '.\ssk2\loadSSK.lua' 11:54:29.320 no file 'C:\Program Files (x86)\Corona Labs\Corona\lua\ssk2\loadSSK.lua' 11:54:29.320 no file 'C:\Program Files (x86)\Corona Labs\Corona\lua\ssk2\loadSSK\init.lua' 11:54:29.320 no file 'C:\Program Files (x86)\Corona Labs\Corona\ssk2\loadSSK.lua' 11:54:29.320 no file 'C:\Program Files (x86)\Corona Labs\Corona\ssk2\loadSSK\init.lua' 11:54:29.320 no file 'C:\Users\aslee\AppData\Roaming\Corona Labs\Corona Simulator\Plugins\ssk2\loadSSK.dll' 11:54:29.320 no file '.\ssk2\loadSSK.dll' 11:54:29.320 no file 'C:\Program Files (x86)\Corona Labs\Corona\Resources\ssk2\loadSSK.dll' 11:54:29.320 no file '.\ssk2\loadSSK.dll' 11:54:29.320 no file 'C:\Program Files (x86)\Corona Labs\Corona\ssk2\loadSSK.dll' 11:54:29.320 no file 'C:\Program Files (x86)\Corona Labs\Corona\loadall.dll' 11:54:29.320 no file 'C:\Users\aslee\AppData\Roaming\Corona Labs\Corona Simulator\Plugins\ssk2.dll' 11:54:29.320 no file '.\ssk2.dll' 11:54:29.320 no file 'C:\Program Files (x86)\Corona Labs\Corona\Resources\ssk2.dll' 11:54:29.320 no file '.\ssk2.dll' 11:54:29.320 no file 'C:\Program Files (x86)\Corona Labs\Corona\ssk2.dll' 11:54:29.320 no file 'C:\Program Files (x86)\Corona Labs\Corona\loadall.dll' 11:54:29.320 no file 'C:\Users\aslee\AppData\Roaming\Corona Labs\Corona Simulator\Plugins\ssk2.loadSSK.dll' 11:54:29.320 no file '.\ssk2.loadSSK.dll' 11:54:29.320 no file 'C:\Program Files (x86)\Corona Labs\Corona\Resources\ssk2.loadSSK.dll' 11:54:29.320 no file '.\ssk2.loadSSK.dll' 11:54:29.320 no file 'C:\Program Files (x86)\Corona Labs\Corona\ssk2.loadSSK.dll' 11:54:29.320 no file 'C:\Program Files (x86)\Corona Labs\Corona\loadall.dll' 11:54:29.320 stack traceback: 11:54:29.320 [C]: in function 'require' 11:54:29.320 ?: in function 'require' 11:54:29.320 C:\Users\aslee\OneDrive\Documents\Corona Projects\BallonTap\main.lua:27: in main chunk
I copied the “SSK2-master” folder to C:…\Corona Projects\BaloonTap same location as my main.lua
Don’t copy master to your project.
Copy the ssk subfolder to your project.
Master contains:
- ssk
- validation
- and bunches of other support content
Also, the current game code you posted need to be modifed. You need to ad the tap listener to the balloon.
Is the balloon project/tutorial available for download? I could look, but since you’re already working on it I’ll let you.
If it is downloadable as a complete and functioning project I might modify it later to show you what I mean.
Yes Corona has the source files here https://github.com/coronalabs/GettingStarted01/archive/master.zip
I thought that’s what the last line was for.
balloon:addEventListener( "tap", pushBalloon )
-
This is a little advanced, but you’ll eventually need to learn it so… full speed ahead.
-
Download this: https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/05/GettingStartedHelp.zip
Compare the contents of the two folders:
- modified
- original
Besides the math, the big change was to the style of listener.
See here for more on listener styles/types:
https://docs.coronalabs.com/guide/events/detectEvents/index.html#local-events
This may actually still be confusing to you.
@everyone - If somone else remembers where the full tutorial on listeners is, please share the link here.
vec = ssk.math2d.scale( vec, forceMag \* self.mass )
Where is mass coming from? Is it part of the physics radius?
Mass is an integral value to the body based on its square area and density.
You can change density and the default is 1.
If you don’t use mass in your force and impulse equations and later you change the size of the object, the behavior will change because the mass will increase or decrease. Thus, to keep the results you tune in, use mass as a factor in your equations as I have.
Where can I read about placing constraints on the screen. I was thinking about placing Walls but that seems inefficient. So that the balloon doesn’t go out of the screen to the left and right.
@asleeprj
You can also use trigonometry to set the forceX, and forceY of the linear impulse. For whatever reason, I ended up using trig instead of vectors for all of my physics calculations. I haven’t compared the two in a while, but my general impression is that vectors (primarily being angle-based) are a faster, cleaner and more efficient for position and motion calculation but trig (being circle-based) has a more nuanced feel that I like for my systems which are more biological rather than vehicular.
I’ve adjusted @roaminggamer’s code for a trig application below.
In the near future you will want to explore linear and angular damping in the physics object properties section
[lua]
local function getAngle ( o, t, inRadians )
local inRadians = inRadians or false
if (inRadians == true) then
local radians = math.atan2(t.y - o.y, t.x - o.x)
return radians – radian - atan2 adjusts or quadrant
elseif (inRadians == false) then
local degrees = math.deg( math.atan2(t.y - o.y, t.x - o.x) )
return degrees – degrees - atan2 adjusts or quadrant
end
end
function balloon.tap( self, event )
local forceFactor = self.mass * 1000 – adjust as needed
local angle = getAngle(self, event, true) – get angle returned in radians
local forceX = math.cos(angle) * forceFactor
local forceY = math.sin(angle) * forceFactor
self:applyLinearImpulse( forceX, forceY, self.x, self.y )
end
balloon:addEventListener(“tap”)
[/lua]
Elements of the solution:
- 2d math to measure distance of touch from center:
- physics force or impulse:
Without looking at the balloon tutorial, my guess is the solution would be something like this (many variants are possible)
- Download, then install ssk to get math2d lib:
a. https://github.com/roaminggamer/SSK2
b. https://roaminggamer.github.io/RGDocs/pages/SSK2/#installing-ssk2
-
In your code (something like this)
local balloon = display.new… local forceMag = 0.2-- modify to suit your needs function balloon.tap( self, event ) local vec = ssk.math2d.sub( self, event ) vec = ssk.math2d.scale( vec, forceMag * self.mass ) self:applyLinearImpulse( vec.x, vec.y, self.x, self.y ) end balloon:addEventListener(“tap”)
Original is like this
local tapCount = 0 local background = display.newImageRect( "background.png", 360, 570 ) background.x = display.contentCenterX background.y = display.contentCenterY local tapText = display.newText( tapCount, display.contentCenterX, 20, native.systemFont, 40 ) tapText:setFillColor( 0, 0, 0 ) local platform = display.newImageRect( "platform.png", 300, 50 ) platform.x = display.contentCenterX platform.y = display.contentHeight-10 local balloon = display.newImageRect ( "balloon.png", 112, 112 ) balloon.x = display.contentCenterX balloon.y = display.contentCenterY balloon.alpha = 0.7 local physics = require( "physics" ) physics.start() physics.addBody( platform, "static" ) physics.addBody( balloon, "dynamic", { radius=50, bounce=0.3 } ) local function pushBalloon() balloon:applyLinearImpulse( 0, -.75, balloon.x, balloon.y ) tapCount = tapCount + 1 tapText.text = tapCount end balloon:addEventListener( "tap", pushBalloon )
I’m having trouble launching SSK
I added
require "ssk2.loadSSK" \_G.ssk.init()
But It’s not loading
11:54:29.320 module 'ssk2.loadSSK' not found: 11:54:29.320 no field package.preload['ssk2.loadSSK'] 11:54:29.320 no file 'C:\Users\aslee\AppData\Roaming\Corona Labs\Corona Simulator\Plugins\ssk2\loadSSK.lua' 11:54:29.320 no file 'C:\Users\aslee\OneDrive\Documents\Corona Projects\BallonTap\ssk2\loadSSK.lua' 11:54:29.320 no file 'C:\Program Files (x86)\Corona Labs\Corona\Resources\ssk2\loadSSK.lua' 11:54:29.320 no file '.\ssk2\loadSSK.lua' 11:54:29.320 no file 'C:\Program Files (x86)\Corona Labs\Corona\lua\ssk2\loadSSK.lua' 11:54:29.320 no file 'C:\Program Files (x86)\Corona Labs\Corona\lua\ssk2\loadSSK\init.lua' 11:54:29.320 no file 'C:\Program Files (x86)\Corona Labs\Corona\ssk2\loadSSK.lua' 11:54:29.320 no file 'C:\Program Files (x86)\Corona Labs\Corona\ssk2\loadSSK\init.lua' 11:54:29.320 no file 'C:\Users\aslee\AppData\Roaming\Corona Labs\Corona Simulator\Plugins\ssk2\loadSSK.dll' 11:54:29.320 no file '.\ssk2\loadSSK.dll' 11:54:29.320 no file 'C:\Program Files (x86)\Corona Labs\Corona\Resources\ssk2\loadSSK.dll' 11:54:29.320 no file '.\ssk2\loadSSK.dll' 11:54:29.320 no file 'C:\Program Files (x86)\Corona Labs\Corona\ssk2\loadSSK.dll' 11:54:29.320 no file 'C:\Program Files (x86)\Corona Labs\Corona\loadall.dll' 11:54:29.320 no file 'C:\Users\aslee\AppData\Roaming\Corona Labs\Corona Simulator\Plugins\ssk2.dll' 11:54:29.320 no file '.\ssk2.dll' 11:54:29.320 no file 'C:\Program Files (x86)\Corona Labs\Corona\Resources\ssk2.dll' 11:54:29.320 no file '.\ssk2.dll' 11:54:29.320 no file 'C:\Program Files (x86)\Corona Labs\Corona\ssk2.dll' 11:54:29.320 no file 'C:\Program Files (x86)\Corona Labs\Corona\loadall.dll' 11:54:29.320 no file 'C:\Users\aslee\AppData\Roaming\Corona Labs\Corona Simulator\Plugins\ssk2.loadSSK.dll' 11:54:29.320 no file '.\ssk2.loadSSK.dll' 11:54:29.320 no file 'C:\Program Files (x86)\Corona Labs\Corona\Resources\ssk2.loadSSK.dll' 11:54:29.320 no file '.\ssk2.loadSSK.dll' 11:54:29.320 no file 'C:\Program Files (x86)\Corona Labs\Corona\ssk2.loadSSK.dll' 11:54:29.320 no file 'C:\Program Files (x86)\Corona Labs\Corona\loadall.dll' 11:54:29.320 stack traceback: 11:54:29.320 [C]: in function 'require' 11:54:29.320 ?: in function 'require' 11:54:29.320 C:\Users\aslee\OneDrive\Documents\Corona Projects\BallonTap\main.lua:27: in main chunk
I copied the “SSK2-master” folder to C:…\Corona Projects\BaloonTap same location as my main.lua
Don’t copy master to your project.
Copy the ssk subfolder to your project.
Master contains:
- ssk
- validation
- and bunches of other support content
Also, the current game code you posted need to be modifed. You need to ad the tap listener to the balloon.
Is the balloon project/tutorial available for download? I could look, but since you’re already working on it I’ll let you.
If it is downloadable as a complete and functioning project I might modify it later to show you what I mean.
Yes Corona has the source files here https://github.com/coronalabs/GettingStarted01/archive/master.zip
I thought that’s what the last line was for.
balloon:addEventListener( "tap", pushBalloon )
-
This is a little advanced, but you’ll eventually need to learn it so… full speed ahead.
-
Download this: https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/05/GettingStartedHelp.zip
Compare the contents of the two folders:
- modified
- original