physics intermittently reacts to object sliding over other objects

Hi !

I’m new to the forums, but I have a questions about seemingly odd results with object interactions.

The code below creates a teal colour dynamic object which has a y-axis force applied and is under no other rules except to bounce between the two white objects at top and bottom of the screen.

There other static objects that the teal object ‘slides’ by and they register a collision. The teal object seems to randomly interact with the objects (by being stopped or recoiling)

It may take a couple of runs to see the effect. I have posted a video of what happens here:

Does anyone know why this is happening? It would be better if the behaviour was consistent, but it seems to be random.

local centerX = display.contentCenterX
local centerY = display.contentCenterY
local screenLeft = display.screenOriginX
local screenWidth = display.contentWidth
local screenRight = screenLeft + screenWidth
local screenTop = display.screenOriginY
local screenHeight = (display.contentHeight)
local screenBottom = screenTop + screenHeight

local WIDE = 32

local function onLocalCollision( self, event )

  self.isFixedRotation = true 

local physics = require( "physics" )
physics.setDrawMode( "normal") -- "hybrid" )  

local obj1 = display.newRect(centerX-100, screenTop+ WIDE+60, WIDE, WIDE)

obj1:setFillColor(.2, .5, .5)
physics.addBody(obj1, "dynamic", {bounce = .5, friction = 0, density=0})

obj1.collision = onLocalCollision
obj1:addEventListener( "collision")

obj1:applyForce( .2, 0, obj1.x, obj1.y )

function delayFunc(event)
  obj1:applyForce( 0, 1, obj1.x, obj1.y )
  local result = timer.pause( timer1 )
timer1 = timer.performWithDelay( 5000, delayFunc, 0 ) 

local function boxes(num)
  local top = display.newRect(centerX-WIDE , screenTop + 32, WIDE, WIDE) ="top"
  physics.addBody(top, "static", {bounce = 1, friction=0, density=0})
  local bottom = display.newRect(centerX-WIDE ,screenBottom - WIDE, WIDE, WIDE)"bottom"
  physics.addBody(bottom, "static", {bounce = 1, friction=0, density=0}) -- 
  for x = 1, num do
    local box = display.newRect(centerX,
      screenTop+ (x * (WIDE+60)),
      WIDE) = x
    box:setFillColor(math.random(), math.random(),math.random())
    physics.addBody(box, "static", {bounce = 0, friction=0, density=0})


My guess is that it still has some sideways momentum when it first starts moving down. The momentum is very slight so it takes quite a while for it to move that 1 pixel further to the right where it’ll collide with the blocks on the side.

The next collision may be caused by some rounding error due to floating point numbers, or perhaps it still has/receives momentum towards right from something (even after the collision), but I can’t say for certain. In general, the physics engine shouldn’t be tried to handle pixel perfect collisions.

Thanks for the reply. Yes,I was thinking about rounding errors. Though the collision fires on the static objects every time so the object is registered as collisiding which I would think would then trigger interaction with the moving object to recoil or stop it.

I tried this in a more general sense where I have the moving object moving freely guided by keyboard input of up, down etc and still I see the same thing where sometimes the object recoils (bounce) or sometimes stops dead when it collides due again I believe to the collision being detected but no ‘physics’ being triggered.