Breakout-Game: force on ball increasing continuously

Hi i am trying to aply force to ball but it is increasing continuously

Code below…

----------------------------------------------------------------------------------------- -- -- level1.lua -- ----------------------------------------------------------------------------------------- local composer = require( "composer" ) local buttons = require ("modules.buttons") local scene = composer.newScene() -- include Corona's "physics" library local physics = require "physics" physics.start(); physics.pause() --physics.setDrawMode("hybrid") local startText local breakOutObjects = {} local ball local paddle local boundaryTop local boundaryLeft local boundaryRight -------------------------------------------- -- forward declarations and other locals local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth\*0.5 function scene:create( event ) -- Called when the scene's view does not exist. -- -- INSERT code here to initialize the scene -- e.g. add display objects to 'sceneGroup', add touch listeners, etc. local sceneGroup = self.view startText = display.newText("Start", halfW, screenW, native.systemFont, 24) sceneGroup:insert(startText) -- add bricks spawnBricks(sceneGroup) --add paddle print("Paddle y " .. screenH-48) paddle = display.newRoundedRect(halfW, screenH-48, 72, 16, 8, 8) -- paddle = display.newImage("buttons/btn\_teal.png", halfW, screenH) paddle.name = "paddle" physics.addBody(paddle, "static") paddle.strokeWidth = 3 paddle:setFillColor( 0.5 ) paddle:setStrokeColor( 1, 1, 1 ) paddle:addEventListener("touch", paddleMover) sceneGroup:insert(paddle) --create boundary createBoundaryAndAttachListeners(sceneGroup) createBallAndAttachListener(sceneGroup) end function spawnBricks(sceneGroup) local yPos, xPos = 72, 82 for i=1, 12, 1 do --load buttons string from modules local btn = buttons.getButton(math.random(1, 6)) --print("Button path " .. btn) local btnObject = display.newImage(btn, xPos, yPos) btnObject.name = "brick" physics.addBody(btnObject, "static") --print("Btn object position " .. btnObject.x) if(i % 3 == 0) then xPos = 0 yPos = yPos + btnObject.contentHeight + 10 --print("Increased y position to " .. yPos .. " xpos is " .. xPos) end xPos = xPos + btnObject.contentWidth + 10 sceneGroup:insert(btnObject) table.insert(breakOutObjects, btnObject) end end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then -- Called when the scene is still off screen and is about to move on screen elseif phase == "did" then -- Called when the scene is now on screen -- -- INSERT code here to make the scene come alive -- e.g. start timers, begin animation, play audio, etc. physics.start() end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then -- Called when the scene is on screen and is about to move off screen -- -- INSERT code here to pause the scene -- e.g. stop timers, stop animation, unload sounds, etc.) physics.stop() elseif phase == "did" then -- Called when the scene is now off screen end end function scene:destroy( event ) -- Called prior to the removal of scene's "view" (sceneGroup) -- -- INSERT code here to cleanup the scene -- e.g. remove display objects, remove touch listeners, save state, etc. local sceneGroup = self.view package.loaded[physics] = nil physics = nil end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) ballCollision = function(self, event) print("Event ", event.other.name) end ballEnterFrame = function(self, event) --print("Basll yPos " .. self.y) if(self.y \> screenH) then print("Reset ball position") ball.x = halfW ball.y = screenH-72 end end paddleMover = function(event) if(event.phase == "moved") then local xPaddlePos = event.x if(xPaddlePos\< event.target.contentWidth \* 0.5 ) then xPaddlePos= event.target.contentWidth \* 0.5 elseif(xPaddlePos \> screenW -(event.target.contentWidth \* 0.5)) then xPaddlePos = screenW - event.target.contentWidth \* 0.5 end event.target.x = xPaddlePos return true end end function applyForceToBall() ball:applyForce( 1, 1, ball.x, ball.y) end function createBallAndAttachListener(sceneGroup) --print("ball y " .. screenH) ball = display.newImage("ball.png", halfW, paddle.y-paddle.contentHeight) local scaleX, scaleY = 0.5, 0.5 --ball:scale( scaleX, scaleY ) local nw, nh = ball.width\*scaleX\*0.5, ball.height\*scaleY\*0.5; physics.addBody( ball, "dynamic", { friction=0, bounce=1 }); ball.angularVelocity = 0 ball.isFixedRotation = true sceneGroup:insert(ball) applyForceToBall() ball.collision = ballCollision ball.enterFrame = ballEnterFrame ball:addEventListener("collision") Runtime:addEventListener("enterFrame", ball) end function createBoundaryAndAttachListeners(sceneGroup) print("Creating boundary") boundaryTop = display.newRoundedRect(0, 0, screenW\*2, 1, 0) boundaryTop.strokeWidth = 1 boundaryTop:setFillColor( 0.5 ) boundaryTop:setStrokeColor( 1, 1, 1 ) sceneGroup:insert(boundaryTop) boundaryLeft = display.newRoundedRect(0, 0, 1, screenH\*2, 0) boundaryLeft.strokeWidth = 1 boundaryLeft:setFillColor( 0.5 ) boundaryLeft:setStrokeColor( 1, 1, 1 ) sceneGroup:insert(boundaryLeft) boundaryRight = display.newRoundedRect(screenW, 0, 1, screenH\*2, 0) boundaryRight.strokeWidth = 1 boundaryRight:setFillColor( 0.5 ) boundaryRight:setStrokeColor( 1, 1, 1 ) sceneGroup:insert(boundaryRight) end ----------------------------------------------------------------------------------------- return scene

local buttons = {} print("Modules buttons loaded") --table of buttons strings local buttonStrings = {} table.insert(buttonStrings,"buttons/btn\_blue.png") table.insert(buttonStrings,"buttons/btn\_dark\_blue.png") table.insert(buttonStrings,"buttons/btn\_green.png") table.insert(buttonStrings,"buttons/btn\_orange.png") table.insert(buttonStrings,"buttons/btn\_teal.png") table.insert(buttonStrings,"buttons/btn\_yellow.png") function buttons.getButton(index) if(index == 1) then return buttonStrings[1] elseif(index == 2) then return buttonStrings[2] elseif(index == 3) then return buttonStrings[3] elseif(index == 4) then return buttonStrings[4] elseif(index == 5) then return buttonStrings[5] elseif(index == 6) then return buttonStrings[6] end end return buttons

Hi @ashish08agre,

I’m somewhat confused how this is happening. You’re only applying force to the ball on one line, and that line is commented out. How is the ball receiving any force?

Best regards,

Brent

Hi Brent, sorry that i posted commented code. i couldn’t edit it then actually its not commented. Let me try to edit. Yeah done please review it once again. Sorry for trouble.

Hi @ashish08agre,

Thanks! Can you be more specific about this? Do you mean the ball increases in velocity after it collides with something? If not, when exactly does the force/velocity increase?

Thanks,

Brent

Yes i mean ball increases its velocity.

When the ball goes off the screen, mean when its yPos becomes > screenH so i reset the position.

 print("Reset ball position") ball.x = halfW ball.y = screenH-72

This piece of code gets called, after first reset all sbusequent reset aumatically increases ball speed. 

Hi @ashish08agre,

This is strange! The absolute only thing that’s happening (right before you see the velocity increase) is that the ball gets reset in position? It’s not also colliding with anything like the “paddle” or “btnObject” or anything like that?

When using Composer for scene management, certain scene events can occur multiple times, i.e. show() and hide() as you potentially leave the scene and re-enter it. But according to your report, you’re seeing the velocity increase completely separately from any scene events, correct?

As a side note, I think the structure you’ve built may have been around for awhile, possibly from some old tutorial or forum post. I never recommend that you outright remove the physics library on scene destroy (package.loaded[physics] = nil, etc.). That is potentially destructive practice. In fact, I also rarely recommend even calling physics.stop() on the scene hide… it’s better to just call physics.pause() instead, because most likely you’re not going to outright stop using the physics engine for a considerable amount of time. That would be a place to potentially stop the engine outright, but generally, that call should be used sparingly and cautiously.

Brent

Hi Brent,

Sorry to ask for more,

Can u please look into this in spare time :slight_smile:

https://gitlab.com/ashish29agre/breakout-lua-corona

source code of this entire root-cause.

Hi again,

What I suggest is that you reset the ball’s linear velocity when it “resets”, then call your “applyForceToBall()” function again, immediately after, to start it going into motion again. Like this, modifying your existing function:

[lua]

local function ballEnterFrame(self, event)

   --print("Basll yPos " … self.y)

   if ( ball.y > screenH ) then

      print(“Reset ball position”)

      ball.x = halfW

      ball.y = screenH-72

      ball:setLinearVelocity(0,0)

      applyForceToBall()

   end

end

[/lua]

Brent

Awesome Brent it worked. 

Its been a while for me to recollect all college physics concepts. 

So from the reference link mentioned here
https://au.answers.yahoo.com/question/index?qid=20070926035532AAKTMoF

So assuming in corona whenever we use force or velocity we need to set opposite one to 0?

Hi @ashish08agre,

Well, it’s not necessarily a hard-core rule to do this, but generally, when you want to “reset” a physical object, it’s best to set its velocity values to 0 so that it’s no carrying over any force/velocity from its previous state.

Brent

Thanks Brent

Will  try remember this.  :slight_smile:

Hi @ashish08agre,

I’m somewhat confused how this is happening. You’re only applying force to the ball on one line, and that line is commented out. How is the ball receiving any force?

Best regards,

Brent

Hi Brent, sorry that i posted commented code. i couldn’t edit it then actually its not commented. Let me try to edit. Yeah done please review it once again. Sorry for trouble.

Hi @ashish08agre,

Thanks! Can you be more specific about this? Do you mean the ball increases in velocity after it collides with something? If not, when exactly does the force/velocity increase?

Thanks,

Brent

Yes i mean ball increases its velocity.

When the ball goes off the screen, mean when its yPos becomes > screenH so i reset the position.

 print("Reset ball position") ball.x = halfW ball.y = screenH-72

This piece of code gets called, after first reset all sbusequent reset aumatically increases ball speed. 

Hi @ashish08agre,

This is strange! The absolute only thing that’s happening (right before you see the velocity increase) is that the ball gets reset in position? It’s not also colliding with anything like the “paddle” or “btnObject” or anything like that?

When using Composer for scene management, certain scene events can occur multiple times, i.e. show() and hide() as you potentially leave the scene and re-enter it. But according to your report, you’re seeing the velocity increase completely separately from any scene events, correct?

As a side note, I think the structure you’ve built may have been around for awhile, possibly from some old tutorial or forum post. I never recommend that you outright remove the physics library on scene destroy (package.loaded[physics] = nil, etc.). That is potentially destructive practice. In fact, I also rarely recommend even calling physics.stop() on the scene hide… it’s better to just call physics.pause() instead, because most likely you’re not going to outright stop using the physics engine for a considerable amount of time. That would be a place to potentially stop the engine outright, but generally, that call should be used sparingly and cautiously.

Brent

Hi Brent,

Sorry to ask for more,

Can u please look into this in spare time :slight_smile:

https://gitlab.com/ashish29agre/breakout-lua-corona

source code of this entire root-cause.

Hi again,

What I suggest is that you reset the ball’s linear velocity when it “resets”, then call your “applyForceToBall()” function again, immediately after, to start it going into motion again. Like this, modifying your existing function:

[lua]

local function ballEnterFrame(self, event)

   --print("Basll yPos " … self.y)

   if ( ball.y > screenH ) then

      print(“Reset ball position”)

      ball.x = halfW

      ball.y = screenH-72

      ball:setLinearVelocity(0,0)

      applyForceToBall()

   end

end

[/lua]

Brent

Awesome Brent it worked. 

Its been a while for me to recollect all college physics concepts. 

So from the reference link mentioned here
https://au.answers.yahoo.com/question/index?qid=20070926035532AAKTMoF

So assuming in corona whenever we use force or velocity we need to set opposite one to 0?

Hi @ashish08agre,

Well, it’s not necessarily a hard-core rule to do this, but generally, when you want to “reset” a physical object, it’s best to set its velocity values to 0 so that it’s no carrying over any force/velocity from its previous state.

Brent