How to monitor when a physics body is added for a display object?

I am terribly out of practice with metatables and whatnot, and I’m not sure if they are applicable here.

I am working with dynamic code and the users may try to add physics bodies for the display objects that they create. However, these objects will be so oddly shaped that they are bound to have conflicts with the physics engine and I want to prevent them from being able to add physics bodies to the objects to begin with.

The simplest solution that I know of would be to create a simple enterFrame listener that checks if the objects have a bodyType, for instance, and if so, then use that information to run removeBody() function on them. However, this seems inelegant and wasteful.

Is anyone aware of more appropriate methods of detecting when a physics body is added for an object as soon as it is added?

I take it no one knows how to achieve this, or how to set up __index or __newindex metamethods for existing Corona display object tables, or that this is just impossible?

It’s not impossible, but it’s tricky - the sort of thing where “if you don’t already know how to do it, then you probably shouldn’t”  :smiley:

“simplest” solution would probably be to monkey-patch the physics lib, e.g (ottomh, just for concept).:

local physics = require("physics") -- listeners: local function onPrePhysicsAddBody(...) local displayObject = select(1,...) print("user will be adding a physics body", displayObject) end local function onPostPhysicsAddBody(...) local displayObject = select(1,...) print("user has added a physics body", displayObject) end -- monkey-patch: physics.\_addBody = physics.addBody physics.addBody = function(...) onPrePhysicsAddBody(...) physics.\_addBody(...) onPostPhysicsAddBody(...) end -- test: local rect = display.newRect(100,100,10,10) print("rect = ", rect) physics.start() physics.addBody(rect, "dynamic", { radius=20 })

You know, Dave, this was again one of those “I can’t believe I didn’t think of that” tunnel vision cases.

Thank you for this simple and elegant solution.

I take it no one knows how to achieve this, or how to set up __index or __newindex metamethods for existing Corona display object tables, or that this is just impossible?

It’s not impossible, but it’s tricky - the sort of thing where “if you don’t already know how to do it, then you probably shouldn’t”  :smiley:

“simplest” solution would probably be to monkey-patch the physics lib, e.g (ottomh, just for concept).:

local physics = require("physics") -- listeners: local function onPrePhysicsAddBody(...) local displayObject = select(1,...) print("user will be adding a physics body", displayObject) end local function onPostPhysicsAddBody(...) local displayObject = select(1,...) print("user has added a physics body", displayObject) end -- monkey-patch: physics.\_addBody = physics.addBody physics.addBody = function(...) onPrePhysicsAddBody(...) physics.\_addBody(...) onPostPhysicsAddBody(...) end -- test: local rect = display.newRect(100,100,10,10) print("rect = ", rect) physics.start() physics.addBody(rect, "dynamic", { radius=20 })

You know, Dave, this was again one of those “I can’t believe I didn’t think of that” tunnel vision cases.

Thank you for this simple and elegant solution.