moving target

Blitz3D Forums/Blitz3D Programming/moving target

I have the most of this already complete... Just a game I developed to learn how to create a missle firing system with tonnes of help from all the Ross' Wolron, Neuromancer et al...

But I can't figure out how to make the targets revolve around the gun.

I think I set the parent\child thingee okay, but when I move them they only slide off the screen. Rotateentity doesn't seem to wrok either.

QUE!!!

; Game 1
; Copyright 2004, 2005 Ralph Wm Dunn & Suspension Software
;

Graphics3D 800,600,16,1	
SetBuffer BackBuffer()
SeedRnd(MilliSecs()) + Pi            ; RANDOM NUMBERS
AppTitle "ALIENbuster - 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 camera=CreateCamera()			
PositionEntity camera,0,10,-15			;	Put the camera at 0, 10 UP and -15 BACK
Global main_bullet=LoadMesh("missle.b3d",turret) ; create a bullet that the type objects can copy.
HideEntity main_bullet						; hide it or it will be at 0,0,0
Global turret=LoadMesh("ufo1.b3d")	
Global badman=LoadMesh("badguy.b3d",camera)
HideEntity badman							; hide it too
Global badman2=LoadMesh("badguy2.b3d",camera)
HideEntity badman2	
Global gun_timer=MilliSecs() ; timer to track the difference in millisecs
Global gun_time=125 ; time in milliseconds that the gun will fire
Global score
Global kill
Global boom
Global shotz ;								If IT AIN'T Global YOU CAN'T USE IT IN A Function
;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-	You should know what this is...

; =-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-		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 BAD DUDES
Type badman
	Field entit
	Field xo
	Field yo
	Field zo
End Type

Type badman2
	Field entit2
	Field xo2
	Field yo2
	Field zo2
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
happy=LoadSound("fart4.wav")
shootit=LoadSound("laser.wav")
SoundVolume shootit,.5
music=LoadSound("music.ogg")
LoopSound music
SoundPitch music,11000/.5
PlaySound music
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 badmans (badman was a TYPE)

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

For t= 1 To 20
hat.badman2 = New badman2 ;					where did HIT come from... I made it up. I could
hat\entit2 = CopyEntity (badman2) ;			have used a letter or any word not reserved
hat\xo2=Rnd(-100,100) ;						NOTE " \ " not " / "
hat\yo2=Rnd(-50,50)
hat\zo2=Rnd(100,125) ; 						Since B3D uses both + and - nums I had to use Rnd
Next ;	

kill=40
Collisions 1,2,2,1
Collisions 2,1,2,1 ; Set collisions bullet is 1 and badman is 2 (is that backwards)
boom = 0


Timer=MilliSecs()


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

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

; =-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=	Ross' routine to slow down rate of fire
	If MouseDown(1) And MilliSecs()>gun_time+gun_timer Then
		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(shootit)
	End If
; =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= Caling more functions to update bullet position and put badmans
	put_badmans()
	update_bullet()
	
If boom = 1 PlaySound (bip)
If boom = 2 PlaySound (happy)

UpdateWorld
RenderWorld
Text 0,10,"POINTS: "+ score
Text 0,25,"KILLED: "+ kill
Text 0,40,"SHOTS: "+shotz
total = score-shotz
Text 0,55,"SCORE: "+total
; 							Why make the SCORE like that??? Just a thing to do.
; 							1000 points for killing all - number of shots
; 							it's a skill thing...
Flip

boom = 0

Oval 50,50,100,100,0 
Line 100,100,XXX,YYY 
If MilliSecs()-Timer>1000 
Seconds=Seconds+1 
If Seconds>59 Then 
Seconds=0 
Timer=MilliSecs() 
End If
EndIf

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=259 ; the range of the bullets
		b\speed=5 ; the speed of the bullets
		shotz=shotz+1
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
				;kill = kill-1
				score = score+10 	; update socre
				FreeEntity b\entity  ; kill bullet if it hits... note the GOTO
				FreeEntity b\pivot 
				Delete hit.badman
				Delete b.bullet 
				Goto outie			; inelegant but functional
			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_badmans()
	For hit.badman = Each badman
		If EntityCollided (hit\entit,2) Then
				boom = 1
				score = score+5 
				kill = kill - 1
		 		FreeEntity hit\entit
		 		FreeEntity xo
		 		FreeEntity yo
				FreeEntity zo
		 		Delete hit.badman
		EndIf
	Next
	For hat.badman2 = Each badman2
		If EntityCollided (hat\entit2,2) Then
				boom = 2
				score = score+25
				kill = kill - 1
		 		FreeEntity hat\entit2
		 		FreeEntity xo2
		 		FreeEntity yo2
				FreeEntity zo2
		 		Delete hat.badman2
		EndIf
	Next
	For hit.badman = Each badman
		EntityType hit\entit,1
		PositionEntity hit\entit,hit\xo,hit\yo,hit\zo
	Next
		For hat.badman2 = Each badman2
		EntityType hat\entit2,1
		PositionEntity hat\entit2,hat\xo2,hat\yo2,hat\zo2
	Next
