drawing connection between 2 objects

Blitz3D Forums/Blitz3D Programming/drawing connection between 2 objects

ryan scott(Posted 2004) [#1]
i have 2 things that are totally separate from each other, and i want to draw a line connecting them. they don't have a parent/child relationship, and i don't want them to, i just want to show a line, in my game's editor.

the objects are movable by the user - i don't have to make the movement of the line automatic as with parent/child relationship - it can be recomputed from scratch each frame.

is there a way to draw a line betwen 2 objects in blitz3d? if not, what would you suggest?


Rob(Posted 2004) [#2]
First turn the 3D coordinates of both objects into 2D coordinates for the screen. You use the CameraProject command for this.

Each time you call CameraProject() it will place the results in ProjectedX() and ProjectedY(). Remember to draw your 2D graphics like Line after Renderworld though as Renderworld will draw over everything.

CameraProject camera,EntityX(ent1),EntityY(ent1),EntityZ(ent1)
x1=ProjectedX()
y1=ProjectedY()

CameraProject camera,EntityX(ent2),EntityY(ent2),EntityZ(ent2)
x2=ProjectedX()
y2=ProjectedY()

Line x1,y1,x2,y2



martonic(Posted 2004) [#3]
How about a small "cube" stretched out (scaled) in just one dimension, like z? Some calculations will be needed to determine the correct dimensions, position and rotation. Good luck.


big10p(Posted 2004) [#4]
I recommend using a 3D line - it'll look much nicer, IMO. :)

;
; Adds a 3D line to the specified mesh (based on original function by koekjesbaby ;)
; Note: 3D lines are only properly visible when rendered in wireframe mode!
; 
; Params:
; mesh     - Mesh to add 3D line to. If 0, a new mesh is created.
; x0,y0,z0 - Start point of line.
; x1,y2,z1 - End point of line.
; r,g,b    - Line colour.
;
; Returns:
; Handle of mesh the 3D line was added to.
;
Function create_3D_line(mesh,x0#,y0#,z0#,x1#,y1#,z1#,r%=255,g%=255,b%=255) 

	If mesh = 0 
		mesh = CreateMesh() 
		surf = CreateSurface(mesh) 
		EntityFX mesh,1+2+16
	Else 
		last_surf = CountSurfaces(mesh)
		surf = GetSurface(mesh,last_surf)
		If CountVertices(surf) > 30000 Then surf = CreateSurface(mesh)
	End If 

	v0 = AddVertex(surf,x0,y0,z0) 
	v1 = AddVertex(surf,x1,y1,z1)  
	v2 = AddVertex(surf,x0,y0,z0)  
	AddTriangle surf,v0,v1,v2
	
	VertexColor surf,v0,r,g,b
	VertexColor surf,v1,r,g,b
	VertexColor surf,v2,r,g,b

	Return mesh 

End Function


Using this will mean you'll have to do 2 renders - 1 in normal, 'solid' mode, and 1 in wireframe mode. Also, you have to clear and rebuild the 3D line mesh from scratch every time one of the two connected objects is moved.


John Blackledge(Posted 2004) [#5]
This is the function I use (nicked from a kind Blitzer). 'rope' is just a cylinder.

;--------------------------------------
Function RopeEntToEnt(rope, ent1, ent2)
;--------------------------------------
Local distance#
Local x1#,y1#,z1#,x2#,y2#,z2#
Local Radius#=0.5

ShowEntity rope
x1# = EntityX(ent1,True)
y1# = EntityY(ent1,True)
z1# = EntityZ(ent1,True)
x2# = EntityX(ent2,True)
y2# = EntityY(ent2,True)
z2# = EntityZ(ent2,True)
distance# = EntityDistance#(ent1, ent2)
FitMesh rope, -Radius# / 2, -Radius# / 2, 0, Radius#, Radius#, distance#
PositionEntity rope,x1#,y1#,z1#
PointEntity rope,ent2
End Function