check for collisions using types

Blitz3D Forums/Blitz3D Tutorials/check for collisions using types

I am posting to ASK someone more knowledgeable to put one here!

Well OK this is what I got. My sources are Ross, Ross C, Matty and Neuromancer as well as a host of others.

I am not going to explain what a type is. Other people have done that. I am going to explain how I use them and HOPE that someone else can help me (and others) with how to do the next step.

What this post is is how to create an arrow and a bow and shoot a target.

The arrow will travel to the target and if it hits the target it will stop. Then it will update your score.

What I would like is if it would make the target vanish.

I don't know how to do that yet.

; Game 1

Graphics3D 800,600,16,1	
SetBuffer BackBuffer()
SeedRnd(MilliSecs()) + Pi            ; RANDOM NUMBERS
AppTitle "BlowOFF - The game for the rest of us!","Do you Quit?"

; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- I put all the globals in front for clarity
;											these things have to be gloabls to re use
;
Global main_bullet=LoadMesh("sperm1.b3d") ; create a bullet that the type objects can copy.
HideEntity main_bullet						; hide it or it will be at 0,0,0
Global turret=LoadAnimMesh("penis1.b3d")	; OK so I named the gun penis... I was mad!
Global target=LoadMesh("target1.b3d")
HideEntity target							; hide it too
Global gun_timer=MilliSecs() ; timer to track the difference in millisecs
Global gun_time=250 ; time in milliseconds that the gun will fire
Global score
;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-	You should know what this is...

camera=CreateCamera()			
PositionEntity camera,0,10,-15			;	Put the camera at 0, 10 UP and -15 BACK

; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=	Work with the bullet
ScaleEntity main_bullet,1.5,1.5,1.5		;   Make it a little bigger since I made it small

; =-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-		Create first bullet type CODE BY ROSS

Type bullet
	Field entity
	Field speed#
	Field range
	Field pivot
End Type
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=	Each of these fields will hold info about the object
Type target
	Field entit
	Field xo
	Field yo
End Type

light=CreateLight()				
RotateEntity light,45,0,0		
AmbientLight 32,32,32

PositionEntity turret,0,0,0 ; 				Put the GUN in the center

bip=LoadSound("bip.ogg") ;					Sounds are more fun

MoveMouse 400,300 ; 						center the mouse in 800,600 use later to move GUN

HidePointer ; 								HIDE mouse pointer

; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=		OK here is where we use the TYPE commands
;											here I am telling the computer to:
;											make 40 targets (target was a TYPE)

For tt= 1 To 40
hit.target = New target ;					where did HIT come from... I made it up. I could
hit\entit = CopyEntity (target) ;			have used a letter or any word not reserved
hit\xo=Rnd(-100,100) ;						NOTE " \ " not " / "
hit\yo=Rnd(-50,50) ; 						Since B3D uses both + and - nums I had to use Rnd
Next ;										That way my targets are spread out evenly-ish

For tp = 1 To 40
PositionEntity hit\entit,hit\xo,hit\yo,100 ; after we make them we PUT them
Next

Collisions 2,1,2,1 ; Set collisions bullet is 1 and target is 2 (is that backwards)

While Not KeyHit(1)
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=		Routine to use mouse to move turret NEEDS WORK
If MouseX()>490 Then MoveMouse 490,MouseY()
If MouseX()<310 Then MoveMouse 310,MouseY()
xx=MouseXSpeed()
yy=MouseYSpeed()

TurnEntity turret,0,xx,0 	; rotate th turret
TurnEntity turret,yy,0,0		; angle the gun

; =-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=	Ross' routine to slow down rate of fire
	If MouseDown(1) And MilliSecs()>gun_time+gun_timer Then
	Animate turret,3
		gun_timer=MilliSecs() ; reset the timer
		create_bullet(turret); call function to create a bullet. Pass across the gun entity to the function
						  ; so the bullet can come from the guns position
		PlaySound(bip)
	End If
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= Caling more functions to update bullet position and put targets
	update_bullet()
	put_targets()


UpdateWorld
RenderWorld
Text 0,20,"SCORE: "+score

Flip

Wend

End

; most of the gun code is from Ross

Function create_bullet(turret_entity)
	b.bullet=New bullet
	
		temp_x=EntityX(turret_entity,True)
		temp_y=EntityY(turret_entity,True)
		temp_z=EntityZ(turret_entity,True)
		
		b\entity=CopyEntity(main_bullet) ; copy the main bullet entity , into the type object
		b\pivot=CreatePivot()
		EntityType b\entity,2
		PositionEntity b\entity,temp_x,temp_y,temp_z
		RotateEntity b\entity,EntityPitch(turret_entity,True),EntityYaw(turret_entity,True),0
		PositionEntity b\pivot,temp_x,temp_y,temp_z
		
		b\range=120 ; the range of the bullets
		b\speed=1 ; the speed of the bullets
End Function

Function update_bullet()
	For b.bullet = Each bullet
		MoveEntity b\entity,0,0,b\speed ; this is my collision modification HERE
			If EntityCollided (b\entity,1) Then ; no one would help with entitycollided
				score=score + 10 	; update socre
				FreeEntity b\entity  ; kill bullet if it hits... note the GOTO
				FreeEntity b\pivot ; inelegant but functional
				Delete b.bullet 
				Goto outie
			EndIf
			If EntityDistance#(b\entity,b\pivot) > b\range Then
			FreeEntity b\entity
			FreeEntity b\pivot
			Delete b.bullet
.outie
		End If
	Next
End Function

Function put_targets()
	For hit.target = Each target
	EntityType hit\entit,1
		PositionEntity hit\entit,hit\xo,hit\yo,100
	Next
End Function


UPDATE:

The fault lies Horatio, not in our stars but in our selves...

You know the computer is stupid.

It only does what it has been told to do.

If you tell it wrong it will do wrong.

I am a Grand Master a telling the computer wrong.

To make the object delete or (whatever you want it to do) in 3D you have to change your code JUST A LITTLE BIT!!!

Look: This is the LAST FUNCTION which I changed a bit:
Function put_targets()
	For hit.target = Each target
		If EntityCollided (hit\entit,2) Then
				score = score+5 
				kill = kill - 1
		 		FreeEntity hit\entit
		 		FreeEntity xo
		 		FreeEntity yo
		 		Delete hit.target
		EndIf
	Next
	For hit.target = Each target
		PositionEntity hit\entit,hit\xo,hit\yo,100
		EntityType hit\entit,1
	Next
End Function

I changed the PUT_TARGETS() routine to CHECK to see if there was a collision... if no it skipped that routine

SO THEN

it frees the entitys that were hit and DELETES the target.

THEN

It put the targets into space. This would have the effect of UPDATING the targets since if there was a collision
Good luck. PLEASE feel free to update this post I you have ANYTHING to add!
-Rook ZImbabwe

are you making a porn game?

I got bored with the spaceship shooting ufo game... changed the spaceship into... an *appendage* and the ufos into baps... bbbaps (big bubbly...) and added 70's porn music as a soundtrack... viola! A CLASSIC!!!

I even posted it on a freeware site... had 2000 hits the first week.

You can get it here:
http://www.silverimports.com/crapola/Setup_SPERMinator.exe

Adultish (smarmy) content... be adivsed