Easy way to check if mesh is behind camera.

Blitz3D Forums/Blitz3D Programming/Easy way to check if mesh is behind camera.

Ross C(Posted 2009) [#1]
Simply use the cameraproject command on a mesh. If the ProjectedZ() command returns 0, the mesh is behind you. You could do this with tform commands too i guess, but meh, i thought i'd give it a mention :)


_PJ_(Posted 2009) [#2]
Just curious, but I presume Projected(Z) wont return negatives?
What about, say Projected X/Y if the object is to th local 'Left' or 'Below' ?


Ross C(Posted 2009) [#3]
Nah, it just returns 0 or 1. However, projectedx returns values up to -3000 odd for behind left of the camera, until it hits behind the camera, then it turns to 0 also.


Ross C(Posted 2009) [#4]
If you look up the projectedZ or X or Y, or camera project example in the blitzdocs and runs it, you'll get the picture :)


_PJ_(Posted 2009) [#5]
Hehe My laziness strikes again!

I was just remembering the post a while abck about someone who wanted to know the off-screen direction ogf the ball/player for a football game, and I see now, why CameraProject would be poor for that.

Nice technique you have, certainly easier from a coding standpoint to remove all the trig.


Guy Fawkes(Posted 2009) [#6]
nice :)

here. tried my best guys :)

;Is Camera behind entity?

Graphics3D 800, 600, 0, 2
Global blah$

AmbientLight 255,255,255

Global camera = CreateCamera()
CameraRange camera, 1, 10000
Global sky = CreateSphere(100.5)
EntityColor sky, 68, 207, 252
ScaleEntity sky, 1000, 1000, 1000
FlipMesh sky
Global player = CreateCube()
PositionEntity player, 0, 1, 0
Global land = CreatePlane()
EntityColor land, 128, 128, 128

For x = 1 To 10
block = CreateCube()
PositionEntity block, Rand(1, 20), 1, Rand(1, 20)
Next

While Not KeyHit(1)
PositionEntity camera, EntityX(player), EntityY(player)+2, EntityZ(player)-8
MoveEntity player, 0, 0, (KeyDown(200)-KeyDown(208))*1
TurnEntity player, 0, (KeyDown(203)-KeyDown(205))*1,0
UpdateWorld
RenderWorld
If behindentity() < 0 Or behindentity() > 0 Then Text 10, 10, blah$
Flip
Wend

Function behindentity()

x# = ProjectedX#()
y# = ProjectedY#()
z# = ProjectedZ#()

CameraProject(camera, x#, y#, z#)

If z# < 0 Then Return blah$ = "camera is behind entity"
If z# > 0 Then Return blah$ = "camera is in front of entity"

End Function


fix it how u wish :)

~DS~


gosse(Posted 2009) [#7]
Or you could simply use a dot product. (which could even let you know if you are in the field of view of the camera)


Ross C(Posted 2009) [#8]
I think it should be:

Function Entity_Behind(camera,entity)

   CameraProject(camera,EntityX(entity,true),EntityY(entity,true),EntityZ(entity,true))
  If ProjectedZ >0 then
     Return 0
  Else
     Return 1
  End if

End Function




_PJ_(Posted 2009) [#9]
If ProjectedZ() won't ever be negative, then

  If ProjectedZ >0 then
     Return 0
  Else
     Return 1
  End if


Can all be substituted with:

Return (Not(ProjectedZ))



Guy Fawkes(Posted 2009) [#10]
my bad :P


Ross C(Posted 2009) [#11]
Hehe, true. Gosse. Can you explain more about a dot product, and why it would let you know if your in the field of view? I've never fully understood them...


Guy Fawkes(Posted 2009) [#12]
yes please. and perhaps you could write an entire example and not just a function like i did for you guys so we both get it.

~DS~


gosse(Posted 2009) [#13]
I'm currently at work and can't write BB code right now. I'll give it a twirl tonight and benchmark the various solutions.


Stevie G(Posted 2009) [#14]
Another solution ...

Function Entity_Behind(camera,entity)

  tformpoint 0,0,0,entity, camera
  return ( tformedz() < 0 )   

End Function


A field of vision check without dot product.

Function EntityInFOV( Source , Entity , Yaw# = 30 , Pitch# = 30 , RangeMin# = 0 , RangeMax# = 1000 )

	OK = False

	TFormPoint 0,0,0, Entity, Source
	If TFormedZ() > RangeMin
		DY# = Abs( DeltaYaw( Source, Entity ) )
		DP# = Abs( DeltaPitch( Source, Entity ) )
		DI# = EntityDistance( Source, Entity )
		If DY < YawMax And DP < Pitch And DI < RangeMax
			OK = True
		EndIf
	EndIf

	Return OK
	
End Function



gosse(Posted 2009) [#15]
Here's the solution I fleshed out with dot product:


Damn it's been a while since I did any B3D :P
I don't check the field of view pitch, but adding it is trivial from this point.
What's left to do is to benchmark all of the solutions and see what is the best to use ;)


Ross C(Posted 2009) [#16]
Thanks Gosse :) Interesting code, i will study :)

Stevie, i take it your code uses the default FOV?


Stevie G(Posted 2009) [#17]
@ Ross, it's mainly for one entity's ability to see the other, rather than using a camera FOV.


Ross C(Posted 2009) [#18]
Ah, handy stuff :) Thanks.