Sensors?

Imagine you have a square added to your game as a body. It is dropping towards the ground.

Now imagine the square has a very thin, rectangular sensor immediately underneath it.

Is it possible to use the sensor to know when the sensor has collided with the ground?

Yes, it is. It fires a collision event.

Is it possible to know when the sensor has stopped colliding with the ground?

Yes, it is. It fires a collision, just like any other body would.

Is it possible to know when the sensor is not in contact with the ground? ie: If the sensor were represented by a transparent blue rectangle and the ground were a transparent yellow rectangle - how would you find out when the intersecting area (in this case coloured green) ceases to exist?

In short: I don’t want to know when the collision of two bodies is started and stopped, as represented by the bodies coming to rest, but when the sensor stops being in contact (or overlaying) another body.

Thanks for any insight,

Matt.

Ps: Yes, I may well need some trees clearing so I can see the wood! [import]uid: 8271 topic_id: 5557 reply_id: 305557[/import]

[lua]event.phase == “ended”[/lua]

? [import]uid: 6645 topic_id: 5557 reply_id: 18968[/import]

This might seem too obvious, but can you simply check the phase ending (event.phase = "ended") instead of “began”? I think this might work… typically I use “began” because I want the engine to fire the collision just once, upon initial contact. Using “ended” might be exactly what you need.

Brent Sorrentino
Ignis Design LLC
[import]uid: 9747 topic_id: 5557 reply_id: 18969[/import]

Ha ha, same thought jmp909, just minutes apart! I think our solution will work perfectly for him. [import]uid: 9747 topic_id: 5557 reply_id: 18970[/import]

So you ment that Corona does not fire an “ended” phase when one object of the collision gets deleted?
[import]uid: 5712 topic_id: 5557 reply_id: 18971[/import]

Thanks guys, it’s gratifying to see so much buzz this time in the morning.

In my previous tests, it seemed to me that the ended phase event fired because the collision essentially stopped getting reported, not that the two bodies were no longer in contact. What I mean is; Say you have a static body and a dynamic body and using absolute positioning you move the dynamic over the static. When you leave the dynamic in position (and perhaps turn it static to keep it there) the ended event would fire, because the act of motion while in contact has ended, not that the two are no longer in contact.

Perhaps I have confused myself somewhere, but I have seen enough weird effects with collision events to not entirely trust them.

Thanks,

Matt [import]uid: 8271 topic_id: 5557 reply_id: 18974[/import]

I think you are right in that event.phase == “ended” is not gonna work for what you want, since I think it fires when the collision finishes i.e. the objects stop moving (though don’t quote me on that).

I think a simple check of the y position of both objects on an enterframe listener would be the best solution. If the y is lower on your rectangle then they’re no colliding. I would use object:setReferencePoint(display.TopLeftReferencePoint ) on your ground and object:setReferencePoint(display.BottomLeftReferencePoint ) on your rectangle so the y values would be equal when colliding. Of course testing is necessary, but that’s how I’d do it.
[import]uid: 10835 topic_id: 5557 reply_id: 19053[/import]

Perhaps another quick test… have you tried setting the body (or both) to “body:isSleepingAllowed = false” and using the phase ended method above? Maybe this would keep both objects active and continually checking for collisions (and their ending), even if their actual motion stopped? It’s a long shot but who knows… might just work.

Aside from this… I’m still not sure how your game objects are oriented or sized, but could you put a second sensor object somewhere above the ground, with the proper spacing between them, such that when the platform (or whatever) moves above/off the ground, it simultaneously collides with the second sensor, effectively sending another collision event to the engine? Seems like a convoluted solution, but it would be reliable and simple to implement, assuming your objects aren’t frequently changing size and such…

Brent
[import]uid: 9747 topic_id: 5557 reply_id: 19066[/import]

Good point, I’d not thought of that. I think the isSleepAllowed option is probably the direction I’ll try. My game doesn’t really have the other option.

Thanks, I’ll let you know what I find.

matt [import]uid: 8271 topic_id: 5557 reply_id: 19072[/import]