Boucing ball and collision detection

hi everyone,

I am at work so I can’t post my code til later. But I have a question.

In my pong game. I have a ball that bounces off the paddle even if I comment out the collision event listeners like I did below .

So my question what makes the ball bounce. I don’t think it’s the collision detection.

I think its the physics.addBody section of the code. Am I correct?

So if I am any object that I add physics.addBody  that the ball hits would cause it to bounce correct ?

Whether its a rectangle, square or a line.

local crate1 = display.newImage( “crate.png” )

physics.addBody( crate1, { density=3.0, friction=0.5, bounce=0.3 } )

crate1.myName = “first crate”

local crate2 = display.newImage( “crate.png” )

physics.addBody( crate2, { density=3.0, friction=0.5, bounce=0.3 } )

crate2.myName = “second crate”

local function onLocalCollision( self, event )

    if ( event.phase == “began” ) then

        print( self.myName … ": collision began with " … event.other.myName )

    elseif ( event.phase == “ended” ) then

        print( self.myName … ": collision ended with " … event.other.myName )

    end

end

–crate1.collision = onLocalCollision

–crate1:addEventListener( “collision” )

–crate2.collision = onLocalCollision

–crate2:addEventListener( “collision” )

  1. Please format all code posts:

formatyourcode.jpg

  1.  In game physics, there are two major concepts:
  • Collision Detection - System detects a collision occurs and runs some code if it is configured to do so.  In Corona/Box2D, this is the listener code for these events:
  • Collision Response - System calculates what the physics body/bodies should do as a result of the collision.  In Corona/Box2D, this is:
    • Bodies bouncing off each other as a result of the collision/interaction.
    • Bodies pushing each other as a result of the collision/interaction.
    • Bodies spinning as a result of the collision/interaction.

You write the code for ‘collision detection’ in the form of the aforementioned listeners.  The purpose of these listeners is to make game decisions and enact game logic as a result of a detected collision (pre, current, or post).

Collision responses are the part you ‘see’ and give your game objects the illusion of being physical objects and thus behaving in a proper (or fantastical based on  your settings) ‘physical’ way.

Box2D handles the ‘collision response’ aspect based on your body settings (bounce, friction, isActive, … )

As you should now understand, disabling listeners DOES NOT affect response.  It only affects your detection code.

To affect physical response you must modify the body settings.

Important: See updates above.  I have modified my original post with additional details and clarifying points.

so  I  amright the ball bounces because of the physics.addBody section.

I am learning so much by just writing one simple game of pong…

are there other methods of making objects bounce off each other by not using physics?

I notice in java script they don’t use a physics

I still plan to use physics but I am just curious.

  1. Yes, you can write your own collision system in Lua if you know what you’re doing math-wise.   Not suggested for new folks.

  2. I think you’re confusing concepts.  Let me clarify:

  • Java is a scripting language
  • Lua is a scripting language
  • Corona is an SDK (Software Development Kit) for making games.
  • An SDK is a collection of code (generally) brought together as a single package to implement some kind of functionality or feature set.
  • Corona uses the Lua scripting language to allow you to write games/apps and to interact with the other parts/modules/libraries of the SDK.
  • Box2D is compiled into the Corona SDK as the physics engine.

Other engines or SDKs may use Java as their publicly exposed scripting language, but again it is just a scripting language not a game SDK or engine.  It needs to work with other software to do that, just like Lua is working with the other parts of Corona SDK to do the job of game/app making.

got I now.  I am sure I will have more questions  but you answered my question thanks.

can there be more than one collison detecton going on in my game

I want separate actions to happen when the ball hits the walls than when it hits the paddles

see my code  below

local myCircle = display.newCircle( 200, 200, 30 ) myCircle:setFillColor( 0.5 ) myCircle.alpha =0 myRect.collision = ballcollison myRect:addEventListener("collision") myCircle.collision = ballcollison myCircle:addEventListener( "collision" ) ---leftwall collision ---collsion notificatons when ball hits paddle and move paddle 2 function wallcollison( self, event) ----added if ( event.phase == "began" ) then print ("event left wall began") elseif ( event.phase == "ended" ) then print ("ball hit wall") end end myleft.collision2 = wallcollison myleft:addEventListener( "collision2" )

