Collision

Blitz3D Forums/Blitz3D Beginners Area/Collision

Hi all,

Could someone please explain how collision and collision types work in b3D, as i don't understand.

An example would be great.

Cheers
Chris

Most of what I know I learned from Wolron and Rob but here is some messy simple code. Bullet and cylinder... shoot the bullet and hit the cylinder and the bullet disappears... you could rework it and make the can dissappear but I didn't feel like re coding.

; ###########################################   THIS CODE IS NOT INDENTED AND IS VERY HARD TO READ
; 												VERY MESSY. 
Graphics3D 640,480,16,0 
SetBuffer BackBuffer()

light=CreateLight(1)
AmbientLight 60, 60, 50

HidePointer

Global set = 0
h=CreatePivot()
PositionEntity h,20,2,40
Global power=1
Global gun_timer=MilliSecs() ; timer to track the difference in millisecs
Global gun_time=450 ; 	time in milliseconds that the gun will fire 
;						lower numbers make the gun spit bullets like a firehose... try 250
Global ammo
Global score
; ################################################  CHANGED THIS FOR MORE FREE FIRE
Type bullet ; 			CREATE a type called bullet
	Field entity ;		gives us a bulet name
	Field speed# ; 		How FAST it will go
	Field range ; 		How long to keep it on the screen
	Field pivot ; 		An AID to AIMING
End Type 
;
;						We don't actually fill this TYPE in yet since we are going to create
;						bullets "on the fly" so to speak (as we need them.) 
;						We could ALSO do the same thing for our targets so we could reposition 
;						them later and make this a real (simple) game.
;
aim=LoadImage("aimpoint2.png") ; you are going to need a PNG for this to work 32X32 Black Back and Yellow Cross is simple enough

aimer=CreateCamera()
PositionEntity aimer,20,2,20

Global bullet=CreateSphere(32)
EntityType bullet,1
PositionEntity bullet,20,2,20
EntityColor bullet,222,255,222
ScaleEntity bullet,Rand(1,2),Rand(1,2),Rand(1,2)

plane=CreatePlane(2)
EntityColor plane,0,0,255
EntityShininess plane,62
EntityType plane,1

bottle=CreateCylinder(40)
PositionEntity bottle,20,0,50
ScaleEntity bottle,1,2,1
EntityRadius bottle, 2
EntityType bottle, 1

turn = 1 ; make the cylinder rotate

; #########################  SET COLLISIONS SO WE CAN SEE IF WE HIT
Collisions 1,2,2,1
Collisions 2,1,2,1

ammo = 20 ; number of bullets

While Not KeyDown(1)

update_bullet()
check_collide()

If MilliSecs()>gun_time+gun_timer Then ShowEntity bullet
If MilliSecs()<gun_time+gun_timer Then HideEntity bullet

; ########################### TO KEEP THE BOTTLE TURNING ONE DIRECTION
If turn = 1 Then
	turning = 1
		tilt = tilt - 1
			If tilt >= 370
				turn = 2
			EndIf 
EndIf 

If turn = 2 Then 
	turning = -1
		tilt = tilt - 1
			If tilt <= 350
				turn = 1
			EndIf 
EndIf  

If turn =1 = True Then TurnEntity bottle,0,0,turning

If turn =2 = True Then TurnEntity bottle,0,0,turning
; ############################# I DIDN'T WRITE THIS PART... WORKS THOUGH

mxs#=MouseXSpeed()/4 ; used to get mouse movement and thus AIM
mys#=MouseYSpeed()/4

xa#=(xa#-mxs#)Mod 360
ya#=(ya#+mys#)Mod 360

MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
RotateEntity aimer,ya#,xa#,0

If KeyDown( 201 )=True Then power=power+1 ; PAGE UP power up FASTER bullets

If KeyDown( 209 )=True Then power=power-1 ; PAGE DOWN power down SLOWER bullets

If power <= 1 Then
	power = 1
EndIf
 
If power >= 12 Then 
	power = 12
EndIf 

If KeyHit(57) = True Then ; 	SPACEBAR to fire
	gun_timer=MilliSecs() 	; 	reset the bullet timer
	create_bullet(aimer)	; call function to create a bullet. Pass across the aimer entity to the function
						  	; so the bullet can come from the guns position
EndIf 

UpdateWorld()
RenderWorld()

DrawImage aim,297,227

Rect 70,15,powerful,5

Text 10,10,"Power:"
Text 10,30,"Energy: " + power
Text 10,70,"Rocks Left: " + ammo
Text 10,90,"SCORE: "+score

check_collide()

Flip
Wend 
End 
; ################################# FUNCTIONS
Function create_bullet(gun_entity) ; this creates a bullet at the aimer

If ammo = 0 Then Return ; no bullets go away!
ammo = ammo - 1

HideEntity bullet ; hide old entity

	b.bullet=New bullet ; make a new bullet entity
		temp_x=EntityX(gun_entity,True)
		temp_y=EntityY(gun_entity,True)
		temp_z=EntityZ(gun_entity,True) 
			b\entity=CopyEntity(bullet)
			b\pivot=CreatePivot()
				PositionEntity b\entity,temp_x,temp_y,temp_z
					EntityType b\entity,2
						RotateEntity b\entity,EntityPitch(gun_entity,True),EntityYaw(gun_entity,True),0
					PositionEntity b\pivot,temp_x,temp_y,temp_z
				b\range = 250  ; set bullet range so it does not clutter the screen (I used power but usually set it at 200 - 300
		b\speed = power  ; set initial bullet speed

End Function

Function update_bullet()

	For b.bullet = Each bullet ; move every bullet fired
		MoveEntity b\entity,0,0,b\speed
		If EntityDistance#(b\entity,b\pivot) > b\range Then
			FreeEntity b\entity
			FreeEntity b\pivot
			Delete b.bullet
		End If
	Next
	
End Function

Function check_collide()

For b.bullet = Each bullet
	If EntityCollided(b\entity,1) Then ;since our bottle is a 1

				HideEntity b\entity
				score = score +10
				FreeEntity b\entity
				FreeEntity b\pivot
				Delete b.bullet
	EndIf
Next
	
	; this function could be reversed to hide the bottle and place a new bottle in a new location
	; simply by a small change of code... 
End Function
It is simple code. Not too messy but hacked from other programs so I apologize in advance.
-RZ

http://www.blitzbasic.com/b3ddocs/command.php?name=Collisions