grenades

Blitz3D Forums/Blitz3D Beginners Area/grenades

I have a game in which I need grenades. I can get one grenade to work(i.e. it flies in an arch and explodes on impact w/ another object.), but I need multiple grenades. I don't want to do the code over and over to accomodate each grenade, but I don't know how to use a type, array, etc. to do it. Any help?

http://www.blitzbasic.com/b3ddocs/command.php?name=Function&ref=2d_cat

This might help

http://www.blitzbasic.com/Community/posts.php?topic=72799

how does the c\phys pivot create movement?

TranslateEntity c\Mesh,EntityX(c\phys),EntityY(c\phys),EntityZ(c\phys)

It's the same as having xspeed, yspeed and zspeed, except all rolled into one variable, with the additional advantage of being able to rotate it and move it in a given direction without having to worry about sine/cosine maths.

ok, I see. now that the throwing-multiple-grenades function is good, how can I set up an explosion for each grenade?

Same principle.

Create a type for your explosion.
Create a function to create an instance (like the newcube() function)
and finally
Create a function to update all explosions (like the updatecubes() function)

Your NewExplosion function would probably accept either x,y,z co-ords and a size

ie
Function NewExplosion(x#,y#,z#,size)
	e.explosion = New explosion
	blah
	blah
	blah
End Function

Function UpdateExplosions()
	For e.explosion = Each explosion
		blah
		blah
	Next
End Function


two problems so far (w/ out the xplode). I can't move the point the grenades are thrown from or set a limit on the amount of grenades you can throw, they just keep on coming. once these are fixed there wont be a problem w/ the explosions.

i don't understand your problem, code please :)

Because I'm a caring sharing loving kinda guy...

Graphics3D 640,480,32,2

SetBuffer BackBuffer()


camera = CreateCamera()
light = CreateLight()

MoveEntity camera,0,10,-15

; create cannon
base = CreateCylinder(12)
ScaleMesh base,3,1,3
PositionMesh base,0,.5,0
s1 = CreateCube()
s2 = CreateCube()
ScaleMesh s1,.5,2,1
ScaleMesh s2,.5,2,1
PositionMesh s1,-1.5,3.5,0
PositionMesh s2,1.5,3.5,0

AddMesh(s1,base)
AddMesh(s2,base)
FreeEntity s1
FreeEntity s2
EntityColor base,100,150,200

barrel= CreateCylinder(12)
RotateMesh barrel,90,0,0
b2 = CreateCylinder(12)
ScaleMesh b2,.8,1,.8
RotateMesh b2,90,0,0
PositionMesh b2,0,0,2
AddMesh(b2,barrel)
FreeEntity b2

Muzzle = CreatePivot(Barrel)
PositionEntity Muzzle,0,0,4

PositionEntity barrel,0,4,0
EntityParent barrel,base
EntityColor barrel,200,150,100

; Make Ground
Ground = CreatePlane()
i = CreateImage(64,64)
SetBuffer(ImageBuffer(i))
Color 0,0,255:Rect 0,0,32,32:Rect 32,32,32,32
Color 0,100,255:Rect 32,0,32,32:Rect 0,32,32,32
checker = CreateTexture(64,64)
CopyRect 0,0,64,64,0,0,ImageBuffer(i),TextureBuffer(checker )
FreeImage i
ScaleTexture checker ,50,50
EntityTexture ground,checker 

xspin# = 0
yspin# = 0

Repeat

	xspin = xspin - (MouseXSpeed()/15)
	yspin = yspin - (MouseYSpeed()/15)
	
	If xspin < -45 Then xspin = -45
	If xspin > 45 Then xspin = 45
	
	If yspin > 0 Then yspin = 0
	If yspin < -80 Then yspin = -80
	
	MoveMouse 320,240
	
	RotateEntity base,0,xspin,0
	RotateEntity barrel,yspin,0,0
	
	If MouseHit(1) Then
		NewGrenade(Muzzle)
	EndIf
	
	updategrenade

RenderWorld

Flip

Until KeyHit(1)


Type Grenade
	Field Mesh
	Field Phys
	Field Life
	Field Explode
End Type

Function NewGrenade(Launcher)
	C.Grenade = New Grenade
	C\Mesh = CreateSphere(6)
	EntityColor C\Mesh,Rand(100,255),Rand(100,255),Rand(100,255)
	C\Phys = CreatePivot()
	PositionEntity C\Mesh,EntityX(Launcher,True),EntityY(Launcher,True),EntityZ(Launcher,True)
	RotateEntity c\Phys,EntityPitch(Launcher,True),EntityYaw(Launcher,True),EntityRoll(Launcher,True)
	MoveEntity c\phys,0,0,1
	EntityShininess c\mesh,1
	c\Life = 150
	c\explode = True
End Function

Function NewExplode(Launcher)
	For n=1 To 30
		C.Grenade = New Grenade
		C\Mesh = CreateCube()
		EntityColor C\Mesh,Rand(1,2)*100,Rand(1,2)*50,0
		C\Phys = CreatePivot()
		PositionEntity C\Mesh,EntityX(Launcher,True),EntityY(Launcher,True),EntityZ(Launcher,True)
		ScaleEntity c\mesh,Rnd(.1,1),Rnd(.1,1),Rnd(.1,1)
		RotateEntity c\mesh,Rand(360),Rand(360),Rand(360)
		PositionEntity c\Phys,Rnd(-.5,.5),Rnd(-1,1),Rnd(-.5,.5)
		EntityShininess c\mesh,0
		EntityFX c\mesh,1
		EntityBlend c\mesh,1
		c\Life = Rand(20,100)
		c\explode = False		
	Next
End Function

Function UpdateGrenade()
	For c.Grenade = Each Grenade
		TranslateEntity c\Mesh,EntityX(c\phys),EntityY(c\phys),EntityZ(c\phys)
		
		TranslateEntity c\phys,0,-.01,0
		
		If EntityY(c\mesh)<.5 Then
			PositionEntity c\mesh,EntityX(c\mesh),.5,EntityZ(c\mesh)
			TranslateEntity c\phys,0,-EntityY(c\phys)*1.5,0			
		EndIf
		
		c\life = c\life - 1 
		
		If c\life = 0 Then
			If c\explode Then NewExplode(c\mesh)
			FreeEntity c\mesh
			FreeEntity c\phys
			Delete c
		EndIf
	Next
End Function


Not to sound idiotic, but, rob farley, how did you do that. I modified the code so that the launcher can move, and if still fires properly from the launcher. ( I don't have time to sift thru code:) ) I got a way to have a grenade-limit, so I'm good there

You don't have time to sift through the code eh... Sounds more like you can't be arsed. Without sifting through the code you won't learn... however...

There is a pivot (called muzzle) parented to the end of the barrel. The handle of the muzzle entity is passed to the NewGrenade function as 'Launcher' which exposes it's properties within the function.

The start position of the grenade is then placed at the xyz of the that, likewise the trajectory of the grenade is picked up from the yaw, pitch and roll of the same passed entity.

Likewise when the NewExplode is called the handle of the grenade mesh (c\mesh) is passed to it so the explosion happens at the point where the grenade is. Then the explosion itself is handled by exactly the same code which handles the updating of the grenades. The explosion is essentially just a bunch of grenades.

Damn I'm being helpful today... I need to stop this before I make a habit of it.

I might point out that I didn't have time to sift thru code at the time I posted. anyway, the world needs more helpful people, there's such a lack of them. sorry for using so much of your time. :)

;)@Rob