Cameras and Walls (3D Noob)

BlitzMax Modules Forums/MiniB3D/Cameras and Walls (3D Noob)

I have a code (that I'm think came with Maplet) and it lets you walk through the places you create. However, it is set in first-person view. I tried moving the camera back to create a third-person view, but the camera can go through a wall and ruin that feel of being inside.

What is the easiest way to create third-person view without letting the camera go through a wall?

That's weird, the wall should stop you from going through. Does the wall use a transparent texture, like water? You might need to use a heavier texture, something solid like wood or brick.

It stops the player (sphere) from going through, but not the camera. I tried to make the camera a 'solid', but then I had problems keeping the camera a set distance behind the player. I think I am missing an important concept or something.

This is actually a common problem with 3rd person programs. You need to figure out how to intelligently move the camera when walls or other objects start to get in the way.
There is an example in the code archives here
http://www.blitzbasic.com/codearcs/codearcs.php?code=1083

I translated and tried the code (Posted Above), but it didn't do what I wanted. I want the camera to not go outside of a mesh 'building'. The player can't get out, but the camera can.

To let the camera not pass the walls you have to create a collidable Pivot which is set to sliding collision. to this pivor you parent the camera. Or you update the position and rotation manual to avoid that the camera will look through the walls.
If you have no idea what I'm talking about I will make you an example.

I vaguely understand, but I really like examples.

OK ;)

Here is a modified castle demo code with 3rd person control:

Import klepto.minib3d

Global width=640,height=480,depth=16,mode=0

Graphics3D width,height,depth,mode

P:TPlayer = TPlayer.Create() 'creates a player type with camera and 3rd person control


Local light=CreateLight(1)
RotateEntity light,90,0,0

Local mesh=LoadMesh("media/test.b3d")
ScaleEntity mesh,10,10,10

;' create mesh octree - makes collision detection faster (MiniB3D only)
CreateOctree(mesh,200,5)

 ; ' set mesh entity type to 2
EntityType mesh,2

;' use collisions command to enable colliisons between entity type 1 and 2, with reponse 4 (sphere only)
Collisions 1 , 2 , 4 , 2


;' used by camera code
Local mxs#=0
Local mys#=0
Global move#=0.5
MouseXSpeed() ;' flush
MouseYSpeed() ; ' flush




;' used by fps code
Local old_ms=MilliSecs()
Local renders
Local fps

While Not KeyDown(KEY_ESCAPE)		

	If KeyHit(KEY_ENTER) Then DebugStop

	;'' control camera
	
	;' mouse look
	
	
	
	
	;' move camera forwards/backwards/left/right with cursor keys
	
	P.Update()
	;''

	UpdateWorld
	RenderWorld

	renders=renders+1

	;' calculate fps
	If MilliSecs()-old_ms>=1000
		old_ms=MilliSecs()
		fps=renders
		renders=0
	EndIf

	Text 0,0,"FPS: "+fps

	Flip
	
Wend
End

Type TPlayer
	Field Entity:TEntity
	Field Camera:TCamera
	Field PlayerPivot:TPivot
	Field mxs:Float
	Field mys:Float
	Field Jump:Byte = False
	Field JumpTime:Int = 0
	
	Function Create:TPlayer()
		Local P:TPlayer = New TPlayer
		P.Entity = CreateCube()
		P.PlayerPivot = CreatePivot(P.Entity)
		P.Camera = CreateCamera()
		
		TranslateEntity P.PlayerPivot , 0 , 1 , - 10
		ScaleEntity P.Entity , 2 , 4 , 2
		PositionEntity P.Camera , 0 , 10 , - 5
		PositionEntity P.Entity,0,14,0
		CameraRange P.Camera,.5,500

		EntityType P.Camera , 1
		EntityType P.Entity , 1
		
		EntityRadius P.Entity,4
		EntityRadius P.Camera , 1
		Return P
	End Function
	
	Method Update() 
				
		If KeyDown(KEY_UP)=True Then MoveEntity Entity,0,0,move# ;' move camera forward
		If KeyDown(KEY_DOWN)=True Then MoveEntity Entity,0,0,-move# ;' move camera back

		If KeyDown(KEY_LEFT)=True Then TurnEntity Entity,0,move#*5,0 ;' move camera left
		If KeyDown(KEY_RIGHT) = True Then TurnEntity Entity , 0 , - move# * 5 , 0 ; ' move camera right
		
		If KeyHit(KEY_SPACE) And Jump = False Then
			Jump = True
			JumpTime = MilliSecs()
		EndIf
		
		Smoothcam(camera , PlayerPivot , entity , 1.5)
		
		
		If Jump = True Then 
			MoveEntity Entity , 0 , .76 , 0
			If (MilliSecs() - JumpTime) > 400 Then
				Jump = False
			EndIf
		EndIf
		
		MoveEntity Entity , 0 , - 0.38 , 0 ' Gravity

	End Method

End Type

Function smoothcam(cam:TCamera,pivot:TEntity,target:TEntity,camspeed:Float = 5.0)

curx#=EntityX(cam)
curz#=EntityZ(cam)
destx#=EntityX(pivot,True)
destz#=EntityZ(pivot,True)

curx#=curx#+((destx#-curx#)/camspeed)
curz#=curz#+((destz#-curz#)/camspeed)
cury#=EntityY(pivot,True)+5.0

PositionEntity cam,curx#,cury#,curz#

PointEntity cam,target
End Function



I hope this works in the way you want.

Thanks klepto2. That works wonderfully. I used standard MiniB3D though.