Detect collision without physics.?

How would i go about detecting a collision without physics?

Here is the code. Its a sample so you can put it into a portrait simulator and see what i want.

I want it so when the bullet gets to a wall it gets removed. This would be easy with physics but i dont want to use physics for this collision.

local physics = require("physics") physics.start() local centerX = display.contentCenterX local centerY = display.contentCenterY local actualW = display.actualContentWidth local actualH = display.actualContentHeight local bullet = {} local bCounter = 1 local bTimer local wallLeft = display.newRect( centerX - actualW/2 + 20, centerY, 10, actualH ) local wallRight = display.newRect( centerX + actualW/2 - 20, centerY, 10, actualH ) local wallTop = display.newRect( centerX, centerY - actualH/2 + 20, actualW, 10 ) local wallBottom = display.newRect( centerX, centerY + actualH/2 - 20, actualW, 10 ) local function shootBullet() local xB = math.random( -359, 359 ) local yB = math.random( -359, 359 ) bullet[bCounter] = display.newRect( centerX, centerY, 6, 6 ) bullet[bCounter].value = bCounter physics.addBody( bullet[bCounter], "dynamic", { isSensor = true } ) bullet[bCounter].gravityScale = 0 bullet[bCounter].myName = "bullet" bullet[bCounter]:setLinearVelocity( xB , yB ) bCounter = bCounter + 1 end bTimer = timer.performWithDelay( 100, shootBullet, -1 )

Thanks!

–SonicX278

https://coronalabs.com/blog/2013/07/23/tutorial-non-physics-collision-detection/

I know how to detect collisions without physics but since I have my table I don’t know how to do it with that.

–SonicX278

Any ideas?

–SonicX278

If i put a print in 

local function shootBullet() local xB = math.random( -359, 359 ) local yB = math.random( -359, 359 ) bullet[bCounter] = display.newRect( centerX, centerY, 6, 6 ) bullet[bCounter].value = bCounter physics.addBody( bullet[bCounter], "dynamic", { isSensor = true } ) bullet[bCounter].gravityScale = 0 bullet[bCounter].myName = "bullet" bullet[bCounter]:setLinearVelocity( xB , yB ) print( bullet[bCounter].x ) bCounter = bCounter + 1 end bTimer = timer.performWithDelay( 100, shootBullet, 1 )

Then it works

But when i try to make a function with a Runtime enterFrame listener to it if give me an error.

local function shootBullet() local count = 0 local xB = math.random( -359, 359 ) local yB = math.random( -359, 359 ) bullet[bCounter] = display.newRect( centerX, centerY, 6, 6 ) bullet[bCounter].value = bCounter physics.addBody( bullet[bCounter], "dynamic", { isSensor = true } ) bullet[bCounter].gravityScale = 0 bullet[bCounter].myName = "bullet" bullet[bCounter]:setLinearVelocity( xB , yB ) local function bob() print( bullet[bCounter].x ) end local function goOnce() local count = 1 if count == 1 then Runtime:addEventListener( "enterFrame", bob ) end end timer.performWithDelay( 1, goOnce, -1 ) bCounter = bCounter + 1 end bTimer = timer.performWithDelay( 100, shootBullet, 1 )

I really cant figure this out! Seems so easy tho!

–SonicX278 

What error?

Referring back to this code – 

local physics = require("physics") physics.start() local centerX = display.contentCenterX local centerY = display.contentCenterY local actualW = display.actualContentWidth local actualH = display.actualContentHeight local bullet = {} local bCounter = 1 local bTimer local wallLeft = display.newRect( centerX - actualW/2 + 20, centerY, 10, actualH ) local wallRight = display.newRect( centerX + actualW/2 - 20, centerY, 10, actualH ) local wallTop = display.newRect( centerX, centerY - actualH/2 + 20, actualW, 10 ) local wallBottom = display.newRect( centerX, centerY + actualH/2 - 20, actualW, 10 ) local function shootBullet(event) local count = 0 local xB = math.random( -359, 359 ) local yB = math.random( -359, 359 ) bullet[bCounter] = display.newRect( centerX, centerY, 6, 6 ) bullet[bCounter].value = bCounter physics.addBody( bullet[bCounter], "dynamic", { isSensor = true } ) bullet[bCounter].gravityScale = 0 bullet[bCounter].myName = "bullet" bullet[bCounter]:setLinearVelocity( xB , yB ) local function bob() print( bullet[bCounter].x ) end local function goOnce() local count = 1 if count == 1 then Runtime:addEventListener( "enterFrame", bob ) end end timer.performWithDelay( 1, goOnce, 1 ) bCounter = bCounter + 1 end bTimer = timer.performWithDelay( 100, shootBullet, 1 )

