Code archives/3D Graphics - Effects/No clipping for FPS weapons!

This code has been declared by its author to be Public Domain code.

Download source code

No clipping for FPS weapons! by flying willy2005
This code shows a way to painlessly have guns that don't get stuck inside walls!
;--------------------------------------------------------------------------------
;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 = True

	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

	make usual fps gun that's rubbish!
	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

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

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

Comments

Drey2005
make usual fps gun that's rubbish!
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, -100

Give this a run..:D.


flying willy2005
EntityOrder might seem like the solution to you, however it will not work on some models. For example, if there's any transparency in it, the drawing order will be messed up.


Damien Sturdy2005
Drey, what about multi-player? best way i used so far is a multi render with everything parented.


jfk EO-111102005
or scale the gun down and move it close to the camera lense. Anyway, it can become a tricky issue sometimes, so any solution is welcome.


Drey2005
I multi render everything myself too. I was just showing him another option.


KuRiX2005
What's the problem with entityorder? MultiPlayer (EntityOrder your own weapon only)?


Zethrax2006
What's the problem with entityorder? MultiPlayer (EntityOrder your own weapon only)?


EntityOrder for meshes stuffs up the Z ordering of the mesh's polygons.

--

The camera in the posted code should be given a negative EntityOrder value to force it to be drawn on top of the existing graphics.

Also, if you parent the gun to the guncamera and then dynamically yaw and pitch the guncamera to match the player camera's yaw and pitch, you'll get correct lighting on the gun.


EQX2013

Also, if you parent the gun to the guncamera and then dynamically yaw and pitch the guncamera to match the player camera's yaw and pitch, you'll get correct lighting on the gun



Hmm ¿??!!
I think that is not possible, if we have the guncamera in Y = 65536 and a spotlight for example at x = -4, Y = 8, Z = -12 What does the angle here?
How light affects the weapon (vertex)?


Code Archives Forum