Destroying a Target

Blitz3D Forums/Blitz3D Beginners Area/Destroying a Target

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?

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

Wow, thanks.
I think I actually understood that.
I'll try it out.

..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..

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.

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..

Unfortunately, at my current level of experience, that is still jibberish to me. Could you please elaborate?

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):

type_target=1
type_missile=2

;Ceating the bullets
fullmag=100
Dim bullet(fullmag)
For i=0 To fullmag
bullet(i)=LoadSprite("Shot 1.bmp")
ScaleSprite bullet(i),.2,.2
HideEntity bullet(i)
EntityType bullet(i),type_missile
EntityRadius bullet(i),.02
Next

;Creating the targets with an array
Dim target_one(20)
For x=1 To 20
target_one(x)=CreateCube()
PositionEntity target_one(x),Rand(-600,800)+x*5,60,Rand(-600,800)
ScaleEntity target_one(x),2,2,2
EntityType target_one(x),type_target
Next

Collisions type_missile,type_target,2,1

While Not KeyDown(1)

;Firing bullets
If MouseHit(1) And reload=0 Then
EntityParent bullet(t),gun
PositionEntity bullet(t),1,500,-10
RotateEntity bullet(t),-90,0,0
ShowEntity bullet(t)
EntityParent bullet(t),0
t=t+1
bam=LoadSound("Plasma Gun.wav")
PlaySound bam
EndIf

bulletpath(fullmag)


UpdateWorld
RenderWorld
Flip
Wend
End

Function bulletpath(fullmag)
For q=0 To fullmag
MoveEntity bullet(q),.02,.04,2.5
If EntityDistance(bullet(q),target_one(x))=<1 Then ;<<<<<It always points to this line and says "Array Index Out Of Bounds"
HideEntity target_one(x)
EndIf
If CountCollisions(bullet(q))=True Then HideEntity bullet(q)
Next
End Function


a line pick might work well.

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 :

Const type_target=1
Const type_missile=2
Global SOUNDbam = LoadSound("Plasma Gun.wav" )

;Ceating the bullets
Global BULLET = LoadSprite("Shot 1.bmp")
ScaleSprite BULLET,.2,.2
EntityType BULLET,type_missile
EntityRadius BULLET,.02
HideEntity BULLET
Global fullmag=100
Dim bullet( fullmag-1 )
For i = 0 To fullmag-1
	bullet(i)= CopyEntity( BULLET )
	HideEntity bullet(i)
Next

;Creating the targets with an array
Global targets = 20
Dim target_one( targets-1)
For x = 0 To targets-1
	target_one(x)=CreateCube()
	PositionEntity target_one(x),Rand(-600,800)+x*5,60,Rand(-600,800)
	ScaleEntity target_one(x),2,2,2
	EntityType target_one(x),type_target
Next

Collisions type_missile,type_target,2,1

While Not KeyDown(1)

	;Firing bullets
	If MouseHit(1) And reload = 0
		EntityParent bullet(t),gun
		PositionEntity bullet(t),1,500,-10
		RotateEntity bullet(t),-90,0,0
		ShowEntity bullet(t)
		EntityParent bullet(t),0
		t=t+1
		PlaySound SOUNDbam
	EndIf
	
	bulletpath()
	UpdateWorld
	RenderWorld
	Flip

Wend
End

;====================================================================
;====================================================================
;====================================================================

Function bulletpath()

	For q = 0 To fullmag-1
	
		MoveEntity bullet(q),.02,.04,2.5

		If CountCollisions(bullet(q)) > 0

			HideEntity bullet(q)

		Else

			For x = 0 To Targets-1
			
				If EntityDistance(bullet(q),target_one(x)) <= 1
					HideEntity target_one(x)
				EndIf
			
			Next

		EndIf

	Next

End Function


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.

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.

This works ... although clearly you were missing alot of code from the above.

Graphics3D 640,480,32,1

Global Gun = CreateCamera()
PositionEntity gun, 0,60,0

Const type_target=1
Const type_missile=2
Global SOUNDbam ;= LoadSound("Plasma Gun.wav" )

;Ceating the bullets
Global BULLETmesh = CreateSprite() ;LoadSprite("Shot 1.bmp")
ScaleSprite BULLETmesh,.2,.2
EntityType BULLETmesh,type_missile
EntityRadius BULLETmesh,.02
HideEntity BULLETmesh
Global fullmag=100
Dim bullet( fullmag-1 )
For i = 0 To fullmag-1
	bullet(i)= CopyEntity( BULLETmesh )
	HideEntity bullet(i)
Next

;Creating the targets with an array
Global targets = 20
Dim target_one( targets-1)
For x = 0 To targets-1
	target_one(x)=CreateCube()
	PositionEntity target_one(x),(x-10 )*5 ,60,Rand(-300,300)
	ScaleEntity target_one(x),2,2,2
	EntityType target_one(x),type_target
Next

Collisions type_missile,type_target,2,1

While Not KeyDown(1)

	TurnEntity Gun, 0, -MouseXSpeed()*.1, 0
	MoveMouse 320,240

	;Firing bullets
	If MouseHit(1) And reload = 0
		HideEntity bullet(t)
		EntityParent bullet(t),gun
		PositionEntity bullet(t),0,2,0 ;1,500,-10
		RotateEntity bullet(t),0,0,0
		ShowEntity bullet(t)
		EntityParent bullet(t),0
		t=t+1
		;PlaySound SOUNDbam
	EndIf
	
	bulletpath()
	UpdateWorld
	RenderWorld
	Flip

Wend
End

;====================================================================
;====================================================================
;====================================================================

Function bulletpath()

	For q = 0 To fullmag-1
	
		MoveEntity bullet(q),.02,-.04,2.5

		If CountCollisions(bullet(q)) > 0

			HideEntity bullet(q)

		Else

			For x = 0 To Targets-1
			
				If EntityDistance(bullet(q),target_one(x)) <= 3
					HideEntity target_one(x)
				EndIf
			
			Next

		EndIf

	Next

End Function


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.