[Resolved] Multiple Body Element Detection

Hi all,

I’m reading the physics.addBody() function in the Docs and it states:

“Each body element may have its own physical properties, and the body element index is reported as part of each collision event – so it is possible to detect which part of a complex body was involved in a collision.”

What code do we use to return the Body Element Index of a complex body during a collision for example?

Cheers,

Michael Piercy [import]uid: 156990 topic_id: 33142 reply_id: 333142[/import]

For anyone looking for a solution to this, I found that event.otherElement (likewise event.selfElement) will return number which is the index of the bodies on the collided object.

Perhaps I was reading the documentation wrong, but it stated this the code would be event.element1, event.element2 here: http://docs.coronalabs.com/api/event/collision/element1.html

API perhaps needs updating?

I hope this helps.

Michael Piercy
Pixel Wolf Studios [import]uid: 156990 topic_id: 33142 reply_id: 131599[/import]

Hi Michael,
I looked at the docs, and it appears that the notation varies depending if you’re using a global (Runtime) collision listener, or local (table) collision listeners attached to each object. The Runtime method uses the “element1/element2” event, and the local method uses the “selfElement/otherElement” event.

If you find that this is otherwise (not true), please let me know and I’ll investigate if the docs need to be changed.

Thanks!
Brent Sorrentino [import]uid: 9747 topic_id: 33142 reply_id: 131605[/import]

For anyone looking for a solution to this, I found that event.otherElement (likewise event.selfElement) will return number which is the index of the bodies on the collided object.

Perhaps I was reading the documentation wrong, but it stated this the code would be event.element1, event.element2 here: http://docs.coronalabs.com/api/event/collision/element1.html

API perhaps needs updating?

I hope this helps.

Michael Piercy
Pixel Wolf Studios [import]uid: 156990 topic_id: 33142 reply_id: 131599[/import]

Hi Michael,
I looked at the docs, and it appears that the notation varies depending if you’re using a global (Runtime) collision listener, or local (table) collision listeners attached to each object. The Runtime method uses the “element1/element2” event, and the local method uses the “selfElement/otherElement” event.

If you find that this is otherwise (not true), please let me know and I’ll investigate if the docs need to be changed.

Thanks!
Brent Sorrentino [import]uid: 9747 topic_id: 33142 reply_id: 131605[/import]

Hi Brent,

Thanks for the reply. You’re absolutely right. Runtime collision events use event.element1 / event.element2, and local (table) collision listeners use event.selfElement / event.otherElement listeners to return the objects index number. Perhaps I miss understood the short explanation under the Overview paragraph.

Nevertheless, figured it out. Hope this helps others in the right direction.

Michael Piercy
Pixel Wolf Studios [import]uid: 156990 topic_id: 33142 reply_id: 131728[/import]

Hi Brent,

Thanks for the reply. You’re absolutely right. Runtime collision events use event.element1 / event.element2, and local (table) collision listeners use event.selfElement / event.otherElement listeners to return the objects index number. Perhaps I miss understood the short explanation under the Overview paragraph.

Nevertheless, figured it out. Hope this helps others in the right direction.

Michael Piercy
Pixel Wolf Studios [import]uid: 156990 topic_id: 33142 reply_id: 131728[/import]