preCollision Not Working

Hi,

I have been trying to make a ball bounce before it hits the ground but It still hits the ground even tho I am using preCollision. Here is the code I have written for the preCollision. Is there anything wrong?

Thanks,
Chris

[lua]local function onLocalPreCollision ()
circle:setLinearVelocity(0, -200)
end

circle:addEventListener(“preCollision”, onLocalPreCollision) [import]uid: 126017 topic_id: 23841 reply_id: 323841[/import]

Here ya go:

local physics = require("physics")  
  
local screenW = display.contentWidth  
local screenH = display.contentHeight  
  
physics.start()  
--physics.setGravity(0, 5)  
  
local bar = display.newRect(0, screenH - 10, screenW, 10)  
physics.addBody(bar, "static", {isSensor=false, density=10.0, friction=0.0})  
  
local circle = display.newCircle(200, 20, 10)  
physics.addBody(circle, "dynamic", {isSensor=false, density=10.0, friction=0.0})  
  
local function preCollision (self, event)  
 print("firing")  
 timer.performWithDelay(1, function()  
 circle:setLinearVelocity(30, -400)  
 end  
 )  
end  
circle.preCollision = preCollision  
circle:addEventListener("preCollision", circle)  

[import]uid: 21331 topic_id: 23841 reply_id: 96007[/import]

Hi,

I tried using the function above but the circle still hits the ground and collides. Is there anyway to prevent this collision?

thanks,
chris [import]uid: 126017 topic_id: 23841 reply_id: 96092[/import]

How right you are sir. Sorry I did not pay better attention. I have been noticing ALOT of issues as of late, but maybe we are just not understanding this correctly. I even tried doing multiple elements (a buffer around the object to grab a collision sooner) but no joy.

Could someone please explain why the collision hits before the preCollision in the following code…

local physics = require("physics")  
  
local screenW = display.contentWidth  
local screenH = display.contentHeight  
  
physics.start()  
physics.setDrawMode("hybrid")  
  
local bar = display.newRect(0, screenH - 10, screenW, 10)  
physics.addBody(bar, "static", {isSensor=false, density=10.0, friction=0.0})  
  
local circle = display.newCircle(200, 20, 10)  
physics.addBody(circle, "dynamic", {isSensor=false, density=10.0, friction=1.0})  
  
local function preCollision (self, event)  
 print("firing preCollision")  
 return true  
end  
  
local function collision(self, event)  
 print("firing collision")  
 return true  
end  
  
circle.preCollision = preCollision  
circle.collision = collision  
circle:addEventListener("preCollision", circle)  
circle:addEventListener("collision", circle)  

[import]uid: 21331 topic_id: 23841 reply_id: 96097[/import]

bump. [import]uid: 21331 topic_id: 23841 reply_id: 96328[/import]

Ya I still can’t get pre collision to work can someone please help us??
[import]uid: 126017 topic_id: 23841 reply_id: 96721[/import]