Any ideas on this?

Blitz3D Forums/Blitz3D Programming/Any ideas on this?

I have a gun model, and I've attached it to the camera as this is a FPS game. Following me? Good.

It runs and looks fine, but it has a clipping problem with the other meshes, not uncommon. So I usualy just do something like EntityOrder -1 and it works.

Not this time, it doesn't clip, but now some of the triangles in the mesh are flipped.

Any ideas?

Sounds like a vertex order problem with your model. Either that or part of your view is actually inside the model.

It only screws up the models view only. It's like I fliped the vertex's. vertex order might be it, but how to fix it.

open in a modeller, select the face/triangle that is giving problems, and reverse vertex order. I don't know what modeller you use but Milkshape is great for this particular problem.

yup milkshape is what I got. but now it's even worse. I'll try to post a screenie



EntityOrder messes with the z-ordering of the meshes triangles, so there is nothing you can do about it.

A few solutions would be

- if the gun has collided/clipped with another object, draw it back (as you would in real life) and flag that the gun can't fire in this position. I've seen a few games that take this option

- scale down the gun mesh, put position it closer to the camera so that it appears in the same position (bottom right) and at the same scale. Scale it down so that it is within the collision radius of the player

- do a 2 pass render. First render the scene, then (without Z buffer clearing), render the gun.

Generally triangle flip when they are in the CameraRange Near using a different entityorder for the mesh and the world.
So the best way may be to resize the mesh .

EntityOrder messes with the z-ordering of the meshes triangles, so there is nothing you can do about it.

Correct, I think the zbuffer is disabled when drawing meshes that use EntityOrder. But you should be able to compensate for this by using the function below. (demo included so you can see the difference)

PCD: Try running the gun mesh through this ZSortMesh() function. It rebuilds the triangle index so that polys furthest along the Z axis will get rendered first.

Graphics3D 800,600,32,2

cam=CreateCamera()
PositionEntity cam,-1,1,-4
light=CreateLight()
RotateEntity light,20,20,0

;Create an unsorted mesh
mesh=CreateSphere(16)
EntityColor mesh,255,0,0
mesh2=CreateSphere(16)
PositionMesh mesh2,0,0,4
AddMesh mesh2,mesh
FreeEntity mesh2

EntityOrder mesh,-1

While Not KeyHit(1)

	If KeyHit(57) ZSortMesh mesh

	RenderWorld
	Text 0,0,"Press SPACE to Z-Sort mesh"
	Flip	
Wend




Type ztri
	Field z#
	Field v0,v1,v2
End Type

Function ZSortMesh(mesh)
	Local temp.ztri = New ztri
	Local tris.ztri[4096] ; assuming meshes have 4096 or less triangles, increase as needed
	
	For i=0 To 4095
		tris[i] = New ztri
	Next

	surfCount = CountSurfaces(mesh)
	
	For i = 1 To surfCount
		surf = GetSurface(mesh,i)
		triCount = CountTriangles(surf)

		;Store each triangles Z distance (based on its vertices average Z value)
		For t = 0 To triCount-1
			z = VertexZ(surf,TriangleVertex(surf,t,0))
			z=z+VertexZ(surf,TriangleVertex(surf,t,1))
			z=z+VertexZ(surf,TriangleVertex(surf,t,2))
			z=z/3

			tris[t]\z = z
			tris[t]\v0 = TriangleVertex(surf,t,0) ;store triangle vertex indexes
			tris[t]\v1 = TriangleVertex(surf,t,1)
			tris[t]\v2 = TriangleVertex(surf,t,2)
		Next

		;Sort the triangles based on Z distance
		For x=0 To triCount-1
			For y=0 To triCount-1
				If x<>y; don't compare with itself
					If tris[y]\z < tris[x]\z
						;Swap the 2 tris
						temp\z  = tris[y]\z
						temp\v0 = tris[y]\v0
						temp\v1 = tris[y]\v1
						temp\v2 = tris[y]\v2

						tris[y]\z  = tris[x]\z
						tris[y]\v0 = tris[x]\v0
						tris[y]\v1 = tris[x]\v1
						tris[y]\v2 = tris[x]\v2
						
						tris[x]\z  = temp\z
						tris[x]\v0 = temp\v0
						tris[x]\v1 = temp\v1
						tris[x]\v2 = temp\v2
					End If
				End If
			Next
		Next

		;Clear the existing triangles
		ClearSurface surf,False,True

		;Add the sorted triangles
		For x = 0 To triCount-1
			AddTriangle surf,tris[x]\v0,tris[x]\v1,tris[x]\v2
		Next
	Next
	Delete Each ztri	
End Function


Take a look at this link

http://www.blitzbasic.com/codearcs/codearcs.php?code=1271

Basically it renders the weapons with a second camera and always appears over the rest of models..
I've implemented it and works fine..

regards!,

in my little fps game, i have got a constant "WeaponAntiClipinFactor#"

the smaller this factor is, the nearer and smaller is the weapon!

or you could just do something like tribes (and other games) and have the gun as an object in the world and test it for collisions with a simple trace, then either move the gun model back corresponding to the space infront, or rotate it so its pointing down a bit or at an angle left-right.

I could well be wrong here but perhaps its simply that part of the gun is closer to the camera than the 'near' CameraRange value. Try setting CameraRange 0.0001,1000 or something and see if that makes a difference.

EDIT - Actually, I think that's maybe what a couple of people were trying to say above.