Collisions and Bullets

Blitz3D Forums/Blitz3D Beginners Area/Collisions and Bullets

This has probably happened before.
I set up collisions for my bullets so that they would stick in the ground like arrows, but when I gave the EntityType command for the bullets I found that I could only fire in certain areas.
I have yet to clean up my code so please bear with me:

Graphics3D 1024,764
SetBuffer BackBuffer()

;Types
type_player=1
type_terrain=2
type_missile=3

;Creating the camera
cam=CreateCamera()
ScaleEntity cam,1,1,1
PositionEntity cam,0,0,0
EntityType cam,type_player
EntityRadius cam,5

ground=LoadTerrain("Canyon1.bmp")
ScaleEntity ground,5,300,5
PositionEntity ground,-500,-200,-500
tex=LoadTexture("Desert Sand.jpg")
ScaleTexture tex,50,50
EntityTexture ground,tex
EntityType ground,type_terrain

sky=CreateSphere(40)
ScaleEntity sky,5000,5000,-5000
PositionEntity sky,0,-500,0
tex2=LoadTexture("sky 1.jpg")
EntityTexture sky,tex2
RotateEntity sky,0,0,90
EntityFX sky,1

gun=LoadMesh("Gun.3ds")
PositionEntity gun,0.2,-0.25,.5
ScaleEntity gun,.005,.005,.005
RotateEntity gun,0,90,-90
tex3=LoadTexture("Metal 3.jpg")
EntityTexture gun,tex3
EntityParent gun,cam
EntityType gun,type_player
EntityRadius gun,.005
HidePointer

light=CreateLight()

;Ceating the bullets
fullmag=100
Dim bullet(fullmag)
For i=0 To fullmag
bullet(i)=CreateSphere()
EntityColor bullet(i),255,217,0
EntityFX bullet(i),1
ScaleEntity bullet(i),.02,.02,.4
HideEntity bullet(i)
EntityType bullet(i),type_missile
EntityRadius bullet(i),.02,.02
Next

;Curve value
Function CurveValue#(current#,destination#,curve)

	current#=current#+((destination#-current#)/curve)
	
	Return current#

End Function

;How far the camera can see
cam_range=10000

;Collision method & respons
Collisions type_player,type_terrain,2,2
Collisions type_missile,type_terrain,2,1

While Not KeyDown(1)

;How near the camera can see
CameraRange cam,.1,cam_range

;Controls
If KeyDown(205)=True Then MoveEntity cam,.3,0,0
If KeyDown(203)=True Then MoveEntity cam,-.3,0,0
If KeyDown(208)=True Then MoveEntity cam,0,0,-0.3
If KeyDown(200)=True Then MoveEntity cam,0,0,.3

;Mouse movement
mxs=MouseXSpeed()*.25
mys=MouseYSpeed()*.25

dest_xangle#=dest_xangle#+mys
dest_yangle#=dest_yangle#-mxs

xangle#=CurveValue#(xangle#,dest_xangle#,3)
yangle#=CurveValue#(yangle#,dest_yangle#,3)
 
RotateEntity cam,xangle#,yangle#,0
    
MoveMouse GraphicsWidth()/2,GraphicsHeight()/2

;Firing bullets (for real)
If MouseHit(1) And reload=0 Then
ShowEntity bullet(t)
EntityParent bullet(t),gun
PositionEntity bullet(t),1,-1.5,-10
RotateEntity bullet(t),-90,0,0
EntityParent bullet(t),0
t=t+1
bam=LoadSound("pistol.wav")
PlaySound bam
EndIf

For q=0 To fullmag
MoveEntity bullet(q),.01,.03,2

Next

;Ammo count
ammocount=100-t
If t=100 Then
t=1
reload=1
EndIf

;Reloading
If KeyHit(19)=True Then
t=1
reload=0
EndIf

;Gravity
TranslateEntity cam,0,-.35,0

;Velocity/Jump
If KeyHit(54)=True And CountCollisions(cam)=True Then velocity#=1
If Not KeyHit(54)=True Then velocity#=velocity#-.008
If velocity#<-.1 Then velocity#=-.1
TranslateEntity cam,0,velocity#,0

;Checking for collisions
UpdateWorld

RenderWorld
If reload=0 Then
Text 0,10,"Ammo: "+ammocount
Else
Text 0,10,"Ammo: 0"
EndIf
If reload=1 Then Text GraphicsWidth()/2,20,"Press R To Reload",1,1
Text GraphicsWidth()/2,GraphicsHeight()/2,"(o)"
Flip

Wend

End 


Oh, yes.
I forgot to mention erlier that the bullet appear to fire from other places besides where the gun is.
I don't know if that makes a difference but I thought I should mention it.

What if you turn while the bullet is in the ground?

wouldn't it make the bullet turn too because you made the gun the parent of the bullet?

P.S. Code is very confusing. :)

Like I said, I have not cleaned up my code yet so I don't blame you for being confused.

As to the turning thing, the gun is parented to the camera, the bullets are suposed to originate from the gun and after they're fired they continue in their original direction, thus I am free to move afterward.

But now that I've added the EntityType command to bullet(i) it only fires from my gun in certain places.
I noticed that if I bloat up the size of the bullet, I see that it somtimes fires from the ground in the distace.
I don't know what to do about that.

First off, when using function, ALWAYS make your variables you intend to keep throughout the code, GLOBAL, else they may be overwritten very easily.

Second point, would be to use Types instead of an array if you know how to use them.

Thirdly, onto your code. I see no real need to make your bullets stick into the ground :o) But, i reckon your entityRadius is probably too big, in relation to your bullet size. By default the entityradius is set to 1, so if your bullet is pretty small, then it probably will get stuck on alot of things.

Try setting all your bullet radius to something very small, like 0.1 or smaller, and see if the problem still occurs.

Again though, i would have your bullet stuck in the ground. Each bullet you create, is going to be a seperate surface, and even if the polygon count isn't high, 100 surfaces or bullets will slow your graphics updates down a fair bit.

I'll need to look into Globals and Types some more.

I wanted my bullets to stick in the ground because my next step was to use HideEntity when they hit the ground. When I discovered that the bullets were not behaving like I intended, I decided to hold of on the HideEntity command for the time being.

As for my radius, it's already .02,.02 (I'll try deleting the extra .02).
I think I actually tried taking that code off and it didn't do anything (except defaulting the radius to 1).

I'll see what happens. Thanks for the tips.

Well, the problem doesn't seem to be the EntityRadius.
On a side note, I'm going to be gone until Friday night so I'll let you guys look at it some more. Tell me if you see anything.
Thanks.

Would help if you posted some media, so we can test this :o)

May have something to do with the blitz terrain as well.

I'm back.
I'll try using another height map, but when I get a good converter I'm going to redo the terrain.
How do I post media?

Put the Showentity Command Last in the fireing part.
For example:

What you did:
ShowEntity bullet(t)
EntityParent bullet(t),gun
PositionEntity bullet(t),1,-1.5,-10

What Might world:
EntityParent bullet(t),gun
PositionEntity bullet(t),1,-1.5,-10
ShowEntity bullet(t)



Try that out.

Thank you Terry.
That worked, not completely sure how, but it worked!
I'm still wondering how to post media, though.

It works because, when you position entity, the radius is still in effect, so if you show entity AFTER you position it, the radius doesnt effect the position.

Ok, that sounded strange but I think I understood enough of it.
Thanks again.