collision problem (fps game mode)

Blitz3D Forums/Blitz3D Programming/collision problem (fps game mode)

yoshark(Posted 2012) [#1]
I'm in the process of creating an fps game...with multiplayer and have been running into a bunch of problems. One of the main problems is the collision effects of the bullets when they are shot from the gun. When I fire a bullet, the bullet stays in a certain position and does not follow the guns movement.....I know it may be difficult to understand but heres the code...I'm pretty much a beginnner so no answers extremely difficult to understand plz. thanks!

[bbcode]
welcome()
Function welcome()
Graphics 1280,960
screen=LoadImage("fps welcome.bmp")
DrawImage screen,0,0,0
While Not KeyDown(1)
If KeyDown(61) Then
screen=LoadImage("fps controls.bmp")
DrawImage screen,0,0,0
End If
If KeyDown(29) Then
screen=LoadImage("fps welcome.bmp")
DrawImage screen,0,0,0
End If
If KeyDown(62) Then
screen=LoadImage("fps map.bmp")
DrawImage screen,0,0,0
End If
If KeyDown(63) Then
screen=LoadImage("fps credits.bmp")
DrawImage screen,0,0,0
End If
If KeyDown(59) Then Return
Wend
End Function


Graphics3D 1280,960
SetBuffer BackBuffer()

;types
type_player=1
type_city=2
type_bullet=3
type_boundary=4

;camera
camera=CreateCamera()
Cam_range=100000
EntityType camera,type_player

;light
light=CreateLight()

;city
city=LoadMesh("metro 1.3ds")
tex=LoadTexture("metro01.jpg","metro02.jpg")
EntityTexture city,tex
PositionEntity city,0,-30,+100
ScaleEntity city,10,10,10
EntityType city,type_city

;soldier
soldier=LoadMesh("soldier.3ds")
tex3=LoadTexture("swattex.jpg")
RotateEntity soldier,0,180,0
ScaleEntity soldier,.08,.08,.08
EntityTexture soldier,tex3
PositionEntity soldier,EntityX(camera),EntityY(camera)-5,EntityZ(camera)+.5
EntityParent soldier,camera

;aimer
HidePointer
aimer=LoadImage("aimer.jpg")
MaskImage aimer,0,0,0
MidHandle aimer

;Creating the gun
gun=LoadMesh("paintball.3ds")
EntityColor gun,255,0,0
ScaleEntity gun,.007,.007,.007
PositionEntity gun,EntityX(camera)+1.2,EntityY(camera)-1.7,EntityZ(camera)-.2
EntityParent gun,camera

;Creating the bullets
maxbull = 100
Dim bullet(maxbull)
For i=0 To maxbull
bullet(i)=CreateSphere()
EntityColor bullet(i),255,0,0
ScaleEntity bullet(i),.1,.1,.1
EntityType bullet(i),type_bullet
Next

;boundary
boundary=CreateCube()
PositionEntity boundary,0,0,0
ScaleEntity boundary,2200,2200,2200
FlipMesh boundary
EntityAlpha boundary,0
EntityType boundary,type_boundary

;sky
sky=CreateSphere()
tex2=LoadTexture("sky8.jpg")
EntityTexture sky,tex2
ScaleEntity sky,100000,100000,100000
FlipMesh sky

;collisions
Collisions type_player,type_city,2,2
Collisions type_player,type_boundary,2,2
Collisions type_bullet,type_city,2,1
EntityRadius camera,2

;pro run
velocity#=0

While Not KeyDown(1)

;network game
If KeyDown(60)=True Then
Select StartNetGame()
Case 0
End
Case 1
Print "Joined!"
Case 2
Print "Server!"
End Select
End If

;changing sky texture
If KeyDown(42) Then
night=LoadTexture("star sky.jpg")
EntityTexture sky,night
End If
If KeyDown(54) Then
tex2=LoadTexture("sky8.jpg")
EntityTexture sky,tex2
End If

yaw#=yaw#-.01
roll#=roll#-.01
RotateEntity sky,0,yaw#,roll#

If KeyDown(47)=True Then
velocity2#=velocity2#-.4
TranslateEntity camera,0,-velocity2#,0
End If

;velocity
If CountCollisions (camera)
time=0
velocity#=0
velocity2#=0
End If
If KeyHit(57) Then
time=time+1
velocity#=2-.01*time
TranslateEntity camera,0,velocity#,0
End If

If MouseDown(3)=True Then MoveEntity camera,0,0,1
If MouseDown(3)=True Then cam_range=cam_range+1
CameraRange camera,1,cam_range
time=time+1
velocity#=velocity#-.01*time
TranslateEntity camera,0,velocity#,0

UpdateWorld

RenderWorld

If MouseHit(1) And reload=0 Then
PositionEntity bullet(t),EntityX(gun,1),EntityY(gun,1),EntityZ(gun,1)
RotateEntity bullet(t),EntityPitch#(gun,1),EntityYaw#(gun,1),EntityRoll#(gun,1)
EntityColor bullet(t),255,0,0
t=t+1
EndIf
For q = 0 To maxbull
MoveEntity bullet(q), 0,0,3
Next


bulletcount=100-t
If t=100 Then
reload=1
EndIf
If KeyDown (19) = True Then
t=0
reload=0
EndIf

DrawImage aimer,MouseX()+250,MouseY()+200

mxs#=mxs#+MouseXSpeed()/4
mys#=mys#+MouseYSpeed()/4

If mxs#<0 Then mxs#=360
If mxs#>360 Then mxs#=0
If mys#>80 Then mys#=80
If mys#<-80 Then mys#=-80

RotateEntity camera,mys#,-mxs#,0
MoveMouse 400,300

Color 255,255,255
Text 500,10,"Bullets Remaining: "+bulletcount
Color 0,255,0
Text 0,10,"Press Esc to Exit"

If reload=1 Then
Color 255,0,0
Text GraphicsWidth()/2, GraphicsHeight()/2,"Press R to Reload",1,1
End If

Flip

Wend
End
[/bbcode]

Last edited 2012


Midimaster(Posted 2012) [#2]
When you create a bullet, it should be the child of the gun. So you need not to care about starting point and direction, when fire.

The better way is to create the bullet even in the moment, when you fire.

to use your code:
If MouseHit(1) And reload=0 Then
     Bullet[t]=CreateSphere(8,Gun)
     ; now do all things to build up your bullet but not position and direction
     EntityColor Bullet[t] ....
     .....
     t=t+1

     ; then cut the relationship
     EntityParent Bullet[t],0


by the way: to optimize your code I would not use an array for the bullets but a type-list. There you could create bullets,when needed and delete them at the end of lifecycle without use of index numbers:

Type BulletTyp
   Field Entity%
End Type

If MouseHit(1) And reload=0 Then
     local Bullet.BulletType = New BulletType
     Bullet\Entity=CreateSphere(8,Gun)
     ; now do all things to build up your bullet
     EntityColor  Bullet\Entity....
     .....
     t=t+1

     ; then cut the relationship
     EntityParent Bullet\Entity,0


to cylce through all bullets you can use this:

For local locBullet = Each BulletType
     MoveEntity locBullet\Entity, 0,0,3

     ; kill a bullet at the end f.e.:
     If locBullet\EntityZ>100
          Delete locBullet
     Endif
Next



RemiD(Posted 2012) [#3]

When I fire a bullet, the bullet stays in a certain position and does not follow the guns movement...



I don't have the files to be able to test it but i think it is because you create the bullet at the position, orientation of the gun, and not at the position, orientation of the end of the barrel (where the bullet should start to move freely).

What you can do is to create a pivot, child to the gun and positioned where the barrel ends. Then you can use this pivot to position and to turn your bullet correctly.

Here :
PositionEntity bullet(t),EntityX(gun,1),EntityY(gun,1),EntityZ(gun,1)
RotateEntity bullet(t),EntityPitch#(gun,1),EntityYaw#(gun,1),EntityRoll#(gun,1)


Then will be :
RotateEntity bullet(t),EntityPitch(Barrel,True),EntityYaw(Barrel,True),EntityRoll(Barrel,True)
PositionEntity bullet(t),EntityX(Barrel,True),EntityY(Barrel,True),EntityZ(Barrel,True)



Another thing that may help you is to create another pivot, child of the barrel, and move it at +1000Z, and when a new bullet is created, you can use PointEntity(Bullet(i),Target) so that your bullet goes toward this target.

Of course a more appropriate way would be to add a mode where the user can put his weapon at the center of the screen in order to be able to aim accurately and the bullet will always move at the center of the screen. (as in Fallout 3)

Last edited 2012


yoshark(Posted 2012) [#4]
thanks midimaster...I was first thinking that I could just create a bullet each time that I clicked the mouse.....only the blitz3d programming book creates the bullets before hand, but your point is working to. Thanks again!

Make sure to check more on this game that I'm making...there is gona be alot of question..