Positioning objects on vertices

Blitz3D Forums/Blitz3D Programming/Positioning objects on vertices

David819(Posted 2004) [#1]
Hi, how do i get objects like multiple sphere to each be positioned on a vertex on another object?


Rob Farley(Posted 2004) [#2]
Run throught the verts of an object getting the x,y and z of each and position an object at that position.


David819(Posted 2004) [#3]
i have tryed that but it doesn't seem to work, i remember there being code somewhere in the code achieves that did that but dont know where it is, i'll have a look for it.


AntonyWells(Posted 2004) [#4]
NewX=VertexX(vert1)

for eachVert(sphere)
x#=newX+vertexX(sphere,spherevert)
next

etc..


David819(Posted 2004) [#5]
ok thanks for the help, just on last question i am making a function to create hair for anyobject and have make a simple start to it, how do i get the object say if i was using a 3d rectangle how would i get it to go out of the object instead of at the top they come out of the enitity and at the bottom it comes though into the center of the object how would i sort that out?


jfk EO-11110(Posted 2004) [#6]
BTW. maybe you need to transform VertexX to real world coordinates using the TFormPoint command group.

And the hair thing: you could use Atan2 to determine the angle of a vertex in relation to the center of the mesh. You could then use this angle to let the hair "grow" away from the skin.


Gabriel(Posted 2004) [#7]
Or you could just use the vertex normal of each.


David819(Posted 2004) [#8]
ok, i'm sorry but i dont understand how to use these commands to get the hair strands to (from the positions on the vertices to them point away from the mesh instead of all going in the one direction, (sorry for the hassle but i aint very good with the understanding of how some of these things work).


jfk EO-11110(Posted 2004) [#9]
Indeed, Sybixsus' Idea is better. Try to read about vertexNX, vertexny and vertexNz , they give you the rotation of a vertex this will make it very easy to turn your hair right. I think you only need to add 90 to each value. Check it out, play with it.

And the TFormPoint Command is used like this

first find the surface of the mesh (would be useful when the fur has it's own surface), then

x#=vertexx(surf,i)
y#=vertexy(surf,i)
z#=vertexz(surf,i)

tformpoint x,y,z,mesh,0
x2#=tformedx()
y2#=tformedy()
z2#=tformedz()


Now you got the true positions of the vertices, relative to the world in x2,y2,z2, and reltive to the mesh still in x,y,z.

I don't know eactly how you want to align the hair Triangles to the vertices, but as mentioned before you can use vertexNx etc. to align them to the skin.


David819(Posted 2004) [#10]
ok, i have attempted to do that but it goes reall wrong it probably is the code of mine and that i am placing it in the wrong area, so because the code once done will be shared here it is.

Function Hair( mesh, length# )
surface = GetSurface(mesh,1)
For temp = 0 To CountVertices(surface) - 1
m2=CreateMesh()
surf=CreateSurface(m2)
AddVertex(surf,VertexX#(surface,temp), VertexY#(surface,temp), VertexZ#(surface,temp))
AddVertex(surf,VertexX#(surface,temp)+.01, VertexY#(surface,temp), VertexZ#(surface,temp))
AddVertex(surf,VertexX#(surface,temp), VertexY#(surface,temp)+length#, VertexZ#(surface,temp))
AddTriangle(surf,verts,verts+2, verts+1)
AddTriangle(surf,verts+1,verts+2,verts)
EntityColor m2,255,255,Rand(1,255)
EntityParent m2,mesh

Next
Return mesh
End Function

Graphics3D 640,480,32,2
SetBuffer BackBuffer()

camera=CreateCamera()
PositionEntity camera,0,0,-3

sphere = CreateSphere(30)

;If length#=>5 Then length#=5
h=Hair( sphere,.5 )


While Not KeyHit(1)
;TurnEntity sphere,0,1,0
WireFrame True
RenderWorld
Flip
Wend
End

How would i apply the code you gave and add it to my code to make it work?


big10p(Posted 2004) [#11]
If you're basically wanting to draw vert normals, I have a lib in the codes archives to do this:

http://www.blitzbasic.com/codearcs/codearcs.php?code=1085#comments


jfk EO-11110(Posted 2004) [#12]
I just rtied to solve it, but it's darn tricky. I'll let you know.


jfk EO-11110(Posted 2004) [#13]
I made this:

This is using Tris. From some angles they may "blink"
Graphics3D 640,480,32,2
SetBuffer BackBuffer()
camera=CreateCamera()
TranslateEntity camera,0,0,-3


c=CreateSphere(32) ; sphere width 32 segments for demo

;       mesh, lenght,width
c2=hair(c,0.2,.04)

;WireFrame 1

While Not KeyDown(1)
 TurnEntity c,.33,1,0
 RenderWorld()
 Flip
Wend
End

Function hair(mesh,ln#,wid#)
 s=GetSurface(mesh,1)
 m3=CreateMesh()
 For i=0 To CountVertices(s)-1
  m2=CreateMesh()
  s2=CreateSurface(m2)
  ScaleMesh m2,wid,ln,wid
  FitMesh m2,-MeshWidth(m2)/2.0,0,-MeshDepth(m2)/2.0,MeshWidth(m2),MeshHeight(m2),MeshDepth(m2)
  x#=VertexX(s,i)
  y#=VertexY(s,i)
  z#=VertexZ(s,i)
  
  nx#=VertexNX(s,i)
  ny#=VertexNY(s,i)
  nz#=VertexNZ(s,i)

  TFormPoint x,y,z,mesh,0
  x=TFormedX()
  y=TFormedY()
  z=TFormedZ()
  v0=AddVertex(s2,0,0,0)
  v1=AddVertex(s2,0,0+ln,0)
  v2=AddVertex(s2,0,0,0+wid)
  red  =Rand(255)
  green=Rand(255)
  blue =Rand(255)
  VertexColor s2,v0,red,green,blue
  VertexColor s2,v1,red,green,blue
  VertexColor s2,v2,red,green,blue
  AddTriangle(s2,v0,v1,v2)
  UpdateNormals m2

  AlignToVector m2,nx,ny,nz,2 ; didn't find a better way yet
  mpitch#= EntityPitch(m2,1)
  myaw#=   EntityYaw(m2,1)
  mroll#=  EntityRoll(m2,1)
  RotateEntity m2,0,0,0,1
  RotateMesh m2,mpitch,myaw,mroll
  PositionMesh m2,x,y,z
  EntityParent m2,mesh
  AddMesh m2,m3
  FreeEntity m2
 Next
 UpdateNormals m3 
 EntityFX m3,16 Or 2 ; 2=use vertexcolors, 16=show both sides of triangles
 EntityParent m3,mesh
End Function


This version is using cones instead. It may look better or more constant, but it also uses 3 times as much polygones:
Graphics3D 640,480,32,2
SetBuffer BackBuffer()
camera=CreateCamera()
TranslateEntity camera,0,0,-3


c=CreateSphere(32) ; sphere width 32 segments for demo

;       mesh,lenght,width
c2=hair(c,0.1,.01)

;WireFrame 1

While Not KeyDown(1)
 TurnEntity c,.33,1,0
 RenderWorld()
 Flip
Wend
End

Function hair(mesh,ln#,wid#)
 s=GetSurface(mesh,1)
 m3=CreateMesh()
 For i=0 To CountVertices(s)-1
  m2=CreateCone(3)
  ScaleMesh m2,wid,ln,wid
  FitMesh m2,-MeshWidth(m2)/2.0,0,-MeshDepth(m2)/2.0,MeshWidth(m2),MeshHeight(m2),MeshDepth(m2)
  x#=VertexX(s,i)
  y#=VertexY(s,i)
  z#=VertexZ(s,i)
  
  nx#=VertexNX(s,i)
  ny#=VertexNY(s,i)
  nz#=VertexNZ(s,i)

  TFormPoint x,y,z,mesh,0
  x=TFormedX()
  y=TFormedY()
  z=TFormedZ()
  red  =Rand(255)
  green=Rand(255)
  blue =Rand(255)
  s2=GetSurface(m2,1)
  For i2=0 To CountVertices(s2)-1
   VertexColor s2,i2,red,green,blue
  Next

  AlignToVector m2,nx,ny,nz,2 ; didn't find a better way yet
  mpitch#= EntityPitch(m2,1)
  myaw#=   EntityYaw(m2,1)
  mroll#=  EntityRoll(m2,1)
  RotateEntity m2,0,0,0,1
  RotateMesh m2,mpitch,myaw,mroll
  PositionMesh m2,x,y,z
  EntityParent m2,mesh
  AddMesh m2,m3
  FreeEntity m2
 Next
 UpdateNormals m3 
 EntityFX m3,2
 EntityParent m3,mesh
End Function



I really had to use the AlignToVector Trick to make this work. Anyway, This way you can place only one "hair" on each Vertex of the skin. For a real Fur or Hair Effect you may use some kind of Special FX Tricks, as seen in the code archives (search for "fur").


David819(Posted 2004) [#14]
Ok, thanks that should help me alot, it will take me a while to finnish the function but i should be able to do the rest, thanks jfk, big10p, Sybixsus, Tony Soprano, and Rob Farley for all the help.


David819(Posted 2004) [#15]
Ok, i have been trying to do some work with the code but cant do any more, cause i do not have the programing skill to do what it needs, so i am gonig to put the code up for others and might if i learn anything of programing physics make updates.

link:
http://www.blitzbasic.co.nz/codearcs/codearcs.php?code=1113