Destroying a Target

Blitz3D Forums/Blitz3D Beginners Area/Destroying a Target

Ace Killjoy(Posted 2008) [#1]
I'm having trouble with collisions (or something).
I'm working on an FPS and I've set up collisions for my bullets, player, terrain, and targets and it all works fine.
The problem is that when I try to make a statement saying that when my bullet collides with a target, make it dissapear.

I've tried several things including:
For q=0 to fullmag
If CountCollisions(bullet(q))
dead=CollisionEntity(bullet(q),1)
HideEntity dead
EndIf
Next


Of course, when I do THAT it hides everything whit a collision that gets hit with a bullet.

I've also tried:
If EntityCollided(target_one(x),type_missile)=True Then HideEntity target_one(x)


Then it says "Array Index out of bounds".

How do I make it so that the bullets will only effect targets?


LedgerARC(Posted 2008) [#2]
there's a few reasons why that isn't working.

the code your using to kill the player doesn't state that it should only kill the player, it's just not going to work.

you got the whole entitycollided thing right, though it says array index out of bounds because the "x" is supposed to be the array for the bullet, not the "type" for it. Try this code in replace of your original code that makes everything it touches disapear.

For q=0 to fullmag
if EntityCollided(bullet(q),whatever the collision type is for your player eg. type_player) Then
HideEntity whatever the name of your player is
EndIf
Next

that should work, but here's some advice that I learned through many problems with collisions. Try using entitydistance, trust me, it will save you many hours of painfull work with collisions. Here's the code I would actually use for that, player will be whatever the name or your player is:

For q=0 to fullmag
if EntityDistance(bullet(q),player)=<1 Then
HideEntity player
EndIf
Next

trust me, entitydistance is a much easier to use command, and will let the collisions of your game be less of a hassle.

here's som more advice :D

I only use collisions for speeding up the game, by the sounds of it you know how to shoot the bullets and whatever else. but here's a hint, use this line of code in your game in your main loop, and make sure this is the only line of code that involves collisions. Use entitydistance for everything else.

If countcollisions(bullet(q)) then hideentity bullet(q) : positionentity bullet(q),0,-10,0 : rotateentity bullet(q),0,0,0

then make sure to call the showentity command when you fire the bullet again.

hope I helped, and that I didn't confuse you :D
Andrew


Ace Killjoy(Posted 2008) [#3]
Wow, thanks.
I think I actually understood that.
I'll try it out.


Naughty Alien(Posted 2008) [#4]
..well..entitydistance are not accurate having in mind that its related only to pivot of target object, so it may happen that your hits happen on places they shouldnt or not at all..


Ace Killjoy(Posted 2008) [#5]
Well, I did this:
For q=0 to fullmag
If EntityDistance(bullet(q),target_one(x))=<1 Then
HideEntity player
EndIf
Next

I put the x in target_one because when I don't it says "Identifier target_one may not be used this way".
So it still gave me an "Array Index Out Of Bounds".

Same thing with:
For q=0 to fullmag
If EntityCollided(bullet(q),type_target) Then
HideEntity target_one(x)
EndIf
Next

I'm sure it's because of the x but I don't know what else to do.

PS: type_target is target_one's collision.


Naughty Alien(Posted 2008) [#6]
why simply not use ray picking for bullet collision check..and if you use one of collision libraries available for B3D or physics library, it will work fast and very accurate..


Ace Killjoy(Posted 2008) [#7]
Unfortunately, at my current level of experience, that is still jibberish to me. Could you please elaborate?


Ace Killjoy(Posted 2008) [#8]
Sorry, I probably should have shown this in the first place.
Here are the lines of code relating to my problem (the graphics and most other stuff works fine by the way):




Nate the Great(Posted 2008) [#9]
a line pick might work well.


Stevie G(Posted 2008) [#10]
Forget linepick - he just want to get rid of the array index error which noone seems to have addressed.

Note that you don't need to load the bullet sprite 20 times - load it once and use that as a blueprint for the others. The same applies to the sound, load it once as a global and re-use it as and when required.

There are better ways to do this but I didn't want to re-write all your code. The following should sort your array index issue though :




Ace Killjoy(Posted 2008) [#11]
Thanks! That took care of that pesky Array Index message, but the targets are still not disappearing.

EDIT: Something IS happening. The targets are disappearing only every now and then when I hit them.


Nate the Great(Posted 2008) [#12]
I haven't gotten a chance to look over your code but when I get into a position like this, I just restart if it is early in the game.


Stevie G(Posted 2008) [#13]
This works ... although clearly you were missing alot of code from the above.




Ace Killjoy(Posted 2008) [#14]
Success!!
I guess "EntityDistance(bullet(q),target_one(x))<=1" was not enough.
I scaled the targets to 2, so a distance of 2 or greater fixed it.
Thank you everyone.