No clipping for FPS weapons!


Tweet blitz3d first-person shooter code-archives effects
(Posted 4 months ago) RonTek

This code shows a way to painlessly have guns that don't get stuck inside walls!

Author: flying willy

Updated by: TomToad, markcwm

Image

;--------------------------------------------------------------------------------
;avoiding camera clipping with weapons (fps example) - see MAKE GUN, below...
;by rob
;--------------------------------------------------------------------------------

;collision vars
Const col_player=1,col_level=2

;--------------------------------------------------------------------------------

;setup display
Graphics3D 800,600,0,2
HidePointer

;--------------------------------------------------------------------------------

;camera
camera = CreateCamera() ;camera
CameraClsColor camera,200,200,255

;--------------------------------------------------------------------------------

;player
player = CreatePivot() ;player
EntityType player,col_player
EntityRadius player,2
EntityParent camera,player ;attach main camera to player
MoveEntity camera,0,6,0 ;move camera to eye height

;--------------------------------------------------------------------------------

;MAKE GUN!

GUNFIX=True ;ENABLE THIS TO SEE THE GUN CLIPPING FIX, false for old method!

If GUNFIX = False

    guncamera = CreateCamera() ;for the gun
    CameraClsMode guncamera,0,1
    PositionEntity guncamera,0,65535+6,0

    ;same code as below, however, offset and NOT parented!
    gun=CreateCylinder()
    ScaleEntity gun,1,3,1
    RotateEntity gun,90,0,0
    TranslateEntity gun,0,65535+4,4
    EntityColor gun,0,0,255

Else

    ;using EntityOrder
    gun=CreateCylinder()
    ScaleEntity gun,1,3,1
    RotateEntity gun,90,0,0
    TranslateEntity gun,0,4,4
    EntityParent gun,camera ;not to player
    EntityColor gun,0,0,255
    EntityOrder gun,-1 ;render last, disables z-buffer

EndIf

;--------------------------------------------------------------------------------
;dummy texure
tex = CreateTexture(32,32,9)
SetBuffer TextureBuffer(tex)
ClsColor 255, 255, 255 : Cls
Color 128, 128, 128
Rect 0, 0, 16, 16
Rect 16, 16, 16, 16
ScaleTexture tex,0.2,0.2
SetBuffer BackBuffer()

;make level
temp=CreatePlane(8)
EntityColor temp,100,200,100
For i=0 To 100
    temp=CreateCube()
    PositionEntity temp,Rnd(-1000,1000),0,Rnd(-1000,1000)
    ScaleEntity temp,1+Rnd(50),10,1+Rnd(50)
    RotateEntity temp,0,Rnd(360),0
    EntityType temp,col_level
    EntityTexture temp,tex
Next

;--------------------------------------------------------------------------------

;activate collisions
Collisions col_player,col_level,2,2

;--------------------------------------------------------------------------------

;mainloop
While Not KeyHit(1)

    Cls

    ;get mouse
    mxspd#=MouseXSpeed()
    myspd#=MouseYSpeed()
    MoveMouse 400,300

    ;turn camera
    ;note, we change only camera pitch as it's attached to the pivot of the player which
    ;only changes it's yaw. This for various reasons makes it easy to control.

    pitch#=pitch#+myspd*0.1
    yaw#=yaw#-mxspd*0.1
    RotateEntity camera,pitch,0,0
    RotateEntity player,0,yaw,0

    ;move player
    spd#=0.8
    If KeyDown(200) MoveEntity player,0,0,spd
    If KeyDown(208) MoveEntity player,0,0,-spd
    If KeyDown(205) MoveEntity player,spd,0,0
    If KeyDown(203) MoveEntity player,-spd,0,0

    UpdateWorld
    RenderWorld
    Flip
Wend
End
;--------------------------------------------------------------------------------
(Posted 4 months ago) markcwm commented:
TranslateEntity gun,0,65535+4,4

That seems odd! What about using EntityOrder gun,-1?

(Posted 4 months ago) RonTek commented:

Hey Mark, the actual code still looks about right. EntityOrder gun,-1 apparently makes the gun disappear.

Edit: Forgot to use -1

(Posted 4 months ago) TomToad commented:

Hey Mark, the actual code still looks about right. EntityOrder gun,1 apparently makes the gun disappear.

Use EntityOrder gun,-1
positive numbers will cause the entity to be drawn first, behind everything else
negative numbers will cause the entity to be drawn last, in front of everything else.

;make usual fps gun that would be rubbish except when using EntityOrder
    gun=CreateCylinder()
    ScaleEntity gun,1,3,1
    RotateEntity gun,90,0,0
    TranslateEntity gun,0,4,4
    EntityParent gun,player
    EntityColor gun,0,0,255
    EntityOrder gun,-1 ;this fixes the problem
(Posted 4 months ago) markcwm commented:

Yes, what TomToad said. From the docs (which are online at Kippykip.com)

Sets the drawing order for an entity.

An order value of 0 will mean the entity is drawn normally. A value greater than 0 will mean that entity is drawn first, behind everything else. A value less than 0 will mean the entity is drawn last, in front of everything else.

Setting an entity's order to non-0 also disables z-buffering for the entity, so should be only used for simple, convex entities like skyboxes, sprites etc.

EntityOrder affects the specified entity but none of its child entities, if any exist.

(Posted 4 months ago) RonTek commented:

that did solve the clipping problem, but it seems the entity parenting is now off.

(Posted 3 months ago) markcwm commented:

Yes, but the parenting was off anyway, it's because gun is parented to player which gets rotated on the y axis by the mouse, this causes the gun to not point up or down, I just parented to camera but there is probably another way to do it.

    gun=CreateCylinder()
    ScaleEntity gun,1,3,1
    RotateEntity gun,90,0,0
    TranslateEntity gun,0,4,4
    EntityParent gun,camera ;not to player
    EntityColor gun,0,0,255
    EntityOrder gun,-1 ;render last, disables z-buffer
(Posted 3 months ago) RonTek commented:

Ok that works now! Testing it again, I do see that the parenting was off from the start and have now updated the code. also I forgot to use -1 before. Thank you again both for the fix!

Reply To Topic (minimum 10 characters)

Please log in to reply