End Function


I looked at the GALAGA type code in the examples but all the variables seem to be written to confuse people. (I think if you are going to submit a program for inclusion in examples your variables have to have better names... IMO)

I really just want to rotate them slowly and be able to fire at them.

OK no help yet... I can be patient... really!
: )
-RZ

Try making the camera a child of the turret.


You could also get rid of your GOTO (and the THENs) using this adaptation of your update_bullet() 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) ; no one would help with entitycollided
				;kill = kill-1
				score = score+10 	; update socre
				FreeEntity b\entity  ; kill bullet if it hits
				FreeEntity b\pivot 
				Delete hit.badman
				Delete b.bullet 

			Else   ;Better than GOTO 

				If EntityDistance#(b\entity,b\pivot) > b\range
					FreeEntity b\entity
					FreeEntity b\pivot
					Delete b.bullet
				Endif

			End If
	Next
End Function



Hope this helps

Nik.

That certainly works in the bullet dept. Thanks... But I need to figure out how to rotate the entitys around the turret.

It is the parent \ child thing and I assume a pivot thing as well... BOTH of which I need to learn more about.
: )
-RZ

I have to be honest and say that I'm not entirely sure what your problem is. (I can't run the code because I don't have the media (and I'm too lazy to create any))

I don't understand what you mean by this sentence:
But I can't figure out how to make the targets revolve around the gun.
What targets are you talking about? Please try to be more specific about what targets your are referring to, what gun you are referring to, and what exactly you mean by revolving (I know what revolving means, but I don't know how YOU want your targets to revolve).

However, here are a few pointers:
I see that you definitely don't need these lines:
		 		FreeEntity xo
		 		FreeEntity yo
				FreeEntity zo

		 		FreeEntity xo2
		 		FreeEntity yo2
				FreeEntity zo2
in your PutBadmans function. Those variables contain entity locations not entity handles. You are just freeing random entities which either actually does free random entities in your game or they (the FreeEntity commands) just fail (which is what probably happens 99.999999% of the time).

You definitely don't need to set the entitytypes every frame of you game:
	For hit.badman = Each badman
		EntityType hit\entit,1
		PositionEntity hit\entit,hit\xo,hit\yo,hit\zo
	Next
		For hat.badman2 = Each badman2
		EntityType hat\entit2,1
		PositionEntity hat\entit2,hat\xo2,hat\yo2,hat\zo2
	Next
Do that when you initialize your badmen like in this For-Next loop:
For tt= 1 To 20
	hit.badman = New badman ;					where did HIT come from... I made it up. I could
	hit\entit = CopyEntity (badman) ;			have used a letter or any word not reserved
	hit\xo=Rnd(-100,100) ;						NOTE " \ " not " / "
	hit\yo=Rnd(-50,50)
	hit\zo=Rnd(100,150) ; 						Since B3D uses both + and - nums I had to use Rnd
	EntityType hit\entit,1
Next ;										That way my badmans are spread out evenly-ish


I suspect that this code::
	For hit.badman = Each badman
		PositionEntity hit\entit,hit\xo,hit\yo,hit\zo
	Next
is intended to not only set the badmens locations but you actually want to use it to UPDATE the badmens locations? If that's the case then you need to add some line in there something like:
	For hit.badman = Each badman
		hit\xo = hit\xo + rnd(-.1, .1)
		hit\yo = hit\yo + rnd(-.1, .1)
		hit\zo = hit\zo + rnd(-.1, .1)
		PositionEntity hit\entit, hit\xo, hit\yo, hit\zo
	Next
Note however, that using the above code will just cause them to wiggle around a bit. But since I don't know what you mean by revolve, I don't know how you actually want them to move.

Wolron,

Your revised type thingees have helped me clean up my code. I appreciate that.

How I want them to rotate:

Imagine you hold an open umbrella. the metal stem is in your hand. your hand is the gun.

The metal posts that support the fabric out to the edges and those little pegs that plug the fabric.

Well those little pegs are the targets.

Rotate the umbrella and watch the pegs move aroung the center (which is the pole and the GUN)

Probably not much clearer... I am going to keep trying.

I saw an example a few weeks ago when someone had a camera orbiting around a sun... like that... in reverse. The gun or camera remains in the middle and all revolves around it.

-RZ

Yes, just place a pivot at the point you want your targets to rotate around, then parent your targets to that pivot. Now you can TurnEntity the pivot to make all the targets rotate however you like.

That makes sense... AND I didn't know that... Wow 2 with one help! : )

UPDATE... OK pivot I got it...