collision not start

Hi guys…I conctact you cause i’ve some problems about collision.

I’ve two bodies (one dinamic and one static) and when they collide, the dynamic does not change direction, but it remains  pasted at the second object . I post the code down, but is only a part who interest the problem.

local physics = require “physics”

physics.start()

physics.setGravity(0, 0)

physics.setDrawMode(“hybrid”)

local physicsData = (require “shapes”).physicsData(1.0)

local dragon = display.newImage (“dragon4.png”,0,0,true)

                dragon.x = W ; dragon.y = H -25

                dragon.name = “dragon”

                physics.addBody( dragon, “static”, physicsData:get(“dragon4”)

ball = display.newImage (“ball.png”)

ball.x = 240; ball.y = 275

ball.name = “ball”

ball.myName = “ball”

 local function onGlobalCollision( event )

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

    print(event.object1.name … " & " … event.object2.name … " collision began" )

    vy= ball:getLinearVelocity()

    ball:setLinearVelocity(-vy )

  end

Runtime:removeEventListener( “collision”, onGlobalCollision )

end

Runtime:addEventListener( “collision”, onGlobalCollision )

Maybe I am missing it, but I don’t see where you are declaring a physics shape for the “ball” object, nor do I see where you are initiating the movement of the “ball” object.

This is the complete strucrure of my code. There are the ball shapes (i used physics editor) and the movement of the ball.

[lua]
local physics = require “physics”
physics.start()
physics.setGravity(0, 0)
physics.setDrawMode(“hybrid”)

local physicsData = (require “shapes”).physicsData(1.0)

local dragon = display.newImage (“dragon4.png”,0,0,true)
dragon.x = W ; dragon.y = H -25
dragon.name = “dragon”
physics.addBody( dragon, “static”, physicsData:get(“dragon4”)

ball = display.newImage (“ball.png”)
ball.x = 240; ball.y = 275
ball.name = “ball”
ball.myName = “ball”
physics.addBody( ball “dynamic”, physicsData:get(“ball”)
 
local function onGlobalCollision( event )

   ball.x = ball.x + vx
   ball.y = ball.y + vy

   if ( event.phase == “began” ) then
      print(event.object1.name … " & " … event.object2.name … " collision began" )
      vy= ball:getLinearVelocity()
      ball:setLinearVelocity(-vy )
   end

   Runtime:removeEventListener( “collision”, onGlobalCollision )
end

Runtime:addEventListener( “collision”, onGlobalCollision )

[/lua]

Hi @marcomm58,

I see a few potential issues here:

  1. Are you getting any errors? I don’t see the closing parentheses on either of your addBody() calls.

  2. It’s not a good idea to use global objects in any case, and especially with physics. As far as I can see with your code, “ball” is a global variable, unless you’ve up-value declared it somewhere above.

  3. What is “vx” and “vy” and where do you define it?

Take care,

Brent

Maybe I am missing it, but I don’t see where you are declaring a physics shape for the “ball” object, nor do I see where you are initiating the movement of the “ball” object.

This is the complete strucrure of my code. There are the ball shapes (i used physics editor) and the movement of the ball.

[lua]
local physics = require “physics”
physics.start()
physics.setGravity(0, 0)
physics.setDrawMode(“hybrid”)

local physicsData = (require “shapes”).physicsData(1.0)

local dragon = display.newImage (“dragon4.png”,0,0,true)
dragon.x = W ; dragon.y = H -25
dragon.name = “dragon”
physics.addBody( dragon, “static”, physicsData:get(“dragon4”)

ball = display.newImage (“ball.png”)
ball.x = 240; ball.y = 275
ball.name = “ball”
ball.myName = “ball”
physics.addBody( ball “dynamic”, physicsData:get(“ball”)
 
local function onGlobalCollision( event )

   ball.x = ball.x + vx
   ball.y = ball.y + vy

   if ( event.phase == “began” ) then
      print(event.object1.name … " & " … event.object2.name … " collision began" )
      vy= ball:getLinearVelocity()
      ball:setLinearVelocity(-vy )
   end

   Runtime:removeEventListener( “collision”, onGlobalCollision )
end

Runtime:addEventListener( “collision”, onGlobalCollision )

[/lua]

Hi @marcomm58,

I see a few potential issues here:

  1. Are you getting any errors? I don’t see the closing parentheses on either of your addBody() calls.

  2. It’s not a good idea to use global objects in any case, and especially with physics. As far as I can see with your code, “ball” is a global variable, unless you’ve up-value declared it somewhere above.

  3. What is “vx” and “vy” and where do you define it?

Take care,

Brent