Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to monitor when a physics body is added for a display object?
Started by XeduR @Spyric Aug 23 2019 12:21 PM

3 replies to this topic
physics metamethods metatable physics bodies

Best Answer davebollinger , 26 August 2019 - 10:34 AM

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"  :D

 

"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 })

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Corona Geek

  • 1,030 posts
  • Corona SDK

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?



[TOPIC: post.html]
#2

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Corona Geek

  • 1,030 posts
  • Corona SDK

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?



[TOPIC: post.html]
#3

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,373 posts
  • Corona SDK

  Best Answer

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"  :D

 

"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 })

  • XeduR @Spyric likes this

[TOPIC: post.html]
#4

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Corona Geek

  • 1,030 posts
  • Corona SDK

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.




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: physics, metamethods, metatable, physics bodies