Please Help!

Firstly, I’d like to thank you all for providing the wealth of information about corona and its application particularly for those of us who are new to developing app games. I have sifted through the forums and online tutorials and cannot get passed a current blockade. Any help would be greatly appreciated.

I am trying to make balls bounce off of eachother at a constant rate. I copied the sample code from “collision filter” for physics and found that this created 3 identical versions of each ball equating to 6 balls on my screen (i only uploaded 2 so far) and they only bounce off some balls then quickly come to a static bounce at the bottom of the screen whereby they no longer move up down left or right but instead bounce a little in place. I have a lot of different balls that I want to put into this game and have them spawn randomly each time a user chooses the right ball corresponding to a hint that i provide at the top of the screen therefore keeping a constant set number of balls on the screen.  Here is what i have so far

local centerX = display.contentCenterX
local centerY = display.contentCenterY
local _W = display.contentWidth
local _H = display.contentHeight

local physics = require(“physics”)
physics.start()

physics.setScale( 60 )

display.setStatusBar( display.HiddenStatusBar )

local background = display.newImageRect( “background.png”, 360, 570 )
background.x = display.contentCenterX
background.y = display.contentCenterY

display.setDefault( “anchorX”, 0.0 )    – default to TopLeft anchor point for new objects
display.setDefault( “anchorY”, 0.0 )

borderCollisionFilter = { categoryBits = 1, maskBits = 6 } – collides with (4 & 2) only
borderBodyElement = { friction=0.4, bounce=0.8, filter=borderCollisionFilter }

local borderTop = display.newRect( 0, 0, 320, 1 )
borderTop:setFillColor( 0, 0, 0, 0)        – make invisible
physics.addBody( borderTop, “static”, borderBodyElement )

local borderBottom = display.newRect( 0, 479, 320, 1 )
borderBottom:setFillColor( 0, 0, 0, 0)        – make invisible
physics.addBody( borderBottom, “static”, borderBodyElement )

local borderLeft = display.newRect( 0, 1, 1, 480 )
borderLeft:setFillColor( 0, 0, 0, 0)        – make invisible
physics.addBody( borderLeft, “static”, borderBodyElement )

local borderRight = display.newRect( 319, 1, 1, 480 )
borderRight:setFillColor( 0, 0, 0, 0)        – make invisible
physics.addBody( borderRight, “static”, borderBodyElement )
local red = {}
local blue = {}

local redCollisionFilter = { categoryBits = 2, maskBits = 3 } – collides with (2 & 1) only
local blueCollisionFilter = { categoryBits = 4, maskBits = 5 } – collides with (4 & 1) only

local redBody = { density=0.2, friction=0, bounce=0.95, radius=43.0, filter=redCollisionFilter }
local blueBody = { density=0.2, friction=0, bounce=0.95, radius=43.0, filter=blueCollisionFilter }

for i = 1,4 do
    red[i] = display.newImage( “blueball.png”, (80*i)-60, 50 + math.random(20) )
    physics.addBody( red[i], redBody )
    red[i].isFixedRotation = true
   
    blue[i] = display.newImage( “redball.png”, (80*i)-60, 250 + math.random(20) )
    physics.addBody( blue[i], blueBody )
    blue[i].isFixedRotation = true
end

-Thanks in advance for your attention. Looking forward to your replies

You can’t rely on box2d restitution (bounce) alone.  I know it seems wrong, but you’re misunderstanding the mathematics involved in this scenario.

In short you have to feed ‘energy’ back into the system and maintain velocity yourself.  Even if the physics system weren’t lossy due to rounding errors, you’d not get the constant rate movement you’re looking for.

See this example, where I show a way to solve the connundrum:

http://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2015/11/bouncy.zip

https://www.youtube.com/watch?v=H2WXQUP6pL4

I have lots of other examples to for many other questions:

http://github.com/roaminggamer/RG_FreeStuff/tree/master/AskEd

Note: This solution uses my math2d library (among other things) from SSK, but you can also get it here: 

https://store.coronalabs.com/plugin/math2d

Note2: It also uses custom builders from SSK, but you can apply the enterFrame solution to your own objects. i.e. You don’t need to use SSK.

PS 

formatyourcode.jpg

The only important bit from my example is:

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

This will maintain a constant velocity, but is kinda expensive.  So beware.

@ZeroConcept,

You IM’d me asking how to use math2d.  Here is a sample app that uses it and shows you how build.settings (which you write on your own) is set up for this case.

http://github.com/roaminggamer/RG_FreeStuff/raw/master/myPluginSamples/math2d/usage.zip

You can’t rely on box2d restitution (bounce) alone.  I know it seems wrong, but you’re misunderstanding the mathematics involved in this scenario.

In short you have to feed ‘energy’ back into the system and maintain velocity yourself.  Even if the physics system weren’t lossy due to rounding errors, you’d not get the constant rate movement you’re looking for.

See this example, where I show a way to solve the connundrum:

http://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2015/11/bouncy.zip

https://www.youtube.com/watch?v=H2WXQUP6pL4

I have lots of other examples to for many other questions:

http://github.com/roaminggamer/RG_FreeStuff/tree/master/AskEd

Note: This solution uses my math2d library (among other things) from SSK, but you can also get it here: 

https://store.coronalabs.com/plugin/math2d

Note2: It also uses custom builders from SSK, but you can apply the enterFrame solution to your own objects. i.e. You don’t need to use SSK.

PS 

formatyourcode.jpg

The only important bit from my example is:

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

This will maintain a constant velocity, but is kinda expensive.  So beware.

@ZeroConcept,

You IM’d me asking how to use math2d.  Here is a sample app that uses it and shows you how build.settings (which you write on your own) is set up for this case.

http://github.com/roaminggamer/RG_FreeStuff/raw/master/myPluginSamples/math2d/usage.zip