The error is this – 

C:\Users\ruvim\Documents\Corona Projects\Gun test\main.lua:30: attempt to index field '?' (a nil value) stack traceback: C:\Users\ruvim\Documents\Corona Projects\Gun test\main.lua:30: in function \<C:\Users\ruvim\Documents\Corona Projects\Gun test\main.lua:29\> ?: in function \<?:205\>

I also attched a screen shot.

Line 30 is the print().

–SonicX278

I’m seriously stuck! Ugh! Anyone!?

–SonicX278

Why do you add a runtime listener for every bullet?

Also, do you remove the bullets anywhere? I bet the issue is that you remove the bullet from the bullets array at some stage, then you’re attempting to print its value in the runtime listener, but it no longer exists.

If that is the case, your going to be trying to access a table element that doesn’t exist (i.e. is nil), hence the error

I only add the Runtime once…

The problem is scope. I think. I don’t remove the bullets. I’m trying to print the x for the bullet when it moving. But when I put a print in a function inside spawn bullets then it doesn’t see it and give me an error.

–SonicX278

local function goOnce() &nbsp; &nbsp; local count = 1 &nbsp; &nbsp; if count == 1 then &nbsp; &nbsp; &nbsp; &nbsp; Runtime:addEventListener( "enterFrame", bob ) &nbsp; &nbsp; end end timer.performWithDelay( 1, goOnce, 1 )

That timer is going to execute each time you call the bullet creation function, thus adding the runtime listener several times. It should override the previous one, but you should avoid doing that.

I’d move “count” to above the createBullet function, and move the “if count == 1” check to above the timer.perform… call

Yes but I only do

bTimer = timer.performWithDelay( 100, shootBullet, 1 )

Once.(For testing purposes)

–SonicX278

Merry Christmas btw!!!

–SonicX278

Please see my edit :slight_smile:

And ah, so the code you posted isn’t the same as the one in your internal side I guess?

Same to you my friend :slight_smile:

Btw, one way to see where it is going wrong is to do something like this:

print("bullet for index:", bCounter, "is", type(bullet[bCounter])

The goOnce function shouldn’t even be there. It’s just making things more confusing.

If you try my latest code sample you will get the same error.

–SonicX278

So, I tried your code. The problem was where I stated :slight_smile:
Try the below code:

local physics = require("physics") physics.start() &nbsp; local centerX = display.contentCenterX local centerY = display.contentCenterY local actualW = display.actualContentWidth local actualH = display.actualContentHeight &nbsp; local bullet = {} local bCounter = 1 local bTimer &nbsp; local wallLeft = display.newRect( centerX - actualW/2 + 20, centerY, 10, actualH ) local wallRight = display.newRect( centerX + actualW/2 - 20, centerY, 10, actualH ) local wallTop = display.newRect( centerX, centerY - actualH/2 + 20, actualW, 10 ) local wallBottom = display.newRect( centerX, centerY + actualH/2 - 20, actualW, 10 ) &nbsp; local count = 1 &nbsp; local function bob() if #bullet \>= 1 then for i = 1, #bullet do print(bullet[i].x) end end &nbsp; return true end &nbsp; local function shootBullet(event) &nbsp; &nbsp; local count = 0 &nbsp; &nbsp; local xB = math.random( -359, 359 ) &nbsp; &nbsp; local yB = math.random( -359, 359 ) &nbsp; &nbsp; bullet[bCounter] = display.newRect( centerX, centerY, 6, 6 ) &nbsp; &nbsp; bullet[bCounter].value = bCounter &nbsp; &nbsp; physics.addBody( bullet[bCounter], "dynamic", { isSensor = true } ) &nbsp; &nbsp; bullet[bCounter].gravityScale = 0 &nbsp; &nbsp; bullet[bCounter].myName = "bullet" &nbsp; &nbsp; bullet[bCounter]:setLinearVelocity( xB , yB ) &nbsp; &nbsp; bCounter = bCounter + 1 end &nbsp; bTimer = timer.performWithDelay( 100, shootBullet, 1 ) Runtime:addEventListener( "enterFrame", bob )

Yes. That works. But when i change count to 1 in the shootBullet function then it gives the same error and doesn’t print the x of the bullet on every frame.

–SonicX278

Please try the code in my previous post again (I have edited it). I posted up the wrong thing before.

That demonstrates a better way of handling your situation.

Hope this helps

That worked! I tweaked it to my liking but your method worked! Thanks a bunch!

–SonicX278

You’re most welcome :slight_smile: