bullet help

Blitz3D Forums/Blitz3D Beginners Area/bullet help

CHALLENGER426(Posted 2008) [#1]
i have been working on my 1st game i have bullets that while appear and disappear, but i cannot get them to shoot...actually move when they are shot...thanks.....
;GUN/SHOOTING
Dim bullet(i)

;bullets
bullet(i)=CreateSphere()
EntityColor bullet(i),100,100,100
ScaleEntity bullet(i),.2,.2,.2
HideEntity bullet(i)

Function Fire()
End Function.......

(........ later in game code)
..........;fire
If KeyHit(57)Then
ShowEntity bullet(i)
PositionEntity bullet(i), EntityX(car,1),EntityY(car,1),EntityZ(car,1)
MoveEntity bullet(i), 0,-1,1.5
RotateEntity bullet(i),EntityPitch#(car,1)-35,EntityYaw#(car,1),EntityRoll#(car,1)
EntityColor bullet(i),Rand(0,255),Rand(0,255),Rand(0,255)
t=t+1
EndIf


Warner(Posted 2008) [#2]
Well, there is something not right about the way you're using the array.
I've placed comments in the code you've posted.
Btw, when posting code, is could be handy to use the following tags to place a codebox around it.

Not sure if I forgot anything, I find it usually more easy to look at working code.


CHALLENGER426(Posted 2008) [#3]
Thanks for the help , but when i go to shoot, spheres just appear but don't go anywhere, is it because i have "Const GRAVITY#=-.01" for the car/tank that is "shooting" the spheres?


Warner(Posted 2008) [#4]
Not sure, I didn't see that in your example ? But based on the above code - most likely it is because the array youre using isn't holding the bullets. For the bullets to appear, it isn't necesarry to have a valid handle to them. But you need a handle to be able to move them.
For instance, normally, you would create a cube like this:
cube = CreateCube()

Then you can move/turn/scale the cube by using stuff like this:
MoveEntity cube, 0, 0, 1

However, if you type just this:
CreateCube()

It will still create a cube, but you can't move it. There is no handle to it.
Same goes for this, which is basically what you were doing in the code in your first post:
cube = CreateCube()
cube = CreateCube()

After this, you can only acces the second cube. The first handle is overwritten, but the cube that is was refering to still exists.
In the code, you werent' changing 'i' anywhere, so it would just reuse the same bullet() again and again.
It would be easiest if you make a small working example based on the above code. It would be more easy to fix it or at least spot the problem.


CHALLENGER426(Posted 2008) [#5]
heres the code for the game, its not near done, and its my first game...Thanks again for the help...
;Welcome
welcome()
Function welcome()
Graphics 1020,740
screen=LoadImage("title.jpg")
DrawImage screen,0,0
While Not KeyDown(1) 
If KeyDown(28) Then Return 
Wend
End Function 
Graphics3D 640,480
HidePointer

Graphics3D 640,480
SetBuffer BackBuffer()


Const GRAVITY#=-.01

Const BODY=1,WHEEL=2,SCENE=3,Water=4,CAM=5
Graphics3D 1024,768
SetBuffer BackBuffer()



;Creating a plane and texture

terr=CreateTexture(32,32,8)
ScaleTexture terr,10,10
SetBuffer TextureBuffer(terr)
Color 0,0,0:Rect 0,0,32,32
Color 0,0,0:Rect 0,0,32,32,False
SetBuffer BackBuffer()




;create cloud planes
tex=LoadTexture( "cloud_2.bmp",3 )
ScaleTexture tex,1000,1000
p=CreatePlane()
EntityTexture p,tex
EntityFX p,1
PositionEntity p,0,70,0
p=CopyEntity( p )
RotateEntity p,0,0,180
	

m=CreateMesh()

	;front face
	b=LoadBrush( file$+"sky_FR.bmp",49 )
	s=CreateSurface( m,b )
	AddVertex s,-1,+1,-1,0,0:AddVertex s,+1,+1,-1,1,0
	AddVertex s,+1,-1,-1,1,1:AddVertex s,-1,-1,-1,0,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;right face
	b=LoadBrush( file$+"sky_LF.bmp",49 )
	s=CreateSurface( m,b )
	AddVertex s,+1,+1,-1,0,0:AddVertex s,+1,+1,+1,1,0
	AddVertex s,+1,-1,+1,1,1:AddVertex s,+1,-1,-1,0,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;back face
	b=LoadBrush( file$+"sky_BK.bmp",49 )
	s=CreateSurface( m,b )
	AddVertex s,+1,+1,+1,0,0:AddVertex s,-1,+1,+1,1,0
	AddVertex s,-1,-1,+1,1,1:AddVertex s,+1,-1,+1,0,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;left face
	b=LoadBrush( file$+"sky_RT.bmp",49 )
	s=CreateSurface( m,b )
	AddVertex s,-1,+1,+1,0,0:AddVertex s,-1,+1,-1,1,0
	AddVertex s,-1,-1,-1,1,1:AddVertex s,-1,-1,+1,0,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;top face
	b=LoadBrush( file$+"sky_UP.bmp",49 )
	s=CreateSurface( m,b )
	AddVertex s,-1,+1,+1,0,1:AddVertex s,+1,+1,+1,0,0
	AddVertex s,+1,+1,-1,1,0:AddVertex s,-1,+1,-1,1,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b

	ScaleMesh m,1000,1000,1000
	FlipMesh m
	EntityFX m,9
	


;Load terrain
	terr=LoadTerrain( "ground.jpg" )
	ScaleEntity terr,20,800,20
	PositionEntity terr,-20*512,0,-20*512
	EntityFX terr,1
	EntityType terr,10

	;apply textures to terrain	
	tex1=LoadTexture( "grass.jpg",1 )
	ScaleTexture tex1,10,10
	tex2=LoadTexture( "muddy.jpg" )
	ScaleTexture tex2,TerrainSize(terr),TerrainSize(terr)
	EntityTexture terr,tex1,0,0
	EntityTexture terr,tex2,0,1
	
	;and ground plane
	plane=CreatePlane()
	ScaleEntity plane,20,1,20
	PositionEntity plane,-20*512,0,-20*512
	EntityTexture plane,tex1,0,0
	EntityOrder plane,3
	EntityFX plane,1
	EntityType plane, SCENE 
	

;create water plane
	tex=LoadTexture( "water-2_mip.bmp",3 )
	ScaleTexture tex,10,10
	p=CreatePlane()
	EntityTexture p,tex
	EntityBlend p,1
	EntityAlpha p,.75
	PositionEntity p,0,10,0
	EntityFX p,1
EntityType p, WATER 



terrain=LoadTerrain("ground.jpg")
ScaleEntity terrain,5,100,5
PositionEntity terrain,-500,0,-500

Texture=LoadTexture("grassy.jpg")
EntityTexture terrain,texture

EntityType terrain,SCENE

;plane
	
plane=LoadMesh("747.x")
ScaleMesh plane,1,1,-1
FlipMesh plane
FitMesh plane,100,100,-50,100,50,100
PositionEntity plane,150,-110,-100
EntityShininess plane,-10
EntityType plane,SCENE
RotateEntity plane, -5,8,0

;debris
a=LoadMesh("wcrate1.3ds")
ScaleMesh a,1,1,-1
FlipMesh a
FitMesh a,5,5,-5,5,5,5
PositionEntity a,300,-2,-30
EntityShininess a,-10
EntityType a,SCENE
RotateEntity a, -5,8,0

a2=LoadMesh("wcrate1.3ds")
ScaleMesh a2,1,1,-1
FlipMesh a2
FitMesh a2,5,5,-5,5,5,5
PositionEntity a2,280,-5,-30
EntityShininess a2,-10
EntityType a2,SCENE
RotateEntity a2, -5,8,0

a3=LoadMesh("wcrate1.3ds")
ScaleMesh a3,1,1,-1
FlipMesh a3
FitMesh a3,5,5,-5,5,5,5
PositionEntity a3,270,0,-20
EntityShininess a3,-10
EntityType a3,SCENE
RotateEntity a3, -5,8,0

a4=LoadMesh("wcrate1.3ds")
ScaleMesh a4,1,1,-1
FlipMesh a4
FitMesh a4,5,5,-5,5,5,5
PositionEntity a4,250,-6,-10
EntityShininess a4,-10
EntityType a4,SCENE
RotateEntity a4, -5,8,0

;tank
car=LoadMesh("warrior.3ds")
ScaleMesh car,1,1,-1
FlipMesh car
FitMesh car,-1.5,-1,-3,3,2,6
PositionEntity car, 0,70,0
EntityShininess car,.1
EntityType car,BODY

Global wheels[4]

cnt=1
For z#=1.5 To -1.5 Step -3
For x#=-1 To 1 Step 2
	wheels[cnt]=CreateSphere( 8,car )
	EntityColor wheels[cnt],0,0,0
	EntityAlpha wheels[cnt],.5
	ScaleEntity wheels[cnt],.5,.5,.5
	EntityRadius wheels[cnt],.5
	PositionEntity wheels[cnt],x,0,z
	EntityType wheels[cnt],WHEEL
	cnt=cnt+1
Next
Next


;GUN/SHOOTING
Dim bullet(100)

	;bullets
	orgbullet=CreateSphere()
	EntityColor orgbullet,100,100,100
	ScaleEntity orgbullet,.2,.2,.2
	HideEntity orgbullet

	Function Fire()
	End Function


;light
light=CreateLight()
TurnEntity light,45,45,0	

target=CreatePivot( car )
PositionEntity target,0,5,-12

camera=CreateCamera()
CameraClsColor camera,0,128,255

CameraFogColor camera,100,192,255
CameraFogRange camera,1000,3000




speed#=0
x_vel#=0:prev_x#=EntityX( car )
y_vel#=0:prev_y#=EntityY( car )
z_vel#=0:prev_z#=EntityZ( car )





;ENEMIES
Dim ducks(5)
For x=1 To 1
ducks(x)=LoadMesh("fighter.3ds")
PositionEntity ducks(x),x*15,60,90
EntityColor ducks(x),255,255,0
ScaleEntity ducks(x),0.1,0.1,0.1
EntityType ducks(x), type_gallery
EntityRadius ducks(x),3
Next
Dim seahorse(5)
For x=1 To 1
seahorse(x)=LoadMesh("stealth.3ds")
PositionEntity seahorse(x),x*6,40,45
EntityColor seahorse(x),255,102,0
ScaleEntity seahorse(x),0.03,0.03,0.03
EntityType seahorse(x),type_gallery
EntityRadius seahorse(x),3
Next
Dim Donkey(5)
For x=1 To 1
donkey(x)=LoadMesh("solarsail.3ds")
PositionEntity donkey(x),x*5,50,80
EntityColor donkey(x),204,204,204
ScaleEntity donkey(x),.003,.003,.003
EntityType donkey(x),type_gallery
EntityRadius donkey(x),3
Next
Dim Flamingo(5)
For x=1 To 1
flamingo(x)=LoadMesh("fsaucer1.3ds")
PositionEntity flamingo(x),x*6,45,0
EntityColor flamingo(x),0,0,0
ScaleEntity flamingo(x),0.003,0.003,0.003
EntityType flamingo(x),type_gallery
EntityRadius flamingo(x),3
Next
Dim Dolphin(5)
For x=1 To 1
dolphin(x)=LoadMesh("mak_robotic.3ds")
PositionEntity dolphin(x),x*6,45,0
EntityColor dolphin(x),102,153,255
ScaleEntity dolphin(x),0.3,0.3,0.3
EntityType dolphin(x),type_gallery
EntityRadius dolphin(x),3
Next
Dim snake(5)
For x=1 To 1
snake(x)=LoadMesh("fsaucer1.3ds")
PositionEntity snake(x),x*16,60,10
EntityColor snake(x),153,255,102
ScaleEntity snake(x),0.06,0.06,0.06
EntityType snake(x),type_gallery
EntityRadius snake(x),5
Next





;collisions

Collisions BODY,SCENE,2,3
Collisions WHEEL,SCENE,2,3
Collisions  BODY, WATER,2,5
Collisions CAM,SCENE,3,2
Collisions WHEEL,WATER,2,5


While Not KeyHit(1)

	;align car to wheels
	zx#=(EntityX( wheels[2],True )+EntityX( wheels[4],True ))/2
	zx=zx-(EntityX( wheels[1],True )+EntityX( wheels[3],True ))/2
	zy#=(EntityY( wheels[2],True )+EntityY( wheels[4],True ))/2
	zy=zy-(EntityY( wheels[1],True )+EntityY( wheels[3],True ))/2
	zz#=(EntityZ( wheels[2],True )+EntityZ( wheels[4],True ))/2
	zz=zz-(EntityZ( wheels[1],True )+EntityZ( wheels[3],True ))/2
	AlignToVector car,zx,zy,zz,1
	
	zx#=(EntityX( wheels[1],True )+EntityX( wheels[2],True ))/2
	zx=zx-(EntityX( wheels[3],True )+EntityX( wheels[4],True ))/2
	zy#=(EntityY( wheels[1],True )+EntityY( wheels[2],True ))/2
	zy=zy-(EntityY( wheels[3],True )+EntityY( wheels[4],True ))/2
	zz#=(EntityZ( wheels[1],True )+EntityZ( wheels[2],True ))/2
	zz=zz-(EntityZ( wheels[3],True )+EntityZ( wheels[4],True ))/2
	AlignToVector car,zx,zy,zz,3
	
	;calculate car velocities	
	cx#=EntityX( car ):x_vel=cx-prev_x:prev_x=cx
	cy#=EntityY( car ):y_vel=cy-prev_y:prev_y=cy
	cz#=EntityZ( car ):z_vel=cz-prev_z:prev_z=cz
	

	cnt=1
	For z=1.5 To -1.5 Step -3
	For x=-1 To 1 Step 2
;		PositionEntity wheels[cnt],0,0,0
;		ResetEntity wheels[cnt]
		PositionEntity wheels[cnt],x,-1,z
		cnt=cnt+1
	Next
	Next
;Moving the Targets
For a=1 To 1
MoveEntity ducks(a),0.1,0,0
MoveEntity seahorse(a),.2,0,0
MoveEntity donkey(a),0,0,.1
MoveEntity flamingo(a),0.1,0,.2
MoveEntity snake(a),-0.2,0,0
MoveEntity dolphin(a),.1,0,0
Next

	;move car
	
	If KeyDown(203) TurnEntity car,0,3,0
	If KeyDown(205) TurnEntity car,0,-3,0
	If EntityCollided( car,SCENE )
		If KeyDown(200)
			speed=speed+.02
			If speed>.5 speed=.5
		Else If KeyDown(208)
			speed=speed-.02
			If speed<-.5 speed=-.5
		Else
			speed=speed*.9
		EndIf
		MoveEntity car,0,0,speed
		TranslateEntity car,0,GRAVITY,0
	Else
		TranslateEntity car,x_vel,y_vel+GRAVITY,z_vel
	EndIf
If KeyDown (32) Collisions BODY,SCENE,2,3
If KeyDown (32) Collisions WHEEL,SCENE,2,3

Cls


	
	;get mouse
	mxspd#=MouseXSpeed()
	myspd#=MouseYSpeed()
	MoveMouse 400,300
	
	;turn camera
	;note, we change only camera pitch as it's attached to the pivot of the player which
	;only changes it's yaw. This for various reasons makes it easy to control.
	
	pitch#=pitch#+myspd*0.1
	yaw#=yaw#-mxspd*0.1
	RotateEntity camera,pitch,0,0
	RotateEntity camera,0,yaw,0
;fire
If KeyHit(57)Then
bullet(i)=CopyEntity(orgbullet)
ShowEntity bullet(i)
PositionEntity bullet(i), EntityX(car,1),EntityY(car,1),EntityZ(car,1)
MoveEntity bullet(i), 0,-1,1.5
RotateEntity bullet(i),EntityPitch#(car,1)-35,EntityYaw#(car,1),EntityRoll#(car,1)
EntityColor bullet(i),Rand(0,255),Rand(0,255),Rand(0,255)
t=i+1
EndIf
For t=1 To i
	MoveEntity Bullet(t),0,0,.1
Next


	;Help Create Game
If KeyDown(31) HideEntity car 
If KeyDown(32) ShowEntity car
; Toggle wireframe enable value between true and false when spacebar is pressed
If KeyHit( 17 )=True Then enable=1-enable
; Enable/disable wireframe rendering
WireFrame enable

;update camera
	If speed>=0	
		dx#=EntityX( target,True )-EntityX( camera )
		dy#=EntityY( target,True )-EntityY( camera )
		dz#=EntityZ( target,True )-EntityZ( camera )
		TranslateEntity camera,dx*.1,dy*.1,dz*.1
	EndIf
	PointEntity camera,car
	
 
;Pausing the game
If KeyDown(25) Then
Cls
pause=LoadImage("image1.jpg")
DrawImage pause,0,0
Flip
WaitMouse
EndIf

UpdateWorld
RenderWorld
Flip
Wend
End



Warner(Posted 2008) [#6]
It is difficult to solve it without being able to actually run the code itself, but I did notice in this part:
;fire
If KeyHit(57)Then
bullet(i)=CopyEntity(orgbullet)
ShowEntity bullet(i)
PositionEntity bullet(i), EntityX(car,1),EntityY(car,1),EntityZ(car,1)
MoveEntity bullet(i), 0,-1,1.5
RotateEntity bullet(i),EntityPitch#(car,1)-35,EntityYaw#(car,1),EntityRoll#(car,1)
EntityColor bullet(i),Rand(0,255),Rand(0,255),Rand(0,255)
t=i+1 <------------this should be i=i+1
EndIf
For t=1 To i
	MoveEntity Bullet(t),0,0,.1
Next

That "t=i+1" should be "i=i+1".