EntityCollided

Blitz3D Forums/Blitz3D Beginners Area/EntityCollided

Yeah, here's an example of that. It just iterates through the collision list for the blue target and looks at what kind of entity was involved in each collision. If it was the room then the target changes direction; if it was a bullet mesh then the owning instance is indentified (there are undocumented commands that can speed this process up) and it is set to decay while sticking to the target.

It's probably not what you'd do with "bullets" and "targets" in-game (because you'd almost certainly want bullets to interact with the room too) but it does show you how to do different things with the items on a collision list based upon what they are.

MODE=0

SeedRnd MilliSecs()
Graphics3D 800,600,0,1
SetBuffer BackBuffer()
cam=CreateCamera() : CameraRange cam,.1,20
campiv=CreatePivot()
EntityParent cam,campiv
PositionEntity cam,0,0,-5

light=CreateLight()
PositionEntity light,4,4,-4

room=CreateCube()
ScaleEntity room,5,5,5
RotateEntity room,0,10,0
FlipMesh room
EntityType room,2
roomTex=CreateTexture(32,32)
Color 100,200,100
Rect 0,0,32,32,True
Color 50,100,50
Line 0,0,31,0 : Line 0,0,0,31
Color 255,255,255
ScaleTexture roomTex,.1,.1
CopyRect 0,0,32,32,0,0,BackBuffer(),TextureBuffer(roomTex)
EntityTexture room,roomTex

Type target
	Field piv
	Field mesh
End Type
currentTarget.target=New target
currentTarget\piv=CreatePivot()
currentTarget\mesh=CreateSphere(8,currentTarget\piv)
EntityType currentTarget\piv,1
EntityColor currentTarget\mesh,100,100,200
RotateEntity currentTarget\piv,Rand(0,359),Rand(0,359),Rand(0,359)

Type bullet
	Field mesh
	Field fading
	Field alpha
End Type
currentBullet.bullet=Null	
bulletTemplate=CreateSphere()
ScaleEntity bulletTemplate,.1,.1,.1
EntityRadius bulletTemplate,.1
EntityColor bulletTemplate,200,100,100
HideEntity bulletTemplate

gunPiv=CreatePivot()
gunSphere=CreateSphere(8,gunPiv)
ScaleEntity gunSphere,.5,.5,.5
EntityColor gunSphere,200,200,200
EntityRadius gunPiv,.5,.5
EntityType gunPiv,4

Collisions 1,2,2,2
Collisions 3,1,1,1
Collisions 1,4,1,2

While Not KeyHit(1)

If KeyHit(57) Then MODE=1-MODE

TurnEntity camPiv,0,1,0
If MODE=1
	modes$="TRACKING"
	PointEntity gunPiv,currentTarget\mesh
Else
	modes$="RANDOM"
	RotateEntity gunPiv,Rand(0,359),Rand(0,359),Rand(0,359)
EndIf

currentBullet.bullet=New bullet
currentBullet\mesh=CopyEntity(bulletTemplate)
PositionEntity currentBullet\mesh,EntityX(gunPiv),EntityY(gunPiv),EntityZ(gunPiv)
RotateEntity currentBullet\mesh,EntityPitch(gunPiv),EntityYaw(gunPiv),EntityRoll(gunPiv)
EntityType currentBullet\mesh,3
currentBullet\fading=False
currentBullet\alpha=100


For currentBullet=Each bullet
	If currentBullet\fading=False Then MoveEntity currentBullet\mesh,0,0,.2
	If EntityDistance (currentBullet\mesh,gunPiv)>20 Then currentBullet\fading=True
	If currentBullet\fading=True
		currentBullet\alpha=currentBullet\alpha-1
		If currentBullet\alpha=0
			FreeEntity currentBullet\mesh
			Delete currentBullet
		Else
			EntityAlpha currentBullet\mesh,currentBullet\alpha*.01
		EndIf
	EndIf
Next


MoveEntity currentTarget\piv,0,0,.1
UpdateWorld

For currentCollision=1 To CountCollisions(currentTarget\piv)
	Select GetEntityType(CollisionEntity(currentTarget\piv,currentCollision))
	
	Case 2; Room
		TurnEntity currentTarget\piv,0,40,0
		RotateEntity currentTarget\mesh,0,0,0,True
	Case 3; Bullet
		For currentBullet=Each bullet
			If currentBullet\mesh=CollisionEntity(currentTarget\piv,currentCollision)
				currentBullet\fading=True
				EntityParent currentBullet\mesh,currentTarget\mesh
			EndIf
		Next
	End Select
Next


;numBull=0
;For currentBullet=Each bullet
;	numBull=numBull+1
;Next

PointEntity cam,camPiv
RenderWorld
Text 10,10,"SPACE TO CHANGE MODE (NOW "+modes$+")."
;Text 10,30,numBull
Flip

Wend