Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

brick break's game : question
Started by yvandotet Jun 10 2018 09:47 AM

6 replies to this topic

Best Answer yvandotet , 10 June 2018 - 01:25 PM

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

[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

yvandotet

[GLOBAL: userInfoPane.html]
yvandotet
  • Contributor

  • 174 posts
  • Corona SDK

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



[TOPIC: post.html]
#2

ldurniat

[GLOBAL: userInfoPane.html]
ldurniat
  • Contributor

  • 373 posts
  • Corona SDK

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

Try (not tested)

timer.performWithDelay( 10, function()    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 )

ldurniat



[TOPIC: post.html]
#3

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,375 posts
  • Corona SDK

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.


Edited by roaminggamer, 10 June 2018 - 01:26 PM.


[TOPIC: post.html]
#4

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,375 posts
  • Corona SDK

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

 

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" )
-


[TOPIC: post.html]
#5

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,375 posts
  • Corona SDK

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


[TOPIC: post.html]
#6

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,375 posts
  • Corona SDK

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)


[TOPIC: post.html]
#7

yvandotet

[GLOBAL: userInfoPane.html]
yvandotet
  • Contributor

  • 174 posts
  • Corona SDK

  Best Answer

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


[topic_controls]
[/topic_controls]