Bullet and tree problems
Blitz3D Forums/Blitz3D Programming/Bullet and tree problems
| ||
Hi Everyone, I'm wasting sooooo much time on this it's not funny. I was hoping someone might have a few ideas for helping me with 2 things.. Firstly my shooting code is bugged.. I fire, but the bullets will stray if I hold the left or right keys. Secondly, How the heck do I turn my tree generator into a function as I'd like to be able to start the level without it still being created around me :P Any help appreciated, Cheers!. ; Going for a large map, survival rpg game here... early days... ; I have written a lot of notes to help anyone that wants ; to tinker so far... but I'm guessing 95% of PPL that even see this ; would laugh there butts off at how dodgy this is anyway... please go easy Im new at this.! ; OK... these 2 things I want to get working and understand.. ;1) bullets to fire straight, and not move as I hold the right and left arrows. ;2) How to make the trees a function, and not live within the main loop... I can see them ; still spawning when I'm walking around. ; Better lighing... and some kind of fog system to..well I guess blur out the horizon a bit. ; I have got the camerafog a few lines below going but its not really what I'm looking for. ; Things I THINK I already now understand and will put in ASAP. ;1) Better lighting..I'm gunna try and get a Sun in here eventually..the day/night thing ;2) AI.. I have it worked out..kinda.. just gotta get the shooting down before I dive in to much there. Graphics3D 1920,1080,0 SetBuffer BackBuffer() camera=CreateCamera() PositionEntity camera,1,1,-100 CameraFogMode camera,1 CameraFogRange camera,10,100 ;.................................................. ;create player model player=CreateCube() ScaleEntity player,.15,.4,.15 EntityColor player,0,0,255 PositionEntity player,100,.3,100 phead=CreateSphere() ScaleEntity phead,.2,.2,.2 EntityColor phead,0,0,255 ;my basic Avatar here until something better comes along. PositionEntity phead,100,1,100 EntityParent phead,player pgun=CreateCube() ScaleEntity pgun,.01,.03,.2 EntityColor pgun,200,200,255 PositionEntity pgun,100.2,.5,100.2 EntityParent pgun,player emove=0 ;................................................. ; create enemy model enemy=CreateCube() ScaleEntity enemy,.15,.4,.15 EntityColor enemy,255,0,0 PositionEntity enemy,10,20,20 ehead=CreateSphere() ScaleEntity ehead,.2,.2,.2 EntityColor ehead,255,0,0 PositionEntity ehead,10,20.7,20 EntityParent ehead,enemy egun=CreateCube() ScaleEntity egun,.01,.03,.2 EntityColor egun,200,200,255 PositionEntity egun,20.2,.5,20.2 EntityParent egun,enemy ;................................................ ;create tree tree=LoadMesh("Tree3.3ds") ;grab any tree.3ds model and throw it in the exe folder ScaleEntity tree,1,1,1 ;light world light=CreateLight() RotateEntity light,90,0,0 ;Load terrain terrain=LoadTerrain( "heightmap.bmp" ) ;grab a heightmap pic and put it in the same folder. ;create water plane water=CreatePlane() ScaleEntity water,100,100,100 ; really gotta put a texture here and raise and lower the height PositionEntity water,1,4,1 ; of it ~slightly~ as the programs running... gives a cool EntityColor water,0,0,255 ;tidal wave effect for cheapskates like me :D ;Scale terrain ScaleEntity terrain,2,100,2 ;x,height,and z size. ;Texture terrain ;grass=LoadTexture( "lushgrass.bmp" ) ; gave up on using one solitary texture as the terraincolore ;EntityTexture terrain,grass,0,1 ;function below the main loop gives more variety. ;ScaleTexture grass,5,5 TerrainColore (terrain) ; love this, kudos to whoever wrote the code... I have been looking thru ; so much code in the last few days I have lost track of where it came from. ;create sky sky=CreateSphere(50) ;higher the number the rounder it is. PositionEntity sky,100,1,100 ;same co-ords as player to start FlipMesh sky ;display the sky512.jpg on the inside of the mesh so we can see it. ScaleEntity sky,500,500,500 skytex=LoadTexture("sky512.jpg") ; if you dont know, put a sky picture in jpg format in the folder EntityTexture sky,skytex ; with this file ..label it sky512.jpg and it should work for you. EntityFX Sky,9; this 9 seems to light the flip-meshed sky so I can see it, instead of being a black void. tr=0 ;setting my initial tree count to zero ; probably could use a for next below come to think of it... but it holds together.. ; hear me baby hold together.! ; main loop XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX While Not KeyDown( 1 ) If KeyDown( 203 )=True Then x#=x#-0.1 If KeyDown( 205 )=True Then x#=x#+0.1 If KeyDown( 208 )=True Then y#=y#-0.1 If KeyDown( 200 )=True Then y#=y#+0.1 ;yada yada yada! If KeyDown( 44 )=True Then z#=z#-0.1 If KeyDown( 30 )=True Then z#=z#+0.1 If KeyDown( 205 )=True Then TurnEntity player,0,-1,0 If KeyDown( 203 )=True Then TurnEntity player,0,1,0 If KeyDown( 208 )=True Then MoveEntity player,0,0,-0.1 If KeyDown( 200 )=True Then MoveEntity player,0,0,0.1 If KeyHit(57) ;Space to fire. If bullet ; kill the current bullet before firing another... dodgy as hell! FreeEntity bullet EndIf ;PointEntity enemy, Player Bullet=CreateCube(pgun) ;create the bullet at the players gun location. EntityColor bullet,222,0,0 ScaleEntity Bullet, 0.5, 0.5, 0.5 PositionEntity bullet,0,y#-4,0 ;set bullet fire location..WHY does the bullet move a bit when ; ...I hold down the left or right arrow... I really want to get this ; working independently so it at least LOOKS right. :( ; Any help with making it independant from these keys would be awesome. EntityParent Bullet,pgun EndIf If Bullet MoveEntity Bullet, 0, 0, 1 ;3rd number is bullet speed EndIf If bullet If bullet And EntityDistance(player, bullet)>200;bullet range from player before destroying itself. FreeEntity bullet EndIf EndIf x#=EntityX(player) y#=EntityY(player) ; ok, the original author of this code used a camera z#=EntityZ(player) ; I changed it to the player and copied similar below for the enemies ; and trees.. easy way for me to understand how to find the Y value xx#=EntityX(enemy) ; of the map position of where I wanted to place an item,,,player,tree etc yy#=EntityY(enemy) ; works for now. zz#=EntityZ(enemy) tx#=EntityX(tree) ty#=EntityY(tree) tz#=EntityZ(tree) emove=emove+1 xx#=xx#+.05;move enemy test... threw in 1 enemy,,, he sucks.. ignore him! zz#=zz#+.05 If emove>=5000;reset the lil bastard after he walks a certain distance! xx#=10 zz#=20 emove=0 EndIf terra_y#=TerrainY(terrain,x#,y#,z#) ;...im betting I could just use colissions on my player and the terra_yy#=TerrainY(terrain,xx#,yy#,zz#); terrain instead of going to all this trouble... but I havn't PositionEntity player,x#,terra_y#+.5,z#; tried yet... bahh it works for now, and at least I can PositionEntity enemy,xx#,terra_yy#+.4,zz# ; understand it. PositionEntity camera,x#,terra_y#+10,z#-25 ; scatter trees ........................... I Have Tried to make this a function ; THIS RAGHHH!!>.. as I THINK it would be the right way to do it. ; but am too stupid to figure it out... 5hrs mind you!. ; I suck at this but its a lot of fun trying. If tr<300 ;how many trees? tx#=Rnd(500) ;random x plane tree pos tz#=Rnd(500) ;random z plane tree pos terra_ty#=TerrainY(terrain,tx#,ty#,tz#) If terra_ty#>15 ;Keep trees within certain height.. this I am proud of... yep all me baby!. If terra_ty#<35 ; so the trees dont spawn in the water or to high on the scenery. newtree = CopyEntity (tree) height=1.5;+Rnd(1) ...... I have tinkered with randomizing the scales here..this'll do for now tho. C=Rnd(255) ScaleEntity tree,1,height,1 EntityColor tree,0,C,0 ;......added this to change tree color a bit PositionEntity newtree,tx#,terra_ty#+height-.2,tz# tr=tr+1 ; add a tree EndIf EndIf EndIf ;update the skybox PositionEntity sky,x#,1,z# TurnEntity sky,0,0,.05 ; spin the sky slowly, just need a couple of double rainbows ..kidding. RenderWorld ; Practice to get text info on screen whooot! Text master. Text 0,0,"Use cursor keys to move about the terrain, Space to fire like there's no tomorrow" Text 0,20,"player height Y: "+terra_y# Text 0,30,"Enemy height Y: "+terra_yy# Flip Wend End ; XXXXXXXXXXXXXXXXXXXX End Loop XXXXXXXXXXXXXXXXXXXXXXX ;... yep blaitantly nicked the below code, and by some mirical of god fluked getting it to work ; with the reset of this mess :P originally used textures on the terrain, but I think I like this more. ; at least its easy to understand.... Function TerrainColore(Terrain%) Local px%,py% Tsz = TerrainSize (Terrain) Tex = CreateTexture (Tsz,Tsz):ScaleTexture(Tex,Tsz,Tsz) CBuf%= GraphicsBuffer () TBuf%= TextureBuffer (Tex):SetBuffer(TBuf):LockBuffer(TBuf) For px = 0 To Tsz-1 For py = 0 To Tsz-1 AH_T#=TerrainHeight(Terrain,px,py) If AH_T<.08 ; Sol->Mer => Sol->Sable Dh#=AH_T*1.0/.08 R=080+030*Dh ; try this.... r=rnd(120)+030*dh ..etc for variety, pretty neat. G=050+030*Dh B=010+040*Dh ; fin = 110 / 080 / 050 ElseIf AH_T<.1 ; Sable Dh#=(AH_T-.08)*1/.02 R=110+080*Dh G=080+080*Dh B=050+070*Dh ; fin = 190 / 160 / 120 ElseIf AH_T<.15 ; Herbe Dh#=(AH_T-.1)*1/.05 R=190-170*Dh G=160-120*Dh B=120-110*Dh ; fin = 020 / 040 / 010 ElseIf AH_T<.7 ; Roche Dh#=(AH_T-.17)*1/.55 R=020+080*Dh ; Nice code for beginners like me to G=040+060*Dh ; fumble thru, its pretty easy to follow B=010+040*Dh ; fin = 120 / 100 / 050 ; and I like to tinker with the colors. Else ; neige Dh#=(AH_T-.72)*1/.3 R=120+020*Dh G=100+050*Dh B=050+160*Dh ; fin = 180 / 150/ 210 EndIf If R<0 R=0 If G<0 G=0 If B<0 B=0 WritePixelFast px,Tsz-py-1,R Shl(16) + G Shl(8) + B Next Next UnlockBuffer(TBuf):SetBuffer(CBuf) EntityTexture (Terrain,Tex,0,0):FreeTexture(Tex) End Function |
| ||
Hello, your bullets are turning with the player because you parent them to the gun :Bullet=CreateCube(pgun) EntityColor bullet,222,0,0 ScaleEntity Bullet, 0.5, 0.5, 0.5 PositionEntity bullet,0,y#-4,0 EntityParent Bullet,pgunThis will make the bullet independant : Bullet=CreateCube() EntityColor bullet,222,0,0 ScaleEntity Bullet, 0.05, 0.05, 0.05 PositionEntity bullet,EntityX(pgun,1),EntityY(pgun,1),EntityZ(pgun,1) RotateEntity bullet,EntityPitch(pgun,1),EntityYaw(pgun,1),EntityRoll(pgun,1)For the trees, in your code you're just creating one tree per loop, until you reach 300 trees, what you want is to create all the trees at once, here is a function that you can call before the loop : Function CreateTrees() For i = 1 To 300 newtree = CopyEntity(tree) x# = Rnd(512) z# = Rnd(512) PositionEntity newtree,x*2,TerrainHeight(terrain,x,z)*100-1,z*2 RotateEntity newtree,0,Rnd(360),0 scale# = 1+Rnd(0.5) ScaleEntity newtree,scale,scale,scale Next End FunctionNote that you must put you tree and terrain variable as Global to be accessible from the function. The trees position must be scaled according to terrain scale. But you should use "Types" for bullets, trees, and enemies, you can find some examples on how to use them in the Blitz3D documentation. It maybe hard to understand at first, but it's just essential. |
| ||
But it is also possible to remove the parent-child binding to get the bullet independet: you wrote: If KeyHit(57) ;Space to fire. ... Bullet=CreateCube(pgun) ... EntityParent Bullet,pgun EndIf but you need to remove the binding: If KeyHit(57) ;Space to fire. ... Bullet=CreateCube(pgun) ... EntityParent Bullet, 0 EndIf As I know you are not firm with TYPEs, i suggest to use an array to fire the bullets. Killing the current one, when firing a new bullet is not the best idea. What about this: Dim Bullet%[10] Global NextBullet%=1 ..... While Not KeyDown( 1 ) ..... If KeyHit(57) ;Space to fire. NextBullet=NextBullet+1 If NextBullet=11 Then NextBullet=1 Bullet(NextBullet)=CreateCube(pgun) EntityColor Bullet(NextBullet),222,0,0 ..... EntityParent Bullet(NextBullet), 0 EndIf For i%=1 to 10 If Bullet(i) MoveEntity Bullet(i), 0, 0, 1 If EntityDistance(player, Bullet(i))>200 FreeEntity Bullet(i) Endif EndIf Next EndIf Now you can fire a run of ten shoots before the first one will be destroyed |
| ||
Thanks for your help guys... Flanker...I tried your Ideas with the bullets but still had the same "steering the bullet" thing happening, I must not have understood something. ...Also when I try the tree spawn, I'm still having problems. Just to confirm. I would type in Createtrees() << before the main loop to call the function below the main loop... When I try this it keeps telling me the "Entity does not exist", and the cursor is flashing next to the ... newtree = CopyEntity(tree) line. I really don't get this as I have a ~create tree~ code way before the main loop. I actually tried a really similar code to this before and had the same problem. M I D I M A S T E R: .. The bullet code works like a charm, but seems to cause a runtime error after I fire 20-30 bullets. I might just slow the bullets and see what happens but the result is exactly what I was trying to achieve. Cheers again for your help. LOL I can't believe you remembered how great I am with ARRAYS!!!... but I will be trying to slip more and more in as it starts to sink in. Cheers for your help guys... this as sad as it might look to you is the best start I've had to date making a game and its heaps of fun!. |
| ||
please try this:If KeyHit(57) ;Space to fire. NextBullet=NextBullet+1 If NextBullet=11 Then NextBullet=1 If Bullet(NextBullet) Then FreeEntity Bullet(NextBullet) Bullet(NextBullet)=CreateCube(pgun) EntityColor Bullet(NextBullet),222,0,0 ..... EntityParent Bullet(NextBullet), 0 EndIf maybe you fire faster than the bullets die by distance. so we should add a FreeEntity before creating a new one. to your "tree"-problem: Try to define the first tree as "Global" ;create tree Global tree%=LoadMesh("Tree3.3ds") ;grab any tree.3ds model and throw it in the exe folder ScaleEntity tree,1,1,1 .... Createtrees() |
| ||
Wow you are fast, thanks I'll give it a try right away :D |
| ||
Rats still no luck, If I fire just one shot..after about 5 seconds an error pops up "the entity does not exist". and the cursor blinks at the beginning of the ....MoveEntity Bullet(i), 0, 0, 1 ...line. That aside if I don't fire a shot and call the createtree function I get the same "The entity does not exist" error. It all seems in the right spot. I have removed all the comments to make it easier to read. Is it possible I have to go by a certain load order for the trees?. Graphics3D 1920,1080,0 SetBuffer BackBuffer() camera=CreateCamera() PositionEntity camera,1,1,-100 CameraFogMode camera,1 CameraFogRange camera,10,100 ;.................................................. ;create player model player=CreateCube() ScaleEntity player,.15,.4,.15 EntityColor player,0,0,255 PositionEntity player,100,.3,100 phead=CreateSphere() ScaleEntity phead,.2,.2,.2 EntityColor phead,0,0,255 PositionEntity phead,100,1,100 EntityParent phead,player pgun=CreateCube() ScaleEntity pgun,.01,.03,.2 EntityColor pgun,200,200,255 PositionEntity pgun,100.2,.5,100.2 EntityParent pgun,player emove=0 ;................................................. ; create enemy model enemy=CreateCube() ScaleEntity enemy,.15,.4,.15 EntityColor enemy,255,0,0 PositionEntity enemy,10,20,20 ehead=CreateSphere() ScaleEntity ehead,.2,.2,.2 EntityColor ehead,255,0,0 PositionEntity ehead,10,20.7,20 EntityParent ehead,enemy egun=CreateCube() ScaleEntity egun,.01,.03,.2 EntityColor egun,200,200,255 PositionEntity egun,20.2,.5,20.2 EntityParent egun,enemy ;................................................ ;Bullet Array Dim Bullet%(10) Global NextBullet%=1 ;light world light=CreateLight() RotateEntity light,90,0,0 ;Load terrain terrain=LoadTerrain( "heightmap.bmp" ) ;create water plane water=CreatePlane() ScaleEntity water,100,100,100 PositionEntity water,1,4,1 EntityColor water,0,0,255 ScaleEntity terrain,2,100,2 ;create tree Global tree%=LoadMesh("Tree3.3ds") ScaleEntity tree,1,1,1 ;create sky sky=CreateSphere(50) PositionEntity sky,100,1,100 FlipMesh sky ScaleEntity sky,500,500,500 skytex=LoadTexture("sky512.jpg") EntityTexture sky,skytex EntityFX Sky,9 ;call functions TerrainColore (terrain) createtrees() ; main loop XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX While Not KeyDown( 1 ) If KeyDown( 203 )=True Then x#=x#-0.1 If KeyDown( 205 )=True Then x#=x#+0.1 If KeyDown( 208 )=True Then y#=y#-0.1 If KeyDown( 200 )=True Then y#=y#+0.1 If KeyDown( 44 )=True Then z#=z#-0.1 If KeyDown( 30 )=True Then z#=z#+0.1 If KeyDown( 205 )=True Then TurnEntity player,0,-1,0 If KeyDown( 203 )=True Then TurnEntity player,0,1,0 If KeyDown( 208 )=True Then MoveEntity player,0,0,-0.1 If KeyDown( 200 )=True Then MoveEntity player,0,0,0.1 If KeyHit(57) ;Space to fire. NextBullet=NextBullet+1 If NextBullet=11 Then NextBullet=1 If Bullet(NextBullet) Then FreeEntity Bullet(NextBullet) Bullet(NextBullet)=CreateCube(pgun) EntityColor Bullet(NextBullet),222,0,0 EntityParent Bullet(NextBullet), 0 EndIf For i%=1 To 10 If Bullet(i) MoveEntity Bullet(i), 0, 0, 1 If EntityDistance(player, Bullet(i))>200 FreeEntity Bullet(i) EndIf EndIf Next x#=EntityX(player) y#=EntityY(player) z#=EntityZ(player) xx#=EntityX(enemy) yy#=EntityY(enemy) zz#=EntityZ(enemy) tx#=EntityX(tree) ty#=EntityY(tree) tz#=EntityZ(tree) emove=emove+1 xx#=xx#+.05 zz#=zz#+.05 If emove>=5000 xx#=10 zz#=20 emove=0 EndIf terra_y#=TerrainY(terrain,x#,y#,z#) terra_yy#=TerrainY(terrain,xx#,yy#,zz#) PositionEntity player,x#,terra_y#+.5,z# PositionEntity enemy,xx#,terra_yy#+.4,zz# PositionEntity camera,x#,terra_y#+10,z#-25 ;update the skybox PositionEntity sky,x#,1,z# TurnEntity sky,0,0,.05 RenderWorld Text 0,0,"Use cursor keys to move about the terrain, Space to fire" Text 0,20,"player height Y: "+terra_y# Text 0,30,"Enemy height Y: "+terra_yy# Flip Wend End ; XXXXXXXXXXXXXXXXXXXX End Loop XXXXXXXXXXXXXXXXXXXXXXX Function CreateTrees() For i = 1 To 300 newtree = CopyEntity(tree) x# = Rnd(512) z# = Rnd(512) PositionEntity newtree,x*2,TerrainHeight(terrain,x,z)*100-1,z*2 RotateEntity newtree,0,Rnd(360),0 scale# = 1+Rnd(0.5) ScaleEntity newtree,scale,scale,scale Next End Function Function TerrainColore(Terrain%) Local px%,py% Tsz = TerrainSize (Terrain) Tex = CreateTexture (Tsz,Tsz):ScaleTexture(Tex,Tsz,Tsz) CBuf%= GraphicsBuffer () TBuf%= TextureBuffer (Tex):SetBuffer(TBuf):LockBuffer(TBuf) For px = 0 To Tsz-1 For py = 0 To Tsz-1 AH_T#=TerrainHeight(Terrain,px,py) If AH_T<.08 ; Sol->Mer => Sol->Sable Dh#=AH_T*1.0/.08 R=080+030*Dh G=050+030*Dh B=010+040*Dh ; fin = 110 / 080 / 050 ElseIf AH_T<.1 ; Sable Dh#=(AH_T-.08)*1/.02 R=110+080*Dh G=080+080*Dh B=050+070*Dh ; fin = 190 / 160 / 120 ElseIf AH_T<.15 ; Herbe Dh#=(AH_T-.1)*1/.05 R=190-170*Dh G=160-120*Dh B=120-110*Dh ; fin = 020 / 040 / 010 ElseIf AH_T<.7 ; Roche Dh#=(AH_T-.17)*1/.55 R=020+080*Dh G=040+060*Dh B=010+040*Dh ; fin = 120 / 100 / 050 Else ; neige Dh#=(AH_T-.72)*1/.3 R=120+020*Dh G=100+050*Dh B=050+160*Dh ; fin = 180 / 150/ 210 EndIf If R<0 R=0 If G<0 G=0 If B<0 B=0 WritePixelFast px,Tsz-py-1,R Shl(16) + G Shl(8) + B Next Next UnlockBuffer(TBuf):SetBuffer(CBuf) EntityTexture (Terrain,Tex,0,0):FreeTexture(Tex) End Function |
| ||
At the moment I cannot check my code, but try this:If KeyHit(57) ;Space to fire. NextBullet=NextBullet+1 If NextBullet=11 Then NextBullet=1 If Bullet(NextBullet)>0 Then FreeEntity Bullet(NextBullet) Bullet(NextBullet)=CreateCube(pgun) EntityColor Bullet(NextBullet),222,0,0 ..... EntityParent Bullet(NextBullet), 0 EndIf .... For i%=1 to 10 If Bullet(i)>0 MoveEntity Bullet(i), 0, 0, 1 If EntityDistance(player, Bullet(i))>200 FreeEntity Bullet(i) Bullet(i)=0 Endif EndIf Next |
| ||
I copied and pasted the new code directly over the old code and on the line For i%=1 to 10 the game freezes and has an Expecting TO error pop up. I notice you put the "...." in the above code. Am I right in saying if I paste the whole new code over the old and remove those dots it should go , or are they indicating that the lower half of the code needs to go elsewhere?. :( Eventually I shall spawn a thousand enemies and kill them all on your behalf!!!... twice!. |
| ||
Whoa!!.. ok the bullet code DOES work!!!.. I re-installed Blitz3d and bam first go. Thanks MIDIMASTER! The Tree code still has the same problem. I have been over this and tried everything that makes sense to me. For now I'll move on and add other stuff in... I'll get back to it. :D |
| ||
You must also put the terrain as Global, as the CreateTrees() function needs to access the height of the terrain to position the trees. |
| ||
@Flanker Awesome the trees now work too!!!. I did not think of making them Global !!!... THANK YOU!!!! :D |
| ||
The dots .... in my samples are placeholders for your lines of code between the relevant changes. It means: "put in your lines here". |
| ||
Thanks guys!!, It's all working now as it should. MIDIMASTER: Thank you for your help, and clarification on the "....." thing. I'm going to try and throw in some AI now and see how I go. Cheers again. |