PickedVertex()

Blitz3D Forums/Blitz3D Programming/PickedVertex()

aCiD2(Posted 2004) [#1]
Anyone know of a function do do this? Im currently trying...

	CameraPick(camera, MouseX(), MouseY())
	If PickedTriangle() And PickedEntity()
		
		surf = GetSurface(meshGridPlane, 1)
		
		PositionEntity chkpiv, VertexX(surf, TriangleVertex(surf, PickedTriangle(), 0)), VertexY(surf, TriangleVertex(surf, PickedTriangle(), 0)), VertexZ(surf, TriangleVertex(surf, PickedTriangle(), 0))
		PositionEntity vertCube, PickedX(), PickedY(), PickedZ()
		length1 = EntityDistance(chkpiv, vertCube)
		
		PositionEntity chkpiv, VertexX(surf, TriangleVertex(surf, PickedTriangle(), 1)), VertexY(surf, TriangleVertex(surf, PickedTriangle(), 1)), VertexZ(surf, TriangleVertex(surf, PickedTriangle(), 1))
		PositionEntity vertCube, PickedX(), PickedY(), PickedZ()
		length2 = EntityDistance(chkpiv, vertCube)
		
		PositionEntity chkpiv, VertexX(surf, TriangleVertex(surf, PickedTriangle(), 2)), VertexY(surf, TriangleVertex(surf, PickedTriangle(), 2)), VertexZ(surf, TriangleVertex(surf, PickedTriangle(), 2))
		PositionEntity vertCube, PickedX(), PickedY(), PickedZ()
		length3 = EntityDistance(chkpiv, vertCube)
		
		If Min(Min(length1, length2), Min(length2, length3)) = length2
			PositionEntity vertCube, VertexX(surf, TriangleVertex(surf, PickedTriangle(), 0)), VertexY(surf, TriangleVertex(surf, PickedTriangle(), 0)), VertexZ(surf, TriangleVertex(surf, PickedTriangle(), 0))
		ElseIf Min(Min(length1, length2), Min(length2, length3)) = length3
			PositionEntity vertCube, VertexX(surf, TriangleVertex(surf, PickedTriangle(), 1)), VertexY(surf, TriangleVertex(surf, PickedTriangle(), 1)), VertexZ(surf, TriangleVertex(surf, PickedTriangle(), 1))
		Else
			PositionEntity vertCube, VertexX(surf, TriangleVertex(surf, PickedTriangle(), 2)), VertexY(surf, TriangleVertex(surf, PickedTriangle(), 2)), VertexZ(surf, TriangleVertex(surf, PickedTriangle(), 2))
		EndIf
	EndIf


But it really doesn't work :)


AntonyWells(Posted 2004) [#2]
This will be better, as it returns the closet vertex to the pick, rather than you having to click on one exactly.

function pickedVertex()
local sd#=999999,srf
local vert,v,tri
    srf=pickedSurface()
    tri=pickedTriangle()
    for v=0 to 2
        vert=triangleVertex(srf,tri)
        xd#=vertexX(srf,vert)-pickedX()
        yd#=vertexY(srf,vetr)-pickedY()
        zd#=vertexZ(srf,vert)-pickedZ()
        td#=sqr(xd*xd+yd*yd+zd*zd)
        if td<sd
            sd=td
            rVert=vert
        endif
    next
    return rVert
end function


Another cheaper way for mouse click, would be to project each vert into screen space(cameraproject) and then just do a quick distance check from screen coords to mouse coords...no idea if it'll be any quicker.


Stevie G(Posted 2004) [#3]
I'm sure vertexx, y, z return the local position of the vertex whereas your pickedx, y, z return global coords. If you've rotated the meshgridplane then this may be the problem?

If that's the case then you could try something like this ..

vx= vertexx( surf, trianglevertex( surf, ..... etc. etc..
vy= etc..

TFORMvector, vx, vy, vz, meshgridplane, 0
positionentity chkpiv,tformedx(),tformedy(),tformedz()


aCiD2(Posted 2004) [#4]
i'll play around with it, and thanks for that Stevie :) I've always wondered the use of tformvector, i've only used point :)


DJWoodgate(Posted 2004) [#5]
Not tested...

Function pickedVertex()
	Local xd#,yd#,zd#,sd#,td#,px#,py#,pz#,srf,tri,vert,v
 	TFormPoint PickedX(),PickedY(),PickedZ(),0,PickedEntity()
	px = TFormedX() 
	py = TFormedY()
	pz = TFormedZ()
	srf = PickedSurface()
	tri = PickedTriangle()
	For v=0 To 2
		vert = TriangleVertex(srf,tri,v)
		xd = VertexX(srf,vert)-px
		yd = VertexY(srf,vert)-py
		zd = VertexZ(srf,vert)-pz
		td = xd*xd+yd*yd+zd*zd
		If td < sd Or v = 0
			sd=td
			rVert=vert
		EndIf
    Next
;	TFormPoint VertexX(srf,rVert),VertexY(srf,rVert),VertexZ(srf,rVert),PickedEntity(),0
;	PositionEntity vertcube,TformedX(),TformedY(),TformedZ(),True
    Return rVert
End Function