My code isn't working properly

[lua]

_W = display.contentWidth;

​_H = display.contentHeight;

db = true;

local physics = require(“physics”);

physics.start();

physics.setGravity(0,0);

 ​

box = display.newImage(“background.jpg”, 10, _H/2);
box.xScale = .2; box.yScale = .2;
box.ID = “Box”;
physics.addBody(box, “static”);

function spawn()
 circle = display.newCircle(10, 10, 20);
 circle.ID = “Circle”;
 circle:setFillColor(math.random(0,1), 0 ,0);
 physics.addBody(circle);
 circle.gravityScale = 0;
 circle.Rotation = true;
 circle.isSensor = true;
end
spawn();

function move()
 if (db == true) then
  transition.to(circle,{time = 2000, x = math.random(10, 300), y = math.random(10, 500)
  , onComplete = move});
 end 
end
move();
function moveIt(mouse)
 
 if (mouse.phase == “began”)then
  circle.oldX = circle.x;
  circle.oldY = circle.y;
  display.getCurrentStage():setFocus(circle);
  circle.hasFocus = true;
 
 elseif (circle.hasFocus) then
  if (mouse.phase == “moved”) then
   circle.x = (mouse.x - mouse.xStart) + circle.oldX;
   circle.y = (mouse.y - mouse.yStart) + circle.oldY;
   db = false;

  elseif (mouse.phase == “ended”) then
   db = true;
   move();
   display.getCurrentStage():setFocus(nil);
   circle.hasFocus = false;
  end
 end
end

circle:addEventListener(“touch”, moveIt);

local function onLocalCollision( self, event )
    if ( event.phase == “began” ) then
      if (self.ID == “Circle” and event.other.ID == “Box”) then
       print(“aya”);
      end
         print( self.ID … ": collision began with " … event.other.ID );
     end
 end
 -----------------------------------------------------------------------------------------

 circle.collision = onLocalCollision;
circle:addEventListener( “collision”, circle);

[/lua]

The part that isn’t working is the collision detection between the circle and image. (Even though it’s saved as “background.jpg” it only takes a little bit of the screen). The collision is being detected when I drag the circle off of the screen and then when it enters the screen. (it prints “Circle: collision began with Box” when I drag it back on the screen). Why is it doing this and not when it collides with box? Also, it doesn’t print aya when the collision begins if that helps.

Hi @patriciambac,

I see a few things here:

  1. When using physics, you should not scale (.xScale or .yScale) the objects. This will scale the visual appearance of the object, but the physics engine will consider it the same non-scaled size. Of course, you can create a physics body that is associated with the size that you want to scale to, but it requires a different definition of the body.

  2. You’ve created a circle, and a physics body on it, but you did not define a “radius” body, so the physics engine will bound that object with a square body.

The following guide should help you with creating proper bodies for objects:

https://docs.coronalabs.com/guide/physics/physicsBodies/index.html

Also, setting “hybrid” draw mode can help you see what the physics engine is working with internally for collisions:

https://docs.coronalabs.com/api/library/physics/setDrawMode.html

Best regards,

Brent

Hi @patriciambac,

I see a few things here:

  1. When using physics, you should not scale (.xScale or .yScale) the objects. This will scale the visual appearance of the object, but the physics engine will consider it the same non-scaled size. Of course, you can create a physics body that is associated with the size that you want to scale to, but it requires a different definition of the body.

  2. You’ve created a circle, and a physics body on it, but you did not define a “radius” body, so the physics engine will bound that object with a square body.

The following guide should help you with creating proper bodies for objects:

https://docs.coronalabs.com/guide/physics/physicsBodies/index.html

Also, setting “hybrid” draw mode can help you see what the physics engine is working with internally for collisions:

https://docs.coronalabs.com/api/library/physics/setDrawMode.html

Best regards,

Brent