Hi @mritorto,

Absolutely, there can be hundreds of collisions registered for a single object… your code just needs to filter out which two objects have collided.

Typically, this is done by assigning “name” properties or similar to various objects, then comparing those names during a collision event. This lets you determine if the object hit another object of a specific type/name, and then you can take the proper action.

This example shows how it can be done:

https://docs.coronalabs.com/guide/physics/collisionDetection/index.html#local-collision-handling

Hope this helps,

Brent

This section of the “Getting Started” guide also outlines how to filter out specific object collisions:

https://docs.coronalabs.com/guide/programming/03/index.html#collision-handling

I got it working  . All I have to do is add scoring and and a game over /restart section to my game.

I also have to improve the ball and paddle movement. but at least I am almost there.

I have spent a month this cause I only get to spend 1 to 2 hrs a week on this and I am brand new to programming

It seems as if name.collision can’t  be name.collision2

see my code below

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- ----pong game ----- -- Your code here local physics = require("physics") physics.start() ----- ----left side of room local myleft = display.newLine( 10, -50, 10, 1000 ) physics.addBody( myleft, "static" ,{density = 0, friction = 0}) ----rightside of room local myright = display.newLine( 310, 900, 310, -50 ) physics.addBody( myright, "static" ,{density = 0, friction = 0}) --center of room local mycenter = display.newLine( 160, 550, 170, -50 ) ---------------------------------------------------------------------- local creategamescreen local startgame local moveball local ballcollison ------creates first screen a player sees function creategamescreen() startgametext = display.newText( "Tap here to start the game!", 150, 250, "Helvetica", 24 ) startgametext:setFillColor( 1, 0, 0 ) local function removetxt() display.remove(startgametext) startgame() end startgametext:addEventListener ( "tap", removetxt ) end creategamescreen() ----this starts the game function startgame(event) timer.performWithDelay ( 3000, moveball ) ------------------------------------------------------------------- end -- Create a rectangle player one myRect = display.newRect( 0, 0, 30, 100 ) myRect.x =40 myRect.y =125 --- paddle for computer player cpupaddle=display.newImage ("paddle1.png") cpupaddle.x =270 cpupaddle.y =325 physics.addBody( cpupaddle, "static" ,{density = 1.0, friction = .3, bounce = 0}) physics.addBody( myRect, "static" ,{density = 1.0, friction = .3, bounce = 0}) ---------------move paddle player#1 local function ontouch (event) if event.phase == "ended" then ----moves it any where i click-- --- transition.to (myRect,{x=event.x, y=event.y}) ---just moves it up and down transition.to (myRect,{y=event.y}) end end Runtime:addEventListener("touch", ontouch) ----draw ball function moveball () ---local myCircle = display.newCircle( 200, 200, 30 ) myCircle = display.newCircle( 200, 200, 30 ) myCircle:setFillColor( 0.5 ) physics.addBody(myCircle,"dynamic",{density=0.1,bounce = .8, friction = 0}) --myCircle.x =330 myCircle.x =300 myCircle.y =125 ----this works to make the ball bounce off the paddle but ball moves at random for x & y myCircle:setLinearVelocity(math.random(-600,-300),math.random(-100,500)) ---this moves cpupaddle transition.to (cpupaddle,{x=cpupaddle.x, y=cpupaddle.y + math.random(-250,200)}) end ---collsion notificatons when ball hits paddle and move paddle 2 function ballcollison( self, event) --added if ( event.phase == "began" ) then print ("event began") elseif ( event.phase == "ended" ) then print ("ball hit paddle") timer.performWithDelay ( 4000, moveball ) end end local myCircle = display.newCircle( 200, 200, 30 ) myCircle:setFillColor( 0.5 ) myCircle.alpha =0 myRect.collision = ballcollison myRect:addEventListener("collision") myCircle.collision = ballcollison myCircle:addEventListener( "collision" ) ---leftwall collision ---collsion notificatons when ball hits left wall function leftwallcollison( self, event) ----added if ( event.phase == "began" ) then print ("event left wall began") elseif ( event.phase == "ended" ) then print ("ball hit left wall") end end myleft.collision = leftwallcollison myleft:addEventListener( "collision" ) ---right collision ---collsion notificatons when ball hits right wall function rightwallcollison( self, event) ----added if ( event.phase == "began" ) then print ("event right wall began") elseif ( event.phase == "ended" ) then print ("ball hit right wall") end end myright.collision = rightwallcollison myright:addEventListener( "collision" )
  1. Please format all code posts:

