Collision question
Blitz3D Forums/Blitz3D Beginners Area/Collision question
| ||
I like to use function in my program as well as types, but i am getting a problem when trying to create collisions. I put the Collisions command directly before my game loop, and i then in my game loop create objects when a certain action happens. While Not Keydown(1) ; If MouseHit(1) Then Select g_currentweapon Case "pistol" pistol.weapontype = New weapontype ;create a new projectile and add it to type_projectile pistol\projectile = CreateSphere() EntityRadius pistol\projectile, .01 ;make the sphere smaller like a bullet ScaleEntity pistol\projectile, .02, .02, .02 PositionEntity pistol\projectile, EntityX(camera), EntityY(camera), EntityZ(camera) TurnEntity pistol\projectile, EntityPitch(camera), EntityYaw(camera), EntityRoll(camera) pistol\name$ = "pistol" EntityType pistol\projectile, type_projectile End Select End If UpdateWorld() RenderWorld() Flip Next Now collisions will work when that code is inserted direcly in the main game loop like above. But here is the problem. If i get that same exact code and put it into a functin like below, it will never create a collision. While Not Keydown(1) ; ;Here i just replaced the code above with this function TestInput() UpdateWorld() RenderWorld() Flip Next Function TestInput() If MouseHit(1) Then Select g_currentweapon Case "pistol" pistol.weapontype = New weapontype ;create a new projectile and add it to type_projectile pistol\projectile = CreateSphere() EntityRadius pistol\projectile, .01 ;make the sphere smaller like a bullet ScaleEntity pistol\projectile, .02, .02, .02 PositionEntity pistol\projectile, EntityX(camera), EntityY(camera), EntityZ(camera) TurnEntity pistol\projectile, EntityPitch(camera), EntityYaw(camera), EntityRoll(camera) pistol\name$ = "pistol" EntityType pistol\projectile, type_projectile End Select End If End if End Function Is there a way to get around this problem in blitz3D? I know you can use arrays, but arrays do not offer the same power as types do. When i created a 2d game i had no poblem with testing collisions from space ship that were creating ouside of the main game loop in a function. I know that Blitz3D is different, but is there any command that will allow me still have collisions even if the type is instantiated out side of the main loop? |
| ||
Have you remembered to make g_currentweapon global? [edit] And type_projectile will need global scope, too. I.e. Make sure it's a Const. Just make sure that anything referenced in the function has global scope! |
| ||
yes i made g_currentweapon global. My types are created properly; the bullets fly across the screen and hit the objects but do not react to the collision unless the types are instantiated in the main game loop. Wierd stuff. :-( |
| ||
This code workes: Run it for yourself to seeGraphics3D 1024, 768 ;Setup Buffer SetBuffer BackBuffer() ;Constants: Format (CONSTANT) ALL CAPS. Const ESCKEY% = 1 ;Types: Format ( xtype ) where x is the name of the type Type bullettype Field projectile Field name$ End Type ;Object Collision Types: go here format ( type_x = num ) where x = any type name ; and num = any number type_bullet = 1 type_enemy = 2 ;Global Variables: Format ( g_x) where x = name of variables ;Create the Camera camera = CreateCamera() ;Create the Light light = CreateLight() ;Create the cone to shoot at cone = CreateCone(10) PositionEntity cone, 0,0, 5 ;add the cone to the enemy type EntityType cone, type_enemy ;Set up the collisions Collisions type_bullet, type_enemy, 2 , 2 ;Main Game Loop While Not KeyDown(ESCKEY) If MouseHit(1) bullet.bullettype = New bullettype bullet\projectile = CreateSphere() ;makes the bullet originate from the camera PositionEntity bullet\projectile, EntityX(camera), EntityY(camera), EntityZ(camera) ;adds the bullet\projectile to the type_bullet for collision testing EntityType bullet\projectile, type_bullet End If For bullet.bullettype = Each bullettype MoveEntity bullet\projectile, 0 , 0 , .1 Next For bullet.bullettype = Each bullettype ;if a collision occured then chage the sphere that hit the cone to red. If CountCollisions(bullet\projectile) EntityColor bullet\projectile, 255, 0 ,0 End If Next UpdateWorld () RenderWorld() Flip Wend Now put the code in the main body into a simple function and the colisions will not work. Graphics3D 1024, 768 ;Setup Buffer SetBuffer BackBuffer() ;Constants: Format (CONSTANT) ALL CAPS. Const ESCKEY% = 1 ;Types: Format ( xtype ) where x is the name of the type Type bullettype Field projectile Field name$ End Type ;Object Collision Types: go here format ( type_x = num ) where x = any type name ; and num = any number type_bullet = 1 type_enemy = 2 ;Global Variables: Format ( g_x) where x = name of variables ;Create the Camera Global camera = CreateCamera() ;Create the Light light = CreateLight() ;Create the cone to shoot at cone = CreateCone(10) PositionEntity cone, 0,0, 5 ;add the cone to the enemy type EntityType cone, type_enemy ;Set up the collisions Collisions type_bullet, type_enemy, 2 , 2 ;Main Game Loop While Not KeyDown(ESCKEY) TestInput() UpdateWorld () RenderWorld() Flip Wend Function TestInput() If MouseHit(1) bullet.bullettype = New bullettype bullet\projectile = CreateSphere() ;makes the bullet originate from the camera PositionEntity bullet\projectile, EntityX(camera), EntityY(camera), EntityZ(camera) ;adds the bullet\projectile to the type_bullet for collision testing EntityType bullet\projectile, type_bullet End If For bullet.bullettype = Each bullettype MoveEntity bullet\projectile, 0 , 0 , .1 Next For bullet.bullettype = Each bullettype ;if a collision occured then chage the sphere that hit the cone to red. If CountCollisions(bullet\projectile) EntityColor bullet\projectile, 255, 0 ,0 End If Next End Function thanks guys. |
| ||
Like I said, make sure everything needed has global scope. Change these to Constants and it works. :)Const type_bullet = 1 Const type_enemy = 2 |
| ||
ok thanks, works great. I put those as global right before i got your message on here and now it works like a charm. If i use Const does that make it global as well? Thanks! |
| ||
Yep. Constants have global scope. You may want to define your constant names using all capitals - I, and I believe most people, use this convention to make constants easily distinguishable from variables. |