Hi!
I create a kinematic object that starts outside the screen and then transitions onto to the screen and ends outside it on the other end. From that object I want to create new dynamic objects if the kinematic object is on the screen. How can I detect if it’s on or not? Until now I’ve used timers, but I don’t think it works very well.
Hi @joelwe,
I can think of two possibilities for this:
- You create an overall “screen sensor” that covers the entire screen. This would be a dynamic object (so it detects collision with the kinematic object), set as a sensor. You could also set up collision filters so that it doesn’t sense collision with anything except the kinematic object. During your transition, in whatever collision listener you set up, you should get a “began” phase as the kinematic object crosses over the screen sensor, and an “ended” phase when it exits those bounds. That “ended” phase would indicate it’s off the screen.
- Use the “contentBounds” API to determine if a particular edge of this object is off the screen. For example, if the object moves from left to right, when the transition is complete, you check for the “xMin” value of the kinematic object and compare it to the screen width. If the xMin value is greater, it resides off the screen. You could do similar checks on the other edges of the kinematic object in relation to the screen bounds.
http://docs.coronalabs.com/api/type/DisplayObject/contentBounds.html
Hope this helps,
Brent
Hi Brent and thank you for your reply!
I think I will go with your first possibility. I guess I should make like a “dangling stick”, like a one dimensional long dynamic object attached to a static object in a joint. Or do you have any better idea?
Glad to help. Your suggestion sounds reasonable… a distance joint would be a reasonable approach, attaching one end of the “dangling stick” to the static body.
http://developer.coronalabs.com/content/game-edition-physics-joints
Brent
IT WORKS! Thank you so much Brent for your help!
This is my final code:
I made several sensors so the objects don’t fall on the same spot all the time.
function onLocalPreCollision( event ) if event.other.isSensor then if event.other.myName == event.target.sensorNum then if event.contact ~= nil then event.contact.isEnabled = false event.target.Skipped = event.other if event.target.name == "copter" and not event.target.Paras then event.target.Paras = true startParas(event.target) end end else if event.contact ~= nil then event.contact.isEnabled = false end end end end function setUpSensor() for i=1, 14 do local attacher = display.newRect( 0, 0, 0, 20) attacher.x = display.contentWidth -15-i\*30 attacher.y = 0 physics.addBody(attacher, "static") local sensor = display.newRect( 0, 0, 1, display.contentHeight - 50) sensor.myName = "sensor"..i sensor.x = display.contentWidth -15-i\*30 sensor.y = display.contentHeight / 2 sensor.isSensor = true physics.addBody(sensor, "dynamic", collisionTable.sensorProp) local myJoint = physics.newJoint( "pivot", attacher, sensor, attacher.x, attacher.y, sensor.x,sensor.y ) end end function createEnemy() --...Some code... local sensorNum = math.random(1, 14) enemy.sensorNum = "sensor"..sensorNum enemy:addEventListener( "preCollision", onLocalPreCollision ) --...Some more code... end
Hi @joelwe,
I can think of two possibilities for this:
- You create an overall “screen sensor” that covers the entire screen. This would be a dynamic object (so it detects collision with the kinematic object), set as a sensor. You could also set up collision filters so that it doesn’t sense collision with anything except the kinematic object. During your transition, in whatever collision listener you set up, you should get a “began” phase as the kinematic object crosses over the screen sensor, and an “ended” phase when it exits those bounds. That “ended” phase would indicate it’s off the screen.
- Use the “contentBounds” API to determine if a particular edge of this object is off the screen. For example, if the object moves from left to right, when the transition is complete, you check for the “xMin” value of the kinematic object and compare it to the screen width. If the xMin value is greater, it resides off the screen. You could do similar checks on the other edges of the kinematic object in relation to the screen bounds.
http://docs.coronalabs.com/api/type/DisplayObject/contentBounds.html
Hope this helps,
Brent
Hi Brent and thank you for your reply!
I think I will go with your first possibility. I guess I should make like a “dangling stick”, like a one dimensional long dynamic object attached to a static object in a joint. Or do you have any better idea?
Glad to help. Your suggestion sounds reasonable… a distance joint would be a reasonable approach, attaching one end of the “dangling stick” to the static body.
http://developer.coronalabs.com/content/game-edition-physics-joints
Brent
IT WORKS! Thank you so much Brent for your help!
This is my final code:
I made several sensors so the objects don’t fall on the same spot all the time.
function onLocalPreCollision( event ) if event.other.isSensor then if event.other.myName == event.target.sensorNum then if event.contact ~= nil then event.contact.isEnabled = false event.target.Skipped = event.other if event.target.name == "copter" and not event.target.Paras then event.target.Paras = true startParas(event.target) end end else if event.contact ~= nil then event.contact.isEnabled = false end end end end function setUpSensor() for i=1, 14 do local attacher = display.newRect( 0, 0, 0, 20) attacher.x = display.contentWidth -15-i\*30 attacher.y = 0 physics.addBody(attacher, "static") local sensor = display.newRect( 0, 0, 1, display.contentHeight - 50) sensor.myName = "sensor"..i sensor.x = display.contentWidth -15-i\*30 sensor.y = display.contentHeight / 2 sensor.isSensor = true physics.addBody(sensor, "dynamic", collisionTable.sensorProp) local myJoint = physics.newJoint( "pivot", attacher, sensor, attacher.x, attacher.y, sensor.x,sensor.y ) end end function createEnemy() --...Some code... local sensorNum = math.random(1, 14) enemy.sensorNum = "sensor"..sensorNum enemy:addEventListener( "preCollision", onLocalPreCollision ) --...Some more code... end