Collision not Working! Please help Bullet - Enemy!

That’s fine, we have other young developers using Corona. I think those video series should really help though… they explain some physics-related issues in pretty good detail. And when you need more help with physics stuff, just ask… myself and others here in the forums can provide assistance on that.

Brent

Okay so i was changing it and everything, and now the images print DEAD and they do not interact, they are still visible though, and the bullets still hit them, I did soldier:removeSelf() soldier = nil, i also tried soldier.isVisible = false. Like the tutorial you showed me

EDIT: Okay so now something different is happening, the bullets now just pile up infront of the moving line of soldiers, meaning the soldiers are still phyiscal bodies, they won’t change nor leave the screen.

Hi @krpatel420,

Have you studied the sample Physics projects in the Corona application? That code might help you see how to set up collisions properly. On your computer, they’re here:

Corona SDK > SampleCode > Physics

Yes, that is the first place i tried. I have been trying to fix this days before i posted on here using this as a last resort, trying not to make someone else do all the work. I’m sorry if that’s happening, but I’m stumped the collision detection stops, but the sprites keep moving,and they still act like a physics body.

I moved the function into the .lua that is made to spawn the soldiers, and now only the soldier spawned in the back disappears while the other four continue moving. Do they each have to be individuals?

EDIT: So now there is progress, the back soldier gets removed but the other 3 keep moving. (This will be the outline for the rest of the monsters i will create for the game so this will not be a problem for the other times)

I think the main thing to establish is the references to objects. If you have multiple soldiers and multiple bullets, “soldier” doesn’t refer to all of the soldiers. Even if you assign the last-created solider to that handle, the other soldiers won’t equate to that. So, you can’t just do “soldier:removeSelf()” on the collision.

That’s why it’s important to use the “event” references from the collision to determine exactly which two objects (which bullet and which soldier) you want to deal with. So, depending on the style of collision you use (local style or global style), those parameters will vary in code… but if you get them correct, you shouldn’t have any problem getting rid of them on screen.

Brent

I guess the true problem is getting them correct, looks like I will stay up a bit longer to research this stuff, it really gets confusing when you add multiple of one thing. In my game Run there were just two enemies and one you, they were each individual images and all. And after a collision you just had to change scenes.

It’s not nearly as complicated as this. Having to take away health, get rid of objects. I don’t think i quite understand the collision references even after reading all the documentation so many times. Anyway i’l keep trying and get back to you tomorrow morning (it’s 10:00 here). Thanks for helping me so much Brent! 

So I have been expiramenting for days, but the same error keeps coming up event.object1 is a nil value, i followed the tutorial on the corona website exactly, and now i don’t understand what’s happening

After all the progress i have made, all that happens now is the soldiers are not visible but their physics bodies are, the bullets don’t remove themselves anymore because of them, they do get pushed back though

EDIT: I set the draw mode for physics to hybrid and i found out that the physics body remains after the image is deleted, so i tried physics.removeBody(soldier) but to effect took place.

EDIT2: I looked at the error and it says i can not remove the body during a collision event, so how could i remove the body outside?

Did you tried it with:

local function onGlobalCollision( event ) ?

Hi @krpatel420,

Is this “bullet” exactly as you state? A small object moving very fast? If so, you might need to increase the collision sensitivity on it with the “.isBullet” property (which increases how often the object detects collision, at a tiny but necessary performance penalty, but not enough to notice if you’re using it sparingly).

Regards,

Brent

Actually, you should be able to remove the object on collision. However, there are several physics actions which you can NOT do at the exact time of collision… this is because Box2D is still working out the “math” involved with the collision, and needs to finish that before it can proceed to the next step. If you get that warning/error, you’re attempting one of those physics actions.

The solution is simple enough: you just need to perform the action after a tiny, almost imperceptible timer of 10-20 milliseconds, using “timer.performWithDelay()” (seek this out in the documentation). This allows Box2D to complete its processing, and then on the next game cycle, you can perform the physics action you need.

Just did still did not detect I wonder why.

Here is the code for the bullet.

function spawnBullet() bullet = display.newImage("bullet.png") bullet.x = 480 bullet.y = 270 bullet.name = 'bullet' bullet.isBullet = true bullet.isSensor = true physics.addBody(bullet, "dynamic", {bounce = 0}) --bullets.insert(bullets, bullet) bullet:applyLinearImpulse( -1/2, 0, bullet.x, bullet.y ) end

Hi @krpatel420,

I see you’re setting both “.isBullet” and “.isSensor” before you create the actual physics body. All physical properties must be declared after you create the body.

Brent

do i have to have it outside the function? this is my new code - sorry if i’m frustrating i’m new to this!

function spawnBullet() bullet = display.newImage("bullet.png") bullet.x = 480 bullet.y = 270 bullet.name = 'bullet' physics.addBody(bullet, "dynamic", {bounce = 0}) bullet.isBullet = true bullet:applyLinearImpulse( -1/2, 0, bullet.x, bullet.y ) end

Inside the function is correct, just after the declaration of the physics body.

Brent

Yea so that is where it is at now, i am convinced something is going wrong with collision detection. Here is the relevant soldier code - the rest is just running the sprite.

soldier = display.newSprite( enemy, sequenceData ) soldier.x = -30 soldier.y = 270 soldier:play() physics.addBody(soldier, "static") soldierhealth = 10

Did you follow the full collisions guide here?

http://developer.coronalabs.com/content/game-edition-collision-detection

It’s possible you forgot to declare the correct collisions listener? I assume you didn’t tinker with collision filters yet, did you? That could also be preventing the collision, but you probably haven’t done that yet…

I get the same number crunching is still occurring during the collision event, i don’t think anything is wrong. This happens even if i change the timer to 1000(10 secs.)

function removeBody() soldier:removeSelf() soldier = nil print("DEAD") timer.performWithDelay(500, physics.removeBody(soldier)) end function onLocalCollision(self, event) if event.other.name == "bullet" then display.remove(event.other.name) soldierHealth = soldierHealth - 5 if soldierHealth == 0 then removeBody() end end end soldier.collision = onLocalCollision soldier:addEventListener("collision", soldier)

still experimenting… and still not working. Any other suggestions or sample code?