formatyourcode.jpg

  1.  In game physics, there are two major concepts:
  • Collision Detection - System detects a collision occurs and runs some code if it is configured to do so.  In Corona/Box2D, this is the listener code for these events:
  • Collision Response - System calculates what the physics body/bodies should do as a result of the collision.  In Corona/Box2D, this is:
    • Bodies bouncing off each other as a result of the collision/interaction.
    • Bodies pushing each other as a result of the collision/interaction.
    • Bodies spinning as a result of the collision/interaction.

You write the code for ‘collision detection’ in the form of the aforementioned listeners.  The purpose of these listeners is to make game decisions and enact game logic as a result of a detected collision (pre, current, or post).

Collision responses are the part you ‘see’ and give your game objects the illusion of being physical objects and thus behaving in a proper (or fantastical based on  your settings) ‘physical’ way.

Box2D handles the ‘collision response’ aspect based on your body settings (bounce, friction, isActive, … )

As you should now understand, disabling listeners DOES NOT affect response.  It only affects your detection code.

To affect physical response you must modify the body settings.

Important: See updates above.  I have modified my original post with additional details and clarifying points.

so  I  amright the ball bounces because of the physics.addBody section.

I am learning so much by just writing one simple game of pong…

are there other methods of making objects bounce off each other by not using physics?

I notice in java script they don’t use a physics

I still plan to use physics but I am just curious.

  1. Yes, you can write your own collision system in Lua if you know what you’re doing math-wise.   Not suggested for new folks.

  2. I think you’re confusing concepts.  Let me clarify:

  • Java is a scripting language
  • Lua is a scripting language
  • Corona is an SDK (Software Development Kit) for making games.
  • An SDK is a collection of code (generally) brought together as a single package to implement some kind of functionality or feature set.
  • Corona uses the Lua scripting language to allow you to write games/apps and to interact with the other parts/modules/libraries of the SDK.
  • Box2D is compiled into the Corona SDK as the physics engine.

Other engines or SDKs may use Java as their publicly exposed scripting language, but again it is just a scripting language not a game SDK or engine.  It needs to work with other software to do that, just like Lua is working with the other parts of Corona SDK to do the job of game/app making.

got I now.  I am sure I will have more questions  but you answered my question thanks.

can there be more than one collison detecton going on in my game

I want separate actions to happen when the ball hits the walls than when it hits the paddles

see my code  below

local myCircle = display.newCircle( 200, 200, 30 ) myCircle:setFillColor( 0.5 ) myCircle.alpha =0 myRect.collision = ballcollison myRect:addEventListener("collision") myCircle.collision = ballcollison myCircle:addEventListener( "collision" ) ---leftwall collision ---collsion notificatons when ball hits paddle and move paddle 2 function wallcollison( self, event) ----added if ( event.phase == "began" ) then print ("event left wall began") elseif ( event.phase == "ended" ) then print ("ball hit wall") end end myleft.collision2 = wallcollison myleft:addEventListener( "collision2" )

Hi @mritorto,

Absolutely, there can be hundreds of collisions registered for a single object… your code just needs to filter out which two objects have collided.

Typically, this is done by assigning “name” properties or similar to various objects, then comparing those names during a collision event. This lets you determine if the object hit another object of a specific type/name, and then you can take the proper action.

This example shows how it can be done:

https://docs.coronalabs.com/guide/physics/collisionDetection/index.html#local-collision-handling

Hope this helps,

Brent

This section of the “Getting Started” guide also outlines how to filter out specific object collisions:

https://docs.coronalabs.com/guide/programming/03/index.html#collision-handling