Oh i see…
Well thanks for all the help and you time!
–SonicX278
Oh i see…
Well thanks for all the help and you time!
–SonicX278
One last note. I will mostly be using SSK from now on to write complex examples.
Why? Well, as I said. It is easy to read and grok the concepts as the syntax is very terse. That way people can fit the example in their heads faster.
Also, as you can see from the side-by-side comparisons… writing pure Corona takes too dang long (compared to one line of SSK based code). That is what SSK is for. To speed up my coding.
Cheers,
Ed
Hey! So i have been trying to convert it to just Corona and Lua but i just cant figure out how to do the ssk stuff… Is it possible for you to help?
Here what i have for far…
local ballSpeed = 500 -- 100 pixels per second local lw = display.newRect( display.contentCenterX - 450, display.contentCenterY, 40, 600 ) physics.addBody( lw, "static", { bounce = 1 }) local rw = display.newRect( display.contentCenterX + 450, display.contentCenterY, 40, 600 ) physics.addBody( rw, "static", { bounce = 1 }) local tw = display.newRect( display.contentCenterX, display.contentCenterY - 300, 900, 40 ) physics.addBody( tw, "static", { bounce = 1 }) local bw = display.newRect( display.contentCenterX, display.contentCenterY + 300, 900, 40 ) physics.addBody( bw, "static", { bounce = 1 }) local enterFrame = function( self ) local vx,vy = self:getLinearVelocity() vx,vy = normVec( vx, vy ) vx,vy = scaleVec( vx, vy, ballSpeed ) self:setLinearVelocity( vx, vy ) end local function newBall( x, y, angle ) local ball = display.newCircle( x, y, 25 ) physics.addBody( ball, { radius = 25, bounce = 1 }) ball.isFixedRotation = true local vec = angle2Vector( angle, true ) vec = scaleVec( vec, ballSpeed ) ball:setLinearVelocity( vec.x, vec.y ) ball.enterFrame = enterFrame ball:addEventListener( "enterFrame", ball ) end local timer = timer.performWithDelay( 1, function() newBall( display.contentCenterX, display.contentCenterY, mRand(0,359) ) end, 1 )
Thanks!
Or should i just stick with the math2D plugin? Is it easier? Id still like to know how to do it with just pure corona lua.?
Thanks!
I tried getting it work work with just the math2d plugin but no luck…
–SonicX278
I got the project working with just the math2d plugin!
Here’s the code!
local math2d = require "plugin.math2d" local physics = require "physics" physics.start() physics.setGravity(0,0) local ballSpeed = 500 local lw = display.newRect( display.contentCenterX - 450, display.contentCenterY, 40, 600 ) physics.addBody( lw, "static", { bounce = 1 }) local rw = display.newRect( display.contentCenterX + 450, display.contentCenterY, 40, 600 ) physics.addBody( rw, "static", { bounce = 1 }) local tw = display.newRect( display.contentCenterX, display.contentCenterY - 300, 900, 40 ) physics.addBody( tw, "static", { bounce = 1 }) local bw = display.newRect( display.contentCenterX, display.contentCenterY + 300, 900, 40 ) physics.addBody( bw, "static", { bounce = 1 }) local enterFrame = function( self ) local vx,vy = self:getLinearVelocity() vx,vy = math2d.normVec( vx, vy ) vx,vy = math2d.scaleVec( vx, vy, ballSpeed ) self:setLinearVelocity( vx, vy ) end local function newBall( x, y, angle ) local ball = display.newCircle( x, y, 25 ) physics.addBody( ball, { radius = 25, bounce = 1 }) ball.isFixedRotation = true local vec = math2d.angle2Vector( angle, true ) vec = math2d.scale( vec, ballSpeed ) ball:setLinearVelocity( vec.x, vec.y ) ball.enterFrame = enterFrame ball:addEventListener( "enterFrame", ball ) end local timer = timer.performWithDelay( 1, function() newBall( display.contentCenterX, display.contentCenterY, math.random(0,359) ) end, 1 )
So now the question stands – Can this be made with just LUA and Corona?
–SonicX278
Congrats.
I’m afraid I’ve been working…but had I been checking regularly I’d have answered.
A. Yes, use math2d.
B. The only important part of my example was this:
local enterFrame = function( self ) local vx,vy = self:getLinearVelocity() vx,vy = normVec( vx, vy ) vx,vy = scaleVec( vx, vy, ballSpeed ) self:setLinearVelocity( vx, vy ) end
and the physics settings: friction = 0, bounce = 1, and isFixedRotation = true (for balls).
You pretty much got all that to, so again, “Good job.”
I do think you may want to go with 0 friction though.
Well this part
local enterFrame = function( self ) local vx,vy = self:getLinearVelocity() vx,vy = normVec( vx, vy ) vx,vy = scaleVec( vx, vy, ballSpeed ) self:setLinearVelocity( vx, vy ) end
Isn’t math2d…
But this part
local vec = math2d.angle2Vector( angle, true ) vec = math2d.scale( vec, ballSpeed ) enemyTop:setLinearVelocity( vec.x, vec.y ) enemyTop.enterFrame = enterFrame enemyTop:addEventListener( "enterFrame", enemyTop )
is…
So how would you make this part pure Corona Lua? Is it possible? Or is it jut to much?
–SonicX278
It is possible, but I’m not doing long hand vector math to answer questions in the forums.
Also, the vector math is Lua code , not Corona code.
If you want to learn long hand vector math you can find many sources on the web, and/or try to dig though the math2d source (AKA RGMath2D.lua): http://github.com/roaminggamer/SSKCorona/blob/master/ssk/RGMath2D.lua
Read the *Fast functions for a straightforward tour of vector math.
i.e. If you want to learn to do 2D addition, look at the file and find
function math2do.add( ... )
Then look below it and find:
math2do.addFast( x1, y1, x2, y2 )
The prior is the flexible version (takes various arguments depending on need). The *Fast version only takes vector components as scalar values.
I really think your friend would be better served understanding the principles of what the code is doing while keeping the vector operations separated into functions. Writing the code out long hand will just make it hard to understand and not useful for future examples.
If you keep the key operations separated as functions, you and your friend can re-use and not worry about re-implementing each time.
This is the purpose and value of separating common functionality into functions, modules, and libraries of code.
Also, if one doesn’t already understand vector math, a long hand example won’t be very useful. A good lesson in vector math would.
Note: I learned vector math in school and extended my knowledge via game development and reading.
This book (PDF) has a great appendix on Trigonometry (including vectors) starting on page 911:
http://portal.aauj.edu/portal_resources/downloads/programming/windows_game_programming_guru.pdf
I still refer to this today on occasion.
One more note for clarity sake. Both of those are math2d and both are SSK. math2d is from SSK.
The first one is written using localized calls to functions in SSK’s RGMath2D.lua module. (You need to look at the whole source file that I supplied as a download to see that.)
http://github.com/roaminggamer/RG_FreeStuff/blob/master/AskEd/2015/11/bouncy/main.lua
While, the second one uses the math2d plugin.
The thing is, math2d is the same as RGMath2D.lua, except I modified it slightly to make it a standalone plugin.
This way folks can get 2D math functions w/o needing to get all of SSK too.