brick break's game : question

hello

in my post : https://forums.coronalabs.com/topic/72956-bug-with-2d-box-collision/ 

i was talking about a bug involve during the collision between the ball and the horizontal bar, and i found that it was because I used ball.x in the collision process.

But I have one question that I was unabled to solve :

when the ball arrives to the horizontal bar from the right side, and touch the left part of the bar , the ball simply bounce and continue to the left side.

But when the ball touch the right part of the bar, we want that the ball go back to the right side.

And at that point, I am block, because I need to calculate the difference between ball.x and bar.x, to check if it’s positive or negative.

51KU4QJpf5L.png

function ball:collision(event) local ph=event.phase local x,y=event.x,event.y local other=event.other if ph=="began" then if other.myName=="bloc" then other.delete() end // the brick to break elseif ph=="ended" then local vx,vy=self:getLinearVelocity() if other.myName=="bar" then local dx = self.x-bar.x // here is the problem : I need to calculate this, but this create bug ! if dx\*vx\<0 then vx=-vx end // changing direction self:setLinearVelocity( vx,vy ) end end end ball:addEventListener("collision",ball)

thanks you

According to documentation(see Important table) you can’t call object.x method during collision event.

Try (not tested)

timer.performWithDelay( 10, function()&nbsp;&nbsp; &nbsp;if other.myName=="bar" then &nbsp; &nbsp; &nbsp; &nbsp;local dx = self.x-bar.x // here is the problem : I need to calculate this, but this create bug ! &nbsp; &nbsp; &nbsp; &nbsp;if dx\*vx\<0 then vx=-vx end // changing direction &nbsp; &nbsp; &nbsp; &nbsp;self:setLinearVelocity( vx,vy ) &nbsp; &nbsp;end end )

ldurniat

Regardless of what the docs say I don’t buy that.  You should be able to READ the  x and y properties.

Writing these properties would be an issue, but reading is fine.

Nomenclature Tip: The docs incorrectly use the term ‘call’ and then include the properties x and y in the list. 

Calling is something done to a function or method. 

Properties (fields on an object) are something you read and write. 

Lastly, most properties can be read at any time as long as the object is valid and the properties still exist.

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/06/readingXYDuringCollision.zip

https://www.youtube.com/watch?v=rfPF_Mla_FI&feature=youtu.be

local wall = display.newRect( cx + 138, cy - 150, 310, 40 ) wall.rotation = 45 physics.addBody( wall, "static" ) local wall = display.newRect( cx - 100, cy + 150, 300, 40 ) wall.rotation = 45 physics.addBody( wall, "static" ) local wall = display.newRect( cx + 134, cy + 100, 400, 40 ) wall.rotation = 135 physics.addBody( wall, "static" ) local wall = display.newRect( cx - 102, cy - 100, 400, 40 ) wall.rotation = 135 physics.addBody( wall, "static" ) local ball = display.newCircle( cx, cy - 50, 20) physics.addBody( ball, { radius = 20, bounce = 1 }) ball:setLinearVelocity( 4000, -2000) local label = display.newText( "", left + 150, top + 50 ) function ball.collision( self, event ) label.text = "\< " .. tostring( math.floor( self.x ) ) .. ", " .. tostring( math.floor( self.y ) ) .. " \>" end; ball:addEventListener( "collision" ) -

Back to the original post:

 local dx = self.x-bar.x // here is the problem : I need to calculate this, but this create bug !

The only reason that will created a bug is if self or bar is an invalid object and has been deleted.

Check your cleanup code to ensure that the objects are being correctly disposed of.

My guess is if you do this,

print( "bar", bar.setLinearVelocity, type(bar.setLinearVelocity) == "function" ) print( "self", self.setLinearVelocity, type(self.setLinearVelocity) == "function" ) local dx = self.x-bar.x // here is the problem : I need to calculate this, but this create bug !

, when your game crashes, you’ll see this printed before the crash:

bar, nil, false

or (less likely)

self, nil, false

Lastly, I’m not sure why you are referencing ‘bar’.  ‘other’ is the thing you hit.

This seems more correct:

function ball:collision(event) local ph=event.phase local x,y=event.x,event.y local other=event.other if ph=="began" then if other.myName=="bloc" then other.delete() end // the brick to break elseif ph=="ended" then local vx,vy=self:getLinearVelocity() if other.myName=="bar" then local dx = self.x-other.x if dx\*vx\<0 then vx=-vx end // changing direction self:setLinearVelocity( vx,vy ) end end end ball:addEventListener("collision",ball)

Ok I finally find what was erroneous in the code.

I also thinking that the docs speaks about changing the value of ball.x during the collision, but reading the value is allowed and does no disturbances.

In fact during my calculation I done one moment a wrong calculation that replace vx by an invalid number, something like √(-230) !

Thanks and Ciao

According to documentation(see Important table) you can’t call object.x method during collision event.

Try (not tested)

timer.performWithDelay( 10, function()&nbsp;&nbsp; &nbsp;if other.myName=="bar" then &nbsp; &nbsp; &nbsp; &nbsp;local dx = self.x-bar.x // here is the problem : I need to calculate this, but this create bug ! &nbsp; &nbsp; &nbsp; &nbsp;if dx\*vx\<0 then vx=-vx end // changing direction &nbsp; &nbsp; &nbsp; &nbsp;self:setLinearVelocity( vx,vy ) &nbsp; &nbsp;end end )

ldurniat

Regardless of what the docs say I don’t buy that.  You should be able to READ the  x and y properties.

Writing these properties would be an issue, but reading is fine.

Nomenclature Tip: The docs incorrectly use the term ‘call’ and then include the properties x and y in the list. 

Calling is something done to a function or method. 

Properties (fields on an object) are something you read and write. 

Lastly, most properties can be read at any time as long as the object is valid and the properties still exist.

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/06/readingXYDuringCollision.zip

https://www.youtube.com/watch?v=rfPF_Mla_FI&feature=youtu.be

local wall = display.newRect( cx + 138, cy - 150, 310, 40 ) wall.rotation = 45 physics.addBody( wall, "static" ) local wall = display.newRect( cx - 100, cy + 150, 300, 40 ) wall.rotation = 45 physics.addBody( wall, "static" ) local wall = display.newRect( cx + 134, cy + 100, 400, 40 ) wall.rotation = 135 physics.addBody( wall, "static" ) local wall = display.newRect( cx - 102, cy - 100, 400, 40 ) wall.rotation = 135 physics.addBody( wall, "static" ) local ball = display.newCircle( cx, cy - 50, 20) physics.addBody( ball, { radius = 20, bounce = 1 }) ball:setLinearVelocity( 4000, -2000) local label = display.newText( "", left + 150, top + 50 ) function ball.collision( self, event ) label.text = "\< " .. tostring( math.floor( self.x ) ) .. ", " .. tostring( math.floor( self.y ) ) .. " \>" end; ball:addEventListener( "collision" ) -

Back to the original post:

 local dx = self.x-bar.x // here is the problem : I need to calculate this, but this create bug !

The only reason that will created a bug is if self or bar is an invalid object and has been deleted.

Check your cleanup code to ensure that the objects are being correctly disposed of.

My guess is if you do this,

print( "bar", bar.setLinearVelocity, type(bar.setLinearVelocity) == "function" ) print( "self", self.setLinearVelocity, type(self.setLinearVelocity) == "function" ) local dx = self.x-bar.x // here is the problem : I need to calculate this, but this create bug !

, when your game crashes, you’ll see this printed before the crash:

bar, nil, false

or (less likely)

self, nil, false

Lastly, I’m not sure why you are referencing ‘bar’.  ‘other’ is the thing you hit.

This seems more correct:

function ball:collision(event) local ph=event.phase local x,y=event.x,event.y local other=event.other if ph=="began" then if other.myName=="bloc" then other.delete() end // the brick to break elseif ph=="ended" then local vx,vy=self:getLinearVelocity() if other.myName=="bar" then local dx = self.x-other.x if dx\*vx\<0 then vx=-vx end // changing direction self:setLinearVelocity( vx,vy ) end end end ball:addEventListener("collision",ball)

Ok I finally find what was erroneous in the code.

I also thinking that the docs speaks about changing the value of ball.x during the collision, but reading the value is allowed and does no disturbances.

In fact during my calculation I done one moment a wrong calculation that replace vx by an invalid number, something like √(-230) !

Thanks and